我刚开始使用 websockets,我正在尝试使用 ws
包执行以下操作:
Client <--> Server X <--> Server Y
另外,我有连接工作:
const WebSocket = require('ws');
//connection between client and server X
const wss = new WebSocket.Server({ port: 5000 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log(message);
});
});
// connection to server Y, but this needs to happen AFTER client sends a message to server X
const ws = new WebSocket(
'ws://someurl/',
[],
{
headers: {
// data from client needs to go here
}
}
);
ws.on('message', function incoming(message) {
const modifiedData = doSomething(message)
// I need to send modifiedData to the client
});
我只是不确定如何将这两件事联系在一起,我想知道这样做的好方法是什么。任何帮助将不胜感激。