简体   繁体   中英

Send data to different websocket endpoints in one connection

i have a question about websockets and sending data to different backend-endpoints in Javascript.

So is it possible to send data to different backend-endpoints in one websocket connection or do i need to establish a new connection for every endpoint?

Lets say:

var ws = new Websocket('ws://localhost:8080');

// send data to /login
ws.send('/login', {logindata...});

// send data to /messages
ws.send('/messages', {data...});

I think what you must do is define a message protocol so the server execute the correct endpoint for each type of message.

For example:

{
   "msgType": "login",
   "msgData": {logindata...}
}

and

{
   "msgType": "messages",
   "msgData": {data...}
}

Those could be you two type of messages you send to the server. And in the backend, have a controller to execute de "login/" endpoint or the "messages/" endpoint.

I don't know if that helps... That's the way I do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM