如何在客户端获取会话ID? (WebSocket) [英] How to get session id on the client side? (WebSocket)

查看:2509
本文介绍了如何在客户端获取会话ID? (WebSocket)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法吗?

客户端:

function connectWebSocket() {
    var socket = new SockJS('/socket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        console.log("connected");
    });
}

服务器端并不重要.上面的代码执行完后,我需要知道我的会话ID.

Server side is not important. After the code above has been executed I need to know my session id.

推荐答案

要获取会话ID,我们需要对SockJS库进行一些更改.
字符串

To get session id we need to make some changes into SockJS library.
The string

var connid = utils.random_string(8);

用于获取我们的ID.因此,我们只需要像这样完成它:

is used to get our id. So, we need only complete it like this:

var connid = utils.random_string(8);
that.sessionId = connid;

然后我们可以从客户端代码中读取此字段:

and then we can read this field from the client code:

function connectWebSocket() {
    var socket = new SockJS('/socket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        console.log("connected, session id: " + socket.sessionId);
    });
}

如果在调用connect方法之前需要了解会话ID,则可以修改SockJS的构造函数和connect方法以使用客户端传递的值.

And if we need to know session id before calling connect method we can modify SockJS' constructor and connect method to use client-passed value.

这篇关于如何在客户端获取会话ID? (WebSocket)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆