最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - websocket reconnect using session id - Stack Overflow

programmeradmin1浏览0评论

I'm using Java and Websocket API for my application

This is the problem I face :

I open a websocket connection to connect through certain server :

ws = new WebSocket(targetURL);

When I send every message using ws, I always check the state of the ws first, by :

if(ws.readyState == 1) {
        ws.send(request);
} else {
        alert("THE CONNECTION IS CLOSED. PLEASE REFRESH YOUR BROWSER.");
}

I saved an information in my EndPoint during the munication between client and server. But sometimes, when I want to send message from client, the state is not 1, which means that the ws is no longer open (CMIIW). If I try to connect again using ws = new WebSocket(targetURL), it will create a new connection, thus the information in my EndPoint bee lost.

So my question is : Is there any way to reconnect a closed connection of Websocket using Session ID or any other unique ID so that the information in my EndPoint can be preserved?

Thank you

I'm using Java and Websocket API for my application

This is the problem I face :

I open a websocket connection to connect through certain server :

ws = new WebSocket(targetURL);

When I send every message using ws, I always check the state of the ws first, by :

if(ws.readyState == 1) {
        ws.send(request);
} else {
        alert("THE CONNECTION IS CLOSED. PLEASE REFRESH YOUR BROWSER.");
}

I saved an information in my EndPoint during the munication between client and server. But sometimes, when I want to send message from client, the state is not 1, which means that the ws is no longer open (CMIIW). If I try to connect again using ws = new WebSocket(targetURL), it will create a new connection, thus the information in my EndPoint bee lost.

So my question is : Is there any way to reconnect a closed connection of Websocket using Session ID or any other unique ID so that the information in my EndPoint can be preserved?

Thank you

Share Improve this question edited Apr 11, 2014 at 10:49 Pavel Bucek 5,32429 silver badges44 bronze badges asked Apr 11, 2014 at 3:32 xemjasxemjas 1351 gold badge4 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

WebSockets use a HTTP handshake, and it will send whatever cookie they have for that origin. So a way of doing it, is assign a cookie to you clients, and they will forward it on connection. You could even set the cookie in the handshake response, if the framework you are using it allows it. You can use that cookie on connection to figure out the ID or assign a new ID if no cookie.

Other option is to create for example your own "hello" protocol, where the browser has to send a special mand before start, indicating its ID if any, or just an null ID if it is first time.

But aside of the cookie, there is not built-in mechanism for that.

发布评论

评论列表(0)

  1. 暂无评论