package.json
{
"name": "ws-seq-para",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ws": "^7.2.3"
}
}
server.js
const WebSocket = require('ws')
const wss = new WebSocket.Server({ port: 2999 })
wss.on('connection', function connection(ws) {
ws.send(JSON.stringify({ type: 'long' }))
setTimeout(() => {
ws.send(JSON.stringify({ type: 'short' }))
}, 1000)
})
client.js
const WebSocket = require('ws')
const ws = new WebSocket('ws://localhost:2999')
ws.on('message', function incoming(json) {
const { type } = JSON.parse(json)
switch (type) {
case 'long':
console.log('long start')
setTimeout(console.log, 2000, 'long stop')
break
case 'short':
console.log('short running')
break
default:
break
}
})
вывод консоли:
long start
short running
long stop
резюме: нужен queue... воркер не решает эту проблему...