I use socket.io to make a chat room in my website, there is no problem when I run it in my computer, but when I run it on the production server, the socket.io.js output the following error:
WebSocket connection to 'ws://myWebsite.com/socket.io/?EIO=3&transport=websocket&sid=1qtzy4QRde6G-PHkAAAU' failed: Error during WebSocket handshake: Unexpected response code: 400
I am using socket.io@1.4.5 for server and client
The website is listening on port 8080 behind nginx with port 80, everything works fine except the error message, but the chat room is still functional, that is very strange.
I have searched some similar issues on Stack Overflow, and the suggested solutions are already my nginx settings (/etc/nginx/sites-enabled/myWebsite.com):
server {
listen 80;
server_name myWebsite.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
Meteor WebSocket handshake error 400 with nginx Meteor WebSocket connection to 'ws://.../websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
Although the chat room still works, I still want to get rid of the error message
For your reference, below is the socket.io related code:
// I use angular.js as the front-end framework
function Socket (socketFactory, $rootScope) {
var ioSocket = io.connect(window.location.host);
var socket = socketFactory({ioSocket : ioSocket});
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
});
}
};
}
Socket.$inject = ['socketFactory', '$rootScope'];
angular.module('core').factory('Socket', Socket);
Socket.on('new message', function (data) { ... });