27

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?

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Ivy
  • 503
  • 1
  • 10
  • 23
  • 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](http://dev.w3.org/html5/websockets/#dom-websocket-send) for `.send`. – pimvdb Jun 24 '12 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 – Ivy Jun 25 '12 at 00:40
  • If I create a websockerserver, can this server send data to specific ip address,and what method should I call? – Ivy Jun 25 '12 at 00: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. – Denys Séguret Jun 25 '12 at 05: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. – pimvdb Jun 25 '12 at 09:26
  • @dystroy I have many clients that are already connected to the server, and I want to establish one connection that connects two clients, how can I do this? how can the server choose which one it wants to send data? – Ivy Jun 25 '12 at 12:11
  • In my programs, in Go, I simply have a map idclient->websocket and I have a router dispatching the messages. – Denys Séguret Jun 25 '12 at 12:12
  • @pimvdb My clients are already connected to the server, the connection between two clients is not established yet. what I want to do is to establish a specific connection between clients,so I need to send a the connection message to a specific IP address. How can the server send data to one socket? – Ivy Jun 25 '12 at 12:15
  • @dystroy can you give me a small sample code ? Thank you soooo much – Ivy Jun 25 '12 at 12:17
  • I don't think you can identify a connection by an address. There can be multiple clients connecting to your server from the same IP. Anyway, are you sure you want a client to select another client *by address*? It would make more sense to have each client have a nickname/ID. – pimvdb Jun 25 '12 at 12:40
  • @pimvdb you are right! I must assign each client a ID, but I still don't know how to establish the connection between two clients. I mean, server will broadcast the client_list to every online client and then each client can choose whom he wants to connect. – Ivy Jun 25 '12 at 13:00
  • the clients make connections with the server, and the server routes the data as you program it. yes, you need some way to track the users and who gets what. –  Jul 04 '13 at 07:28
  • @pimvdb in 2019, it's actually possible to make a client to client connection using webRCT – Ferrybig Jul 12 '19 at 06:36

1 Answers1

55

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);
  }
});
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
pimvdb
  • 151,816
  • 78
  • 307
  • 352