26

Usually, we only put the data we want to send as websocket.send() method's parameter, but I want to know whether there are other parameters like IP that we can put inside the brackets. Can we use it this way:

websocket.send(ip, data);  // send data to this ip address

Or I should call other methods?

13
  • 4
    You can't just pass an address because you're required to set up a connection using websocket = new WebSocket(addresss) in the first place. There's only one argument for .send. Commented Jun 24, 2012 at 22:11
  • I already set up the connection. then what should I do if I want to send data to a specific ip address? Then Commented Jun 25, 2012 at 0:40
  • If I create a websockerserver, can this server send data to specific ip address,and what method should I call? Commented Jun 25, 2012 at 0:46
  • A connection is made between two IP addresses. If you want to send data to a specific IP address, first establish a connection to that address. The server doesn't establish the connection, only the client, but if you have many connection open, the serve can choose the one on which it wants to send something. Commented Jun 25, 2012 at 5:58
  • The connection is already bound to a specific address, which you should have passed to the WebSocket constructor. And what do you mean by "server send data to specific ip address"? The server can send data to one of the sockets that connected to it - you probably won't need to code a specific address on the server side. Commented Jun 25, 2012 at 9:26

1 Answer 1

57

As I understand it, you want the server be able to send messages through from client 1 to client 2. You cannot directly connect two clients because one of the two ends of a WebSocket connection needs to be a server.

This is some pseudocodish JavaScript:

Client:

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);
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

var obj = JSON.parse(str); This line will return a "undefined" error, do you know why? I am using chrome 20 beta
@Amy: It was pseudocode; str should refer to the data received from the client. I don't know which library you're using so you'll have to refer to the documentation of it so as to see how to obtain the data.
In the client code I guess you should wait the onopen callback before sending.
what is server object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.