|
23 | 23 |
|
24 | 24 | #ifdef __EMSCRIPTEN__
|
25 | 25 | #define WEBSOCKET_SUPPORT
|
26 |
| -#endif |
27 |
| - |
28 |
| -#ifdef WEBSOCKET_SUPPORT |
29 | 26 | #include <emscripten/websocket.h>
|
30 | 27 | #include <emscripten/val.h>
|
31 | 28 | #endif
|
@@ -362,6 +359,12 @@ void HandleHttpRequest(const duckdb_httplib_openssl::Request& req, duckdb_httpli
|
362 | 359 | }
|
363 | 360 | }
|
364 | 361 |
|
| 362 | +#ifdef WEBSOCKET_SUPPORT |
| 363 | +EM_BOOL WebSocketOpen(int eventType, const EmscriptenWebSocketOpenEvent* websocketEvent, void* userData); |
| 364 | +EM_BOOL WebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent* websocketEvent, void* userData); |
| 365 | +EM_BOOL WebSocketMessage(int eventType, const EmscriptenWebSocketMessageEvent* websocketEvent, void* userData); |
| 366 | +#endif |
| 367 | + |
365 | 368 | void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t auth = string_t()) {
|
366 | 369 | if (global_state.is_running) {
|
367 | 370 | throw IOException("HTTP server is already running");
|
@@ -406,7 +409,8 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
|
406 | 409 |
|
407 | 410 | EmscriptenWebSocketCreateAttributes attrs;
|
408 | 411 | emscripten_websocket_init_create_attributes(&attrs);
|
409 |
| - attrs.url = ("ws://" + host_str + ":" + std::to_string(port)).c_str(); |
| 412 | + std::string ws_url = "ws://" + host_str + ":" + std::to_string(port); |
| 413 | + attrs.url = ws_url.c_str(); |
410 | 414 |
|
411 | 415 | global_state.websocket_server = emscripten::val::global("WebSocket").new_(emscripten::val(attrs.url));
|
412 | 416 | #endif
|
@@ -446,6 +450,38 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
|
446 | 450 | }
|
447 | 451 | }
|
448 | 452 |
|
| 453 | +#ifdef WEBSOCKET_SUPPORT |
| 454 | +EM_BOOL WebSocketOpen(int eventType, const EmscriptenWebSocketOpenEvent* websocketEvent, void* userData) { |
| 455 | + auto* state = static_cast<HttpServerState*>(userData); |
| 456 | + state->websocket_connections[websocketEvent->socket] = emscripten::val::global("WebSocket").new_(websocketEvent->socket); |
| 457 | + return EM_TRUE; |
| 458 | +} |
| 459 | + |
| 460 | +EM_BOOL WebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent* websocketEvent, void* userData) { |
| 461 | + auto* state = static_cast<HttpServerState*>(userData); |
| 462 | + state->websocket_connections.erase(websocketEvent->socket); |
| 463 | + return EM_TRUE; |
| 464 | +} |
| 465 | + |
| 466 | +EM_BOOL WebSocketMessage(int eventType, const EmscriptenWebSocketMessageEvent* websocketEvent, void* userData) { |
| 467 | + auto* state = static_cast<HttpServerState*>(userData); |
| 468 | + std::string message(reinterpret_cast<char*>(websocketEvent->data), websocketEvent->numBytes); |
| 469 | + |
| 470 | + // Process the message (e.g., run a query) |
| 471 | + Connection con(*state->db_instance); |
| 472 | + auto result = con.Query(message); |
| 473 | + |
| 474 | + // Convert result to JSON |
| 475 | + ReqStats req_stats{0.0, 0, 0}; // You may want to update these stats |
| 476 | + std::string json_output = ConvertResultToJSON(*result, req_stats); |
| 477 | + |
| 478 | + // Send the response back through the WebSocket |
| 479 | + state->websocket_connections[websocketEvent->socket].call<void>("send", json_output); |
| 480 | + |
| 481 | + return EM_TRUE; |
| 482 | +} |
| 483 | +#endif |
| 484 | + |
449 | 485 | void HttpServerStop() {
|
450 | 486 | if (global_state.is_running) {
|
451 | 487 | global_state.server->stop();
|
|
0 commit comments