refactor to make transport send/receive more universal (defer to apps to handle)

This commit is contained in:
dab 2025-08-14 15:33:18 +00:00
parent 47507e80ae
commit 8f44f6b508
2 changed files with 18 additions and 15 deletions

View file

@ -27,7 +27,17 @@ const get_siteroot_html = function ({ page_title }) {return `
</html>
`.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)
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(['no handler defined for evt_type', evt_type])
}
}
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')

View file

@ -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
}