core: Enough websocket in C to run an app.

This commit is contained in:
2025-12-06 18:03:52 -05:00
parent c4ff00dec1
commit 69f9646955

View File

@@ -219,6 +219,7 @@ typedef struct _app_t
{
tf_http_request_t* request;
const char* settings;
JSValue opaque;
JSValue credentials;
tf_taskstub_t* taskstub;
JSValue process;
@@ -288,12 +289,40 @@ static void _http_json_send(tf_http_request_t* request, JSContext* context, JSVa
JS_FreeValue(context, json);
}
static JSValue _httpd_app_on_tfrpc(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data)
{
tf_printf("TODO: TFRPC\n");
return JS_UNDEFINED;
}
static JSValue _httpd_app_on_output(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data)
{
JSClassID class_id = 0;
app_t* app = JS_GetAnyOpaque(func_data[0], &class_id);
if (app)
{
_http_json_send(app->request, context, argv[0]);
}
return JS_UNDEFINED;
}
static JSValue _httpd_app_on_process_start(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data)
{
JSClassID class_id = 0;
app_t* app = JS_GetAnyOpaque(func_data[0], &class_id);
app->process = JS_DupValue(context, argv[0]);
tf_printf("ON START %p => %s\n", app, app->request->path);
JSValue client_api = JS_GetPropertyStr(context, app->process, "client_api");
JSValue tfrpc = JS_NewCFunctionData(context, _httpd_app_on_tfrpc, 1, 0, 1, func_data);
JS_SetPropertyStr(context, client_api, "tfrpc", tfrpc);
JS_FreeValue(context, client_api);
JSValue process_app = JS_GetPropertyStr(context, app->process, "app");
JSValue send = JS_NewCFunctionData(context, _httpd_app_on_output, 1, 0, 1, func_data);
JS_SetPropertyStr(context, process_app, "_on_output", send);
JS_FreeValue(context, process_app);
return JS_UNDEFINED;
}
@@ -391,9 +420,9 @@ static void _httpd_app_hello_after_work(tf_ssb_t* ssb, int status, void* user_da
JSValue promise_then = JS_GetPropertyStr(context, result, "then");
JSValue data = JS_NewObject(context);
JS_SetOpaque(data, work->app);
JSValue then = JS_NewCFunctionData(context, _httpd_app_on_process_start, 0, 0, 1, &data);
work->app->opaque = JS_NewObject(context);
JS_SetOpaque(work->app->opaque, work->app);
JSValue then = JS_NewCFunctionData(context, _httpd_app_on_process_start, 0, 0, 1, &work->app->opaque);
JSValue promise = JS_Call(context, promise_then, result, 1, &then);
tf_util_report_error(context, promise);
@@ -401,7 +430,6 @@ static void _httpd_app_hello_after_work(tf_ssb_t* ssb, int status, void* user_da
/* except? */
JS_FreeValue(context, data);
JS_FreeValue(context, then);
JS_FreeValue(context, promise_then);
@@ -491,7 +519,6 @@ static bool _httpd_app_message_call_message_handler(app_t* work, JSValue message
static void _httpd_app_on_message(tf_http_request_t* request, int op_code, const void* data, size_t size)
{
tf_printf("REQUEST MESSAGE %.*s\n", (int)size, (const char*)data);
app_t* work = request->user_data;
JSContext* context = request->context;
switch (op_code)
@@ -541,9 +568,11 @@ static void _httpd_app_on_close(tf_http_request_t* request)
{
JSContext* context = request->context;
app_t* work = request->user_data;
JS_SetOpaque(work->opaque, NULL);
JS_FreeValue(context, work->credentials);
_httpd_app_kill_task(work);
JS_FreeValue(context, work->process);
JS_FreeValue(context, work->opaque);
work->process = JS_UNDEFINED;
tf_free(work);