Bugs galore, but this is sending and receiving some websocket messages.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4697 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-12-25 23:39:16 +00:00
parent ccebf831e7
commit cd43bf9dfa
3 changed files with 41 additions and 8 deletions

View File

@ -116,14 +116,14 @@ static JSValue _httpd_response_send(JSContext* context, JSValueConst this_val, i
if (length < 126)
{
copy[1] = length;
header += 2;
header += 1;
}
else if (length < (1 << 16))
{
copy[1] = 126;
copy[2] = (length >> 8) & 0xff;
copy[3] = (length >> 0) & 0xff;
header += 4;
header += 3;
}
else
{
@ -138,19 +138,34 @@ static JSValue _httpd_response_send(JSContext* context, JSValueConst this_val, i
copy[7] = (low >> 16) & 0xff;
copy[8] = (low >> 8) & 0xff;
copy[9] = (low >> 0) & 0xff;
header += 10;
header += 9;
}
memcpy(copy + header, message, length);
tf_printf("SEND [%.*s]\n", (int)length, message);
tf_printf("SEND %d\n", (int)length);
tf_http_request_send(request, copy, header + length);
tf_free(copy);
JS_FreeCString(context, message);
return JS_UNDEFINED;
}
static void _httpd_message_callback(tf_http_request_t* request, const void* data, size_t size)
{
tf_printf("message [%.*s]\n", (int)size, (const char*)data);
JSContext* context = request->context;
JSValue response_object = JS_MKPTR(JS_TAG_OBJECT, request->user_data);
JSValue on_message = JS_GetPropertyStr(context, response_object, "onMessage");
JSValue event = JS_NewObject(context);
JS_SetPropertyStr(context, event, "opCode", JS_NewInt32(context, 0x1));
JS_SetPropertyStr(context, event, "data", JS_NewStringLen(context, data, size)); //tf_util_new_uint8_array(context, data, size);
JSValue response = JS_Call(context, on_message, JS_UNDEFINED, 1, &event);
tf_util_report_error(context, response);
JS_FreeValue(context, event);
JS_FreeValue(context, on_message);
}
static void _httpd_callback(tf_http_request_t* request)
{
#if 0
if (request->flags & k_tf_http_handler_flag_websocket)
{
const char* header_connection = tf_http_request_get_header(request, "connection");
@ -186,7 +201,6 @@ static void _httpd_callback(tf_http_request_t* request)
tf_http_respond(request, 101, headers, send_version ? k_headers_count : (k_headers_count - 1), NULL, 0);
}
}
#endif
http_handler_data_t* data = request->user_data;
JSContext* context = data->context;
@ -226,8 +240,11 @@ static void _httpd_callback(tf_http_request_t* request)
JSValue response = JS_Call(context, data->callback, JS_UNDEFINED, 2, args);
tf_util_report_error(context, response);
JS_FreeValue(context, request_object);
JS_FreeValue(context, response_object);
//JS_FreeValue(context, response_object);
JS_FreeValue(context, response);
request->on_message = _httpd_message_callback;
request->context = context;
request->user_data = JS_VALUE_GET_PTR(response_object);
}
static JSValue _httpd_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)