added send and receive events for websocket transport

This commit is contained in:
dab 2025-08-14 12:08:56 +00:00
parent 605d72f9b0
commit 47507e80ae
2 changed files with 47 additions and 11 deletions

View file

@ -27,7 +27,7 @@ const get_siteroot_html = function ({ page_title }) {return `
</html> </html>
`.trim()} `.trim()}
const start_server = function ({ context_meta, config, jsbuild_app_frontend }) { const start_server = function ({ context_meta, config, jsbuild_app_frontend, parse_ws_message, event_handlers }) {
const make_session_object = (ws) => { const make_session_object = (ws) => {
const map_obj = { const map_obj = {
ws, ws,
@ -144,7 +144,24 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend }) {
} }
return return
}, },
message: function (ws, message) {}, 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])
}
}
else {
console.warn('Unexpected: missing "parse_ws_message" definition')
}
return
},
pong: function (ws, data) { pong: function (ws, data) {
//console.debug(['ws pong', ws.data.uid]) //console.debug(['ws pong', ws.data.uid])
const map_obj = context_meta.ws_map.get(ws.data.uid) const map_obj = context_meta.ws_map.get(ws.data.uid)
@ -184,6 +201,8 @@ const close_active_ws_sessions = function ({ context_meta }) {
const async_run = async function ({ const async_run = async function ({
config, config,
jsbuild_app_frontend, jsbuild_app_frontend,
parse_ws_message,
event_handlers,
}) { }) {
const active_config = { const active_config = {
// defaults // defaults
@ -197,7 +216,7 @@ const async_run = async function ({
const context_meta = { const context_meta = {
ws_map: new Map(), ws_map: new Map(),
} }
const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend }) const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, parse_ws_message, event_handlers })
console.info(server) console.info(server)
process.on('SIGINT', async () => { process.on('SIGINT', async () => {
console.log('SIGINT intercepted') console.log('SIGINT intercepted')

View file

@ -20,16 +20,17 @@ return
class FramerockUtils { class FramerockUtils {
constructor () { constructor () {
this._ws = undefined this._ws = undefined
this.setup_transport = this._setup_transport.bind(this) this.setup_transport = this._setup_transport.bind(this)
this.teardown_transport = this._teardown_transport.bind(this) this.teardown_transport = this._teardown_transport.bind(this)
this.transport_send_bytes = this._transport_send_bytes.bind(this)
return return
} }
_setup_transport ({ on_open, on_close }) { _setup_transport ({ on_open, on_close, on_message }) {
console.info('SETUP TRANSPORT') console.info('SETUP TRANSPORT')
@ -47,8 +48,14 @@ class FramerockUtils {
on_close && on_close(event) on_close && on_close(event)
return return
} }
const func_on_message = (e) => { const func_on_message = (event) => {
console.info('WS MESSAGE') console.info('WS MESSAGE', event)
if (on_message !== undefined) {
on_message(event.data)
}
else {
console.warn(['Unhandled message', event])
}
return return
} }
const func_on_error = () => { const func_on_error = () => {
@ -74,6 +81,16 @@ class FramerockUtils {
return return
} }
_transport_send_bytes (msg_bytes) {
const ws = this._ws
if (ws.readyState === WebSocket.OPEN) {
ws.send(msg_bytes)
}
else {
console.warn('ws readyState !== OPEN : dropping event send')
}
return
}
} }
const FRAMEROCK_UTILS = new FramerockUtils() const FRAMEROCK_UTILS = new FramerockUtils()