WebSocket request-response subprotocol

后端 未结 7 520
走了就别回头了
走了就别回头了 2020-12-22 23:47

WebSocket provides a bi-directional communication like a human being talks. The client can send data to the server and the server can send data to the client anytime. But wh

7条回答
  •  生来不讨喜
    2020-12-23 00:06

    (send a id with the request and wait for a reponse with the same id until a timeout period)

    I created a lib that does exactly that, called WebSocketR2 (where R2 means Request Response): https://github.com/ModernEdgeSoftware/WebSocketR2

    It also handles reconnecting to the server if a connection is lost, which could be helpful if you are doing web sockets through a load balancer.

    The final result is you can implement callbacks on the web socket send function like this:

    var request = {
        action: "login",
        params: {
            username: "test",
            password: "password"
        }
    };
    
    ws.send(request, function(response){
        console.log(response)
    });
    

提交回复
热议问题