如果你試圖做的是採取所有傳入的WebSocket消息到達端口9000,並送他們出去端口9000(轉發它們),那麼你可以做的是這樣的:
const WebSocket = require('ws');
// create webSocket server for incoming connections on port 9000
const wServer = new WebSocket.Server({port: 9000});
const wClient = new WebSocket('ws://www.somehost.com', {});
wClient.on('open',() => {
console.log("connected to other client");
});
wClient.on('error, err => {
console.log("client error", err);
});
// server event handlers
wServer.on('listening',() => {
console.log("server started");
});
wServer.on('connection', ws => {
// forward all incoming messages to the other server
ws.on('message', msg => {
wClient.send(msg);
});
});
wSserver.on('error', err => {
console.log("server error", err);
});
是這些普通的TCP套接字? WebSockets的?什麼是套接字協議? – jfriend00
Websockets是協議。 –