连接到websocket时如何添加标题?

时间:2018-10-23 13:15:14

标签: javascript angular websocket spring-websocket stomp

我们使用Spring websocket设置了websocket主题,然后客户端使用Stomp.js进行订阅; 如果直接连接到websocket服务,则可以正常工作;但是现在我们在Websocket服务之前将Kong设置为API网关;连接到它时需要设置标题“ Host:websocket.com”; 但这不适用于Stomp.js;

var url='http://xx.xx.xx.xx:8000/websocket/tracker';
var socket = new SockJS(url);
stompClient = Stomp.over(socket);
var thisheaders={
        Host:'websocket.com'
};
stompClient.connect(thisheaders, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('/topic/greetings', function (greeting) {
        showGreeting(JSON.parse(greeting.body).content);
    });
});

有人知道如何添加标题吗?还是Stomp.js不支持添加标题?

1 个答案:

答案 0 :(得分:0)

@pankaj malik ..试试这个

var url='http://xx.xx.xx.xx:8000/websocket/tracker';
    var socket = new SockJS(url);
    stompClient = Stomp.over(socket);
    var thisheaders={
                    login: 'user',
                    passcode: 'AuWcecmbtSz2',
                    AuthToken: getItem('Authentication')//get your authentication token here
                };
    stompClient.connect(thisheaders, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings', function (greeting) {
            showGreeting(JSON.parse(greeting.body).content);
        });
    });