added send and receive events for websocket transport
This commit is contained in:
parent
605d72f9b0
commit
47507e80ae
2 changed files with 47 additions and 11 deletions
|
@ -27,7 +27,7 @@ const get_siteroot_html = function ({ page_title }) {return `
|
|||
</html>
|
||||
`.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 map_obj = {
|
||||
ws,
|
||||
|
@ -144,7 +144,24 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend }) {
|
|||
}
|
||||
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) {
|
||||
//console.debug(['ws pong', 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 ({
|
||||
config,
|
||||
jsbuild_app_frontend,
|
||||
parse_ws_message,
|
||||
event_handlers,
|
||||
}) {
|
||||
const active_config = {
|
||||
// defaults
|
||||
|
@ -197,7 +216,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 })
|
||||
const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, parse_ws_message, event_handlers })
|
||||
console.info(server)
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('SIGINT intercepted')
|
||||
|
|
|
@ -20,16 +20,17 @@ return
|
|||
|
||||
class FramerockUtils {
|
||||
constructor () {
|
||||
|
||||
|
||||
this._ws = undefined
|
||||
|
||||
this.setup_transport = this._setup_transport.bind(this)
|
||||
this.teardown_transport = this._teardown_transport.bind(this)
|
||||
|
||||
|
||||
this.setup_transport = this._setup_transport.bind(this)
|
||||
this.teardown_transport = this._teardown_transport.bind(this)
|
||||
this.transport_send_bytes = this._transport_send_bytes.bind(this)
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
_setup_transport ({ on_open, on_close }) {
|
||||
_setup_transport ({ on_open, on_close, on_message }) {
|
||||
|
||||
console.info('SETUP TRANSPORT')
|
||||
|
||||
|
@ -47,8 +48,14 @@ class FramerockUtils {
|
|||
on_close && on_close(event)
|
||||
return
|
||||
}
|
||||
const func_on_message = (e) => {
|
||||
console.info('WS MESSAGE')
|
||||
const func_on_message = (event) => {
|
||||
console.info('WS MESSAGE', event)
|
||||
if (on_message !== undefined) {
|
||||
on_message(event.data)
|
||||
}
|
||||
else {
|
||||
console.warn(['Unhandled message', event])
|
||||
}
|
||||
return
|
||||
}
|
||||
const func_on_error = () => {
|
||||
|
@ -74,6 +81,16 @@ class FramerockUtils {
|
|||
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()
|
||||
|
|
Loading…
Add table
Reference in a new issue