6
votes

i am using a websocket on my site but at random it sends me the following error message:

WebSocket connection to 'ws://client.mydomain.com/socket.io/?EIO=3&transport=websocket&sid=4Kbec5T_XStAC949AACS' failed: Error during WebSocket handshake: Unexpected response code: 500

Here is how i connect to the websocket in my application (note it is an Angular application).

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

Im not quite sure what the problem is but basicly the connection string looks like this:

http://client.mydomain.com

Can anyone tell me whats going on?

1
Status 500 means Internal Error - as some of the request complete successfully and other fail I guess the problem is in the server side processing the request. So check what data you send to the server and how the server process them. - Michael
did you ever find the resolution? - Webezine

1 Answers

0
votes

Can't you try it by adding the rest of the code inside the success callback of ajax? Like this:

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