Multiple Websockets

前端 未结 3 1136
误落风尘
误落风尘 2020-12-15 06:12

I\'m trying to use two websockets on one page. This is my code:

var pageViewWs = new WebSocket(\"ws://localhost:9002/pageView\");
var sessionWs = new WebSoc         


        
相关标签:
3条回答
  • 2020-12-15 06:41

    I faced the same problem to run multiple services through the same port. So, I created a PHP library to do this.

    Why ?

    Some free plans of hosting providers don't allow you to bind to ports or allow you to bind to one port. In case of OpenShift Cloud Server, you can only bind to port 8080. So, running multiple WebSocket services is not possible. In this case, Francium DiffSocket is useful.

    You can run different services on the same port using a PHP library called Francium DiffSocket.

    After setting up Francium DiffSocket, you can do this for using different services :

    var chatWS = new WebSocket("ws://ws.example.com:8000/?service=chat");
    var gameWS = new WebSocket("ws://ws.example.com:8000/?service=game");
    

    An example are these services which are running through a single port :

    • Finding Value Of Pi
    • Advanced Live Group Chat With PHP, jQuery & WebSocket
    • Live Group Chat With PHP, jQuery & WebSocket
    0 讨论(0)
  • 2020-12-15 06:42

    Apart from the HTTP Request head both the request are the same. They hit the same application server on the same port. It is up to the server side application to treat each connection differently based on the HTTP request that initiated it.

    I've done this in node. You could do it manually but packages like

    • espress-ws
    • or express-ws-routes

    eases the process.

    0 讨论(0)
  • 2020-12-15 06:43

    I believe you can only create one WebSocket connection from a client to a specific port on the host. Have you tried either running the two services on different ports, or on different servers? This would allow you to determine the limitation...

    0 讨论(0)
提交回复
热议问题