Skip to content

Commit acf8c0b

Browse files
authored
Update httpserver_extension.cpp
1 parent bb74238 commit acf8c0b

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/httpserver_extension.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323

2424
#ifdef __EMSCRIPTEN__
2525
#define WEBSOCKET_SUPPORT
26-
#endif
27-
28-
#ifdef WEBSOCKET_SUPPORT
2926
#include <emscripten/websocket.h>
3027
#include <emscripten/val.h>
3128
#endif
@@ -362,6 +359,12 @@ void HandleHttpRequest(const duckdb_httplib_openssl::Request& req, duckdb_httpli
362359
}
363360
}
364361

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+
365368
void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t auth = string_t()) {
366369
if (global_state.is_running) {
367370
throw IOException("HTTP server is already running");
@@ -406,7 +409,8 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
406409

407410
EmscriptenWebSocketCreateAttributes attrs;
408411
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();
410414

411415
global_state.websocket_server = emscripten::val::global("WebSocket").new_(emscripten::val(attrs.url));
412416
#endif
@@ -446,6 +450,38 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
446450
}
447451
}
448452

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+
449485
void HttpServerStop() {
450486
if (global_state.is_running) {
451487
global_state.server->stop();

0 commit comments

Comments
 (0)