refactor to make transport send/receive more universal (defer to apps to handle)
This commit is contained in:
parent
47507e80ae
commit
8f44f6b508
2 changed files with 18 additions and 15 deletions
|
@ -27,7 +27,17 @@ const get_siteroot_html = function ({ page_title }) {return `
|
||||||
</html>
|
</html>
|
||||||
`.trim()}
|
`.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 make_session_object = (ws) => {
|
||||||
const map_obj = {
|
const map_obj = {
|
||||||
ws,
|
ws,
|
||||||
|
@ -147,18 +157,12 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend, par
|
||||||
message: function (ws, message) {
|
message: function (ws, message) {
|
||||||
const m_bytes = Buffer.byteLength(message)
|
const m_bytes = Buffer.byteLength(message)
|
||||||
console.info(['ws message', ws.data.uid, { size: m_bytes }])
|
console.info(['ws message', ws.data.uid, { size: m_bytes }])
|
||||||
if (parse_ws_message !== undefined) {
|
if (handle_transport_bytes !== undefined) {
|
||||||
const [ evt_type, evt_data ] = parse_ws_message(message)
|
const transport_send_bytes = wrap_transport_sendbytes.bind(null, ws)
|
||||||
const func_handle = event_handlers[evt_type]
|
handle_transport_bytes({ transport_send_bytes }, message)
|
||||||
if (func_handle !== undefined) {
|
|
||||||
func_handle(ws, evt_data)
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.warn(['no handler defined for evt_type', evt_type])
|
console.warn('Unexpected: "handle_transport_bytes" not defined')
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.warn('Unexpected: missing "parse_ws_message" definition')
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
|
@ -201,8 +205,7 @@ 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,
|
handle_transport_bytes,
|
||||||
event_handlers,
|
|
||||||
}) {
|
}) {
|
||||||
const active_config = {
|
const active_config = {
|
||||||
// defaults
|
// defaults
|
||||||
|
@ -216,7 +219,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, parse_ws_message, event_handlers })
|
const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, handle_transport_bytes })
|
||||||
console.info(server)
|
console.info(server)
|
||||||
process.on('SIGINT', async () => {
|
process.on('SIGINT', async () => {
|
||||||
console.log('SIGINT intercepted')
|
console.log('SIGINT intercepted')
|
||||||
|
|
|
@ -87,7 +87,7 @@ class FramerockUtils {
|
||||||
ws.send(msg_bytes)
|
ws.send(msg_bytes)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.warn('ws readyState !== OPEN : dropping event send')
|
console.warn('ws readyState !== OPEN : dropping send')
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue