当我尝试将我的系统连接到nodeJs套接字(socket.io)时,我收到错误消息
WebSocket connection to 'ws://mysite.co/socket.io/?EIO=3&transport=websocket&sid=SkcmTj6P8_qAL_mSALBN' failed: Error during WebSocket handshake: Unexpected response code: 400
配置如下: nginx- v1.8.0
upstream node {
ip_hash;
server localhost:3001;
server localhost:3002;
server localhost:3003;
server localhost:3004;
server localhost:3005;
}
server {
listen 80;
server_name www.mysite.co
root /var/www/mysite/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri @rewriteapp;
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(app|idan_dev)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index app.php;
fastcgi_send_timeout 30;
fastcgi_read_timeout 30;
fastcgi_pass 127.0.0.1:9000;
}
# Statics
location /(bundles|media) {
expires 1d;
try_files $uri @rewriteapp;
}
error_log /var/log/nginx-error.log;
access_log /var/log/nginx-access.log;
location ~ /socket.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://node;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 150;
access_log off;
}
}
在nodejs部分我批准了任何一个(现在,为了测试这个特定的问题)
var app = require('express')();
var bodyParser = require('body-parser');
var redis = require('redis').createClient();
var server = require('http').createServer();
var io = require('socket.io').listen(server);
var sockets = [];
var request = require('request');
// these statements config express to use these modules, and only need to be run once
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/data', function(req, res) {
// do some stuff with data sent directly to node.
});
app.listen(parseInt( process.env.port ) + 1000); // set listening a port for port for the balancer.
module.exports = app;
// run HTTP server on this port
console.log('listening to port ' + ( parseInt( process.env.port ) - 3000 ));
server.listen(process.env.port); // setting multiple ports for same file
// allow any user to authenticate.
io.set('authorization', function(handshake, callback)
{
return callback(null, true);
});
io.sockets.on('connection', function(socket)
{
var id = getUserId(socket);
console.log('on connection id - ' + id);
if( id === false) return;
sockets[id] = socket;
socket.on('disconnect', function()
{
var index = sockets.indexOf(socket);
if (index != -1) {
sockets.splice(index, 1);
}
});
});
function getUserId(socket) {
// getting the id of the user..
return '1';
}
我不知道它是否相关但网站本身是基于php的,只有套接字系统是基于nodejs的。 我对nodejs和socket.io非常好,所以如果有任何数据丢失,请告诉我,我会添加它。