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

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

programmeradmin2浏览0评论

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);
        });
    });
发布评论

评论列表(0)

  1. 暂无评论