Can you connect client to client via web sockets w

2019-03-31 11:30发布

问题:

I don't quite know how to search this in google:

  • "client to client websocket connections"
  • "browser to browser websockets"
  • "websockets without a server"

Haha, is there a way for someone on a webpage in the browser to communicate directly to another person on a web page in the browser, without touching the server?

I am very familiar with socket.io, but that requires all clients emit messages to the server, which can broadcast them to the other connected clients. I am not familiar with the details of web sockets though, so maybe there's a way to communicate without sending messages through the server.

Is this possible? I just want to know the scope of web socket functionality, the limits you can take them too, etc.

回答1:

This is not possible, you have to have the server in the middle.



回答2:

Not Web Sockets, but four years later and now we've got browser-to-browser communication!
http://www.webrtc.org/

There are JS libraries built around it to make it easier (e.g. https://simplewebrtc.com/). However, it does still require a server to orchestrate connections.

I know this question is ancient, but it showed up in Google when I searched so it likely will for others!



回答3:

For an application to accept connections, it has to have a server port open and listening for incoming requests. You cannot have a server socket exposed from a browser. I dont know if you can expose a server socket from within an applet. But even if you could, you would need to know the IP address of the other client for establishing a peer to peer connection.



回答4:

Well, technically when you broadcast, the client emits to the server, the server broadcasts to everyone. I don't think with the current architecture of the web peer to peer connections like this is possible.

But it is possible that a client send a message to server specifying another client ID, and the server sending it to the other clients using sessions.



回答5:

The moment you have a client listening for websockets (which you have to do in other to communicate), it becomes a server.