当 websocketws.send(msg)
和ws.on('message', ...)
. 我在下面的伪代码片段中描述了我的要求。
ws.on('message', (data) => {
let msg = JSON.parse(data)
if (msg.MsgType == 'DummyResponse') {
// we got one response and we need to send it to the
// corresponding RequestID's getResponse() function
requestMap.get(msg.RequestID).unblockingGetResponse(msg)
}
}
async ctx => {
ctx.body = await ws.send({MsgType:'DummyRequest', RequestID: uuid()}).getResponse()
}
我的 nodejs package.json 依赖
"dependencies": {
"koa": "^2.2.0",
"koa-router": "^7.1.1",
"uuid": "^3.0.1",
"ws": "^3.0.0"
}
我的问题是如何在 es6/7 中正确实现这一点?