Ответ 1
Насколько я понимаю, вы хотите, чтобы сервер мог отправлять сообщения от клиента 1 к клиенту 2. Вы не можете напрямую соединить два клиента, потому что один из двух концов соединения WebSocket должен быть сервером.
Это какой-то псевдокодийный JavaScript:
Клиент:
var websocket = new WebSocket("server address");
websocket.onmessage = function(str) {
console.log("Someone sent: ", str);
};
// Tell the server this is client 1 (swap for client 2 of course)
websocket.send(JSON.stringify({
id: "client1"
}));
// Tell the server we want to send something to the other client
websocket.send(JSON.stringify({
to: "client2",
data: "foo"
}));
Сервер:
var clients = {};
server.on("data", function(client, str) {
var obj = JSON.parse(str);
if("id" in obj) {
// New client, add it to the id/client object
clients[obj.id] = client;
} else {
// Send data to the client requested
clients[obj.to].send(obj.data);
}
});