I was building a server, and while trying to implement the websocket protocol, I ran into some problem.
As the question title describes, let's suppose I have defined two routes (/ws1
, /ws2
), which exposes multiple websocket connection.
And in case of a handshake I successfully can understand for which route the handshake request was sent.
The main problem in hand is that, when a subsequent websocket message is sent by the client, how is the server going to understand that which endpoint the websocket message was sent to.
After reading this: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers I understand that there is no such field in message which denotes the route.
Just in case: I am doing this in PHP.
A WebSocket connection is established using an HTTP GET request that upgrades the connection. You can identify clients based on the resource ID in PHP by casting the resource to an integer using (int) $resource
.
TCP connections in general are identified by the source IP / source port / destination IP / destination port quadruple.
You have to keep the URI / endpoint info in an array or similar data structure and use the client ID as index. Then you can look up the endpoint upon receiving new messages.