Come ho capito, si desidera che il server sia in grado di inviare messaggi dal client 1 al client 2. Non è possibile connettere direttamente due client perché una delle due estremità di una connessione WebSocket deve essere un server.
Questo è un po 'pseudocodish JavaScript:
Cliente:
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"
}));
Server:
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);
}
});
Non si può semplicemente passare un indirizzo perché ti viene richiesto di impostare una connessione utilizzando 'websocket = new WebSocket (indirizzi)' in primo luogo. C'è solo [un argomento] (http://dev.w3.org/html5/websockets/#dom-websocket-send) per '.send'. – pimvdb
Ho già configurato la connessione. allora cosa dovrei fare se voglio inviare dati ad uno specifico indirizzo IP? Quindi – Amy
Se creo un server websocker, questo server può inviare dati a indirizzi IP specifici e quale metodo devo chiamare? – Amy