【问题标题】:how to add Headers when connect to websocket?连接到 websocket 时如何添加标头?
【发布时间】:2019-03-27 18:02:19
【问题描述】:

我们用 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 只是不支持添加标题?

【问题讨论】:

    标签: javascript angular websocket spring-websocket stomp


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2015-07-29
    • 2018-05-05
    • 1970-01-01
    • 2020-02-14
    • 2019-05-17
    • 2020-02-11
    相关资源
    最近更新 更多