socket.io v. 1.0
nginx v 1.4.6
nodejs v 0.10.32
ubuntu v 14.04
droplet IP (for example): 123.45.678.90
nodejs port listen on: 3001
domain: mydomain.com
Everythigs works correctly both on localhost in WebStorm and on my droplet address like: 123.45.678.90:3001. Problems begin when i try to bind my domain and nginx by the follow config:
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
When i try to connect the mydomain.com from browser, sometimes it works correct, but sometimes i see that message:
{ host: 'mydomain.com',
connection: 'close',
'accept-encoding': 'gzip, deflate',
'user-agent': 'WebIndex',
accept: 'text/html' }
/var/www/bm/socket/index.js:29
var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split
^
TypeError: Cannot call method 'split' of undefined
Here is my socket/index.js, part includes io.use:
io.use(function(socket, next){
async.waterfall([
function(callback){
console.log(socket.request.headers);
var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split('.')[0];
console.log(sid);
sessionStore.load(sid, function(err, session){
if (arguments.length == 0) {
return callback(null, null);
} else {
return callback(null, session)
}
});
},
function(session, callback){
socket.handshake.session = session;
callback(null);
}
], function(){
if (socket.handshake.session) {
return next();
} else {
console.log('error, be careful');
return next(new Error('not authorized'));
}
});
});
What happend? I tried to catch that 'WebIndex' and change part of io.set:
console.log(socket.request.headers);
if (socket.request.headers['user-agent'] == 'WebIndex') {
return callback(null, null);
}
var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split('.')[0];
console.log(sid);
As a result, i got this into my browser on address mydomain.com:
WebSocket connection to 'ws://mydomain.com/socket.io/?EIO=3&transport=websocket&sid=QzYL8Ou8QN556_WEAAAC' failed: Error during WebSocket handshake: Unexpected response code: 400
A few seconds socket.io worked correctly but then i got this 400 error message. Any ideas?