我在我的网站上使用一个websocket,但它随机向我发送以下错误消息:
到"ws://client.mydomain.com/socket.io/"的WebSocket连接?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
有人能告诉我发生了什么事吗?
您不能通过在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;
}
});