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
Just something to try. Show implementation of Class Socket() as a worker-thread.
I don't see how this connects to a wss instance, but I might later 😄
socket-worker.js
// socket-worker.js'use strict'const{ Worker }=require('worker_threads')constpath=require('path')classSocket{constructor(url){this._worker=newWorker(path.join(__dirname,'socket-worker.js'),{workerData: {url: url.href,},}).on('error',(err)=>{this.onerror?.(err)}).on('exit',()=>{this.readyState=this.CLOSED}).on('message',({ event, data })=>{if(event==='open'){this.readyState=this.OPENthis.onopen?.()}elseif(event==='error'){this.readyState=this.CLOSINGthis.onerror?.(data)}elseif(event==='close'){this.readyState=this.CLOSEDthis.onclose?.()}elseif(event==='data'){this.onmessage?.({data: Buffer.from(data)})}})this.CONNECTING='CONNECTING'this.OPEN='OPEN'this.CLOSING='CLOSING'this.CLOSED='CLOSED'this.onopen=nullthis.onerror=nullthis.onclose=nullthis.onmessage=nullthis.readyState=this.CONNECTING}send(data){// TODO (perf): Transfer Buffer?this._worker.postMessage(data)}close(){this.readyState=this.CLOSINGthis._worker.terminate()}}module.exports=Socket
socket.js
// socket.js'use strict'const{ Worker }=require('worker_threads')constpath=require('path')classSocket{constructor(url){this._worker=newWorker(path.join(__dirname,'socket-worker.js'),{workerData: {url: url.href,},}).on('error',(err)=>{this.onerror?.(err)}).on('message',({ event, data })=>{if(event==='open'){this.readyState=this.OPENthis.onopen?.()}elseif(event==='error'){this.readyState=nullthis.onerror?.(data)}elseif(event==='close'){this.readyState=nullthis.onclose?.()}elseif(event==='data'){this.onmessage?.({data: Buffer.from(data)})}})this.OPEN='OPEN'this.onopen=nullthis.onerror=nullthis.onclose=nullthis.onmessage=nullthis.readyState=null}send(data){// TODO (perf): Transfer Buffer?this._worker.postMessage(data)}close(){this._worker.terminate()}}module.exports=Socket
This discussion was converted from issue #6 on March 22, 2022 00:39.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
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
-
Just something to try. Show implementation of
Class Socket()
as aworker-thread
.I don't see how this connects to a
wss
instance, but I might later 😄socket-worker.js
socket.js
Beta Was this translation helpful? Give feedback.
All reactions