javascript - how to add Headers when connect to websocket? - Stack Overflow

we set up websocket topic with Spring websocket, and then the client side is using Stomp.js to subscrib

we set up websocket topic with Spring websocket, and then the client side is using Stomp.js to subscribe it; it works fine if connect to the websocket service directly; but now we set up Kong as the API Gateway in front of the websocket service; it needs to set header "Host: websocket" when connect to it; But it doesn't work with Stomp.js;

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

Does anyone know how to add the headers for it? Or the Stomp.js just doesn't support to add headers?

we set up websocket topic with Spring websocket, and then the client side is using Stomp.js to subscribe it; it works fine if connect to the websocket service directly; but now we set up Kong as the API Gateway in front of the websocket service; it needs to set header "Host: websocket." when connect to it; But it doesn't work with 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.'
};
stompClient.connect(thisheaders, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('/topic/greetings', function (greeting) {
        showGreeting(JSON.parse(greeting.body).content);
    });
});

Does anyone know how to add the headers for it? Or the Stomp.js just doesn't support to add headers?

Share Improve this question asked Oct 23, 2018 at 13:15 pankaj malikpankaj malik 1071 gold badge2 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

@pankaj malik.. try this

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);
        });
    });

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

相关推荐

  • javascript - how to add Headers when connect to websocket? - Stack Overflow

    we set up websocket topic with Spring websocket, and then the client side is using Stomp.js to subscrib

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信