WebSocket握手期间出错:意外的响应代码:500

时间:2015-11-02 11:54:00

标签: javascript angularjs node.js websocket socket.io

我在我的网站上使用websocket,但随机它会向我发送以下错误消息:

  

WebSocket连接到'ws://client.mydomain.com/socket.io/?EIO = 3& transport = websocket& sid = 4Kbec5T_XStAC949AACS'失败:WebSocket握手期间出错:意外响应代码:500

以下是我在应用程序中连接websocket的方法(注意它是一个Angular应用程序。)

    app.factory('clientSocket', ['$sessionStorage', 'socketFactory', function ($sessionStorage, socketFactory) {
    var connection = '';
    $.ajax({
        url: 'config.json',
        async: false,
        dataType: 'json',
        success: function (response) {
            connection = response.app_url;
        }
    });
    var myIoSocket = io.connect(connection);

    var clientSocket = socketFactory({
        ioSocket: myIoSocket
    });
    clientSocket.forward('someEvent');

    clientSocket.on('connect', function () {
        if ($sessionStorage.activeUser != null) {
            clientSocket.emit('userData', $sessionStorage.activeUser.user);
        }
    });


    return clientSocket;
}]);

我不太确定问题是什么,但基本上连接字符串看起来像这样:

http://client.mydomain.com

谁能告诉我发生了什么?

1 个答案:

答案 0 :(得分:0)

你不能通过在ajax的成功回调中添加其余代码来尝试吗?像这样:

app.factory('clientSocket', ['$sessionStorage', 'socketFactory', function ($sessionStorage, socketFactory) {
var connection = '';
$.ajax({
    url: 'config.json',
    async: false,
    dataType: 'json',
    success: function (response) {
        connection = response.app_url;
        var myIoSocket = io.connect(connection);

        var clientSocket = socketFactory({
    ioSocket: myIoSocket
});
clientSocket.forward('someEvent');

clientSocket.on('connect', function () {
    if ($sessionStorage.activeUser != null) {
        clientSocket.emit('userData', $sessionStorage.activeUser.user);
    }
});


return clientSocket;
    }
});