javascript - websocket reconnect using session id - Stack Overflow

I'm using Java and Websocket API for my applicationThis is the problem I face :I open a websocket

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744708302a4589187.html

相关推荐

  • javascript - websocket reconnect using session id - Stack Overflow

    I'm using Java and Websocket API for my applicationThis is the problem I face :I open a websocket

    5天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信