From 8f44f6b508787420b153a3cfc9aa1d0246e5a6bc Mon Sep 17 00:00:00 2001 From: dab Date: Thu, 14 Aug 2025 15:33:18 +0000 Subject: [PATCH] refactor to make transport send/receive more universal (defer to apps to handle) --- backend/index.js | 31 +++++++++++++++++-------------- frontend/index.js | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/backend/index.js b/backend/index.js index da5a6f8..8cedaa6 100644 --- a/backend/index.js +++ b/backend/index.js @@ -27,7 +27,17 @@ const get_siteroot_html = function ({ page_title }) {return ` `.trim()} -const start_server = function ({ context_meta, config, jsbuild_app_frontend, parse_ws_message, event_handlers }) { +const wrap_transport_sendbytes = function (ws, msg_bytes) { + if (ws.readyState === WebSocket.OPEN) { + ws.send(msg_bytes) + } + else { + console.warn('ws readyState !== OPEN : dropping send') + } + return +} + +const start_server = function ({ context_meta, config, jsbuild_app_frontend, handle_transport_bytes }) { const make_session_object = (ws) => { const map_obj = { ws, @@ -147,18 +157,12 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend, par message: function (ws, message) { const m_bytes = Buffer.byteLength(message) console.info(['ws message', ws.data.uid, { size: m_bytes }]) - if (parse_ws_message !== undefined) { - const [ evt_type, evt_data ] = parse_ws_message(message) - const func_handle = event_handlers[evt_type] - if (func_handle !== undefined) { - func_handle(ws, evt_data) - } - else { - console.warn(['no handler defined for evt_type', evt_type]) - } + if (handle_transport_bytes !== undefined) { + const transport_send_bytes = wrap_transport_sendbytes.bind(null, ws) + handle_transport_bytes({ transport_send_bytes }, message) } else { - console.warn('Unexpected: missing "parse_ws_message" definition') + console.warn('Unexpected: "handle_transport_bytes" not defined') } return }, @@ -201,8 +205,7 @@ const close_active_ws_sessions = function ({ context_meta }) { const async_run = async function ({ config, jsbuild_app_frontend, - parse_ws_message, - event_handlers, + handle_transport_bytes, }) { const active_config = { // defaults @@ -216,7 +219,7 @@ const async_run = async function ({ const context_meta = { ws_map: new Map(), } - const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, parse_ws_message, event_handlers }) + const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, handle_transport_bytes }) console.info(server) process.on('SIGINT', async () => { console.log('SIGINT intercepted') diff --git a/frontend/index.js b/frontend/index.js index 16e1260..a99b100 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -87,7 +87,7 @@ class FramerockUtils { ws.send(msg_bytes) } else { - console.warn('ws readyState !== OPEN : dropping event send') + console.warn('ws readyState !== OPEN : dropping send') } return }