You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Server side WebSockets in Deno are implemented in such a way that the ping interval/timeout (via ´Deno.upgradeWebSocket´'s idleTimeout option) is reset whenever messages are received. Makes perfect sense, but many other WebSocket frameworks use a strict interval at which pings are sent no matter what.
Anyway, I have a server which accepts WebSocket connections to clients that "expect" a ping every ~30 seconds, or else they disconnect. I came across the ability to access internals via Deno[Deno.internal], and wrote up this hack:
/** * Here be dragons. * * The use of Deno's internals is highly discouraged, * as they are subject to rapid change. */// @ts-ignore: I know what I'm doingif(Deno.internal===undefined){thrownewError("'Deno.internal' is undefined");}// @ts-ignore: I know what I'm doingif(Deno[Deno.internal]===undefined){thrownewError("'Deno[Deno.internal]' is undefined");}// @ts-ignore: I know what I'm doingconst{ core }=Deno[Deno.internal];if(core===undefined){thrownewError("'Deno[Deno.internal].core' is undefined");}const{ ops }=core;if(ops===undefined){thrownewError("'Deno[Deno.internal].core.ops' is undefined");}const{ op_ws_send_ping }=ops;if(op_ws_send_ping===undefined){thrownewError("'Deno[Deno.internal].core.ops.op_ws_send_ping' is undefined");}if(typeofop_ws_send_ping!=="function"){thrownewError("'Deno[Deno.internal].core.ops.op_ws_send_ping' is not a function");}constcache=newWeakMap<WebSocket,number>();exportconstextactRid=(socket: WebSocket)=>{constcached=cache.get(socket);if(cached!==undefined){returncached;}constsymbols=Object.getOwnPropertySymbols(socket);constsymbol=symbols.find(symbol=>symbol.description==="[[rid]]");if(symbol===undefined){thrownewError("Unable to extract socket's RID");}// @ts-ignore: I know what I'm doingconstrid=socket[symbol];if(typeofrid!=="number"){thrownewError("Socket's RID is not a number");}cache.set(socket,rid);returnrid;};exportconstping=(socket: WebSocket)=>{try{op_ws_send_ping(extactRid(socket));}catch(e){console.error("Error sending ping:");console.error(e);}};
However, it seems that since version 1.40, the op_ws_send_ping is no longer available here. Is there still some way to access the internal op_ws_send_ping? Or some other way to send a ping?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Server side WebSockets in Deno are implemented in such a way that the ping interval/timeout (via ´Deno.upgradeWebSocket´'s
idleTimeout
option) is reset whenever messages are received. Makes perfect sense, but many other WebSocket frameworks use a strict interval at which pings are sent no matter what.Anyway, I have a server which accepts WebSocket connections to clients that "expect" a ping every ~30 seconds, or else they disconnect. I came across the ability to access internals via
Deno[Deno.internal]
, and wrote up this hack:However, it seems that since version 1.40, the
op_ws_send_ping
is no longer available here. Is there still some way to access the internalop_ws_send_ping
? Or some other way to send a ping?Beta Was this translation helpful? Give feedback.
All reactions