我试图使pm2群集模块与socketio一起工作,但是当我访问/ hello时我遇到错误。 鉴于群集利用了所有CPU核心,socketio变得混乱,所以我安装了socket.io-redis模块来处理这个,但看起来它根本没有做任何与之相关的事情。 Redis(它是空的)或socket.io,我可能错过了一些非常明显的东西。
我使用Nginx作为Web服务器和我的快速服务器,localhost:8000指向localhost:80。
检查:
Nginx工作,http://localhost/hello返回socketio的回复,我在浏览器控制台上看到了。
Redis实例已开启
如果我使用npm start
启动我的服务器而不使用群集,它会起作用,但sudo pm2 start bin/www -i 0
会在问题的底部产生错误。
BACKEND
这是我的nginx.conf
user myusernameishere staff;
worker_processes 1;
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:8000;
}
}
app.js
app.get('/hello', function(req,res) {
res.render('index');
});
io.js
var io = require('socket.io')();
var redis = require('socket.io-redis');
io.adapter(redis({
host: '127.0.0.1',
port: 6379
}));
io.on('connection', function(socket) {
socket.emit('news', {
hello: 'world'
});
socket.on('my other event', function(data) {
console.log(data);
});
});
io.on('error', function() {
console.log("errr");
});
// attach stuff to io
module.exports = io;
bin / www(我只添加" ioe",其他一切都是一样的。)
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var ioe = require('../io');
var debug = require('debug')('test5:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '8000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
ioe.attach(server);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
FRONTEND
index.jade
html
head
title Socket.io
script(src="/socket.io/socket.io.js")
body
script(src="javascripts/ol.js")
ol.js
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', {
my: 'data'
});
});
错误
WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=r57-btYjx30gu9qIAAAA' failed: Error during WebSocket handshake: Unexpected response code: 502
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMof_&sid=r57-btYjx30gu9qIAAAA Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMogS&sid=r57-btYjx30gu9qIAAAA Failed to load resource: the server responded with a status of 400 (Bad Request)
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=sXOD3N5g0MAcJmvUAAAB' failed: Error during WebSocket handshake: Unexpected response code: 502
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMot7&sid=sXOD3N5g0MAcJmvUAAAB Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMotO&sid=sXOD3N5g0MAcJmvUAAAB Failed to load resource: the server responded with a status of 400 (Bad Request)
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=eL4_3l-I0hvuf2WlAAAC' failed: Error during WebSocket handshake: Unexpected response code: 502
socket.io.js:1456 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMp73&sid=eL4_3l-I0hvuf2WlAAAC 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:1456 POST http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMp7N&sid=eL4_3l-I0hvuf2WlAAAC 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doWrite @ socket.io.js:1310(anonymous function) @ socket.io.js:1829(anonymous function) @ socket.io.js:3291proxy @ socket.io.js:2223(anonymous function) @ socket.io.js:3306(anonymous function) @ socket.io.js:3286exports.encodePacket @ socket.io.js:3085encodeOne @ socket.io.js:3285eachWithIndex @ socket.io.js:3304map @ socket.io.js:3311exports.encodePayload @ socket.io.js:3290Polling.write @ socket.io.js:1828close @ socket.io.js:1797Polling.doClose @ socket.io.js:1802Transport.close @ socket.io.js:841Socket.onClose @ socket.io.js:711Socket.onError @ socket.io.js:689(anonymous function) @ socket.io.js:279Emitter.emit @ socket.io.js:2556Transport.onError @ socket.io.js:814(anonymous function) @ socket.io.js:1333Emitter.emit @ socket.io.js:2556Request.onError @ socket.io.js:1502(anonymous function) @ socket.io.js:1449
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=9oiUaLB-1JnMchjqAAAD' failed: Error during WebSocket handshake: Unexpected response code: 502
socket.io.js:1456 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpKB&sid=9oiUaLB-1JnMchjqAAAD 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:1456 POST http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpKW&sid=9oiUaLB-1JnMchjqAAAD 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doWrite @ socket.io.js:1310(anonymous function) @ socket.io.js:1829(anonymous function) @ socket.io.js:3291proxy @ socket.io.js:2223(anonymous function) @ socket.io.js:3306(anonymous function) @ socket.io.js:3286exports.encodePacket @ socket.io.js:3085encodeOne @ socket.io.js:3285eachWithIndex @ socket.io.js:3304map @ socket.io.js:3311exports.encodePayload @ socket.io.js:3290Polling.write @ socket.io.js:1828close @ socket.io.js:1797Polling.doClose @ socket.io.js:1802Transport.close @ socket.io.js:841Socket.onClose @ socket.io.js:711Socket.onError @ socket.io.js:689(anonymous function) @ socket.io.js:279Emitter.emit @ socket.io.js:2556Transport.onError @ socket.io.js:814(anonymous function) @ socket.io.js:1333Emitter.emit @ socket.io.js:2556Request.onError @ socket.io.js:1502(anonymous function) @ socket.io.js:1449
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=Rm2pZbHx6RKHMB6SAAAE' failed: Error during WebSocket handshake: Unexpected response code: 502
socket.io.js:1456 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpTo&sid=Rm2pZbHx6RKHMB6SAAAE 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:1456 POST http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpU7&sid=Rm2pZbHx6RKHMB6SAAAE 400 (Bad Request)
如果我将io.connect网址从http://localhost
更改为http://127.0.0.1:8000
,socketio会多次触发该文本,我会遇到不同类型的错误。
Object {hello: "world"}
socket.io.js:1456 GET http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=LHYM_kn&sid=BXmJUdQy1IkEsURcAAAA 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:2108 WebSocket connection to 'ws://127.0.0.1:8000/socket.io/?EIO=3&transport=websocket&sid=BXmJUdQy1IkEsURcAAAA' failed: WebSocket is closed before the connection is established.
socket.io.js:1456 POST http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=LHYM_lc&sid=BXmJUdQy1IkEsURcAAAA 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doWrite @ socket.io.js:1310(anonymous function) @ socket.io.js:1829(anonymous function) @ socket.io.js:3291proxy @ socket.io.js:2223(anonymous function) @ socket.io.js:3306(anonymous function) @ socket.io.js:3286exports.encodePacket @ socket.io.js:3085encodeOne @ socket.io.js:3285eachWithIndex @ socket.io.js:3304map @ socket.io.js:3311exports.encodePayload @ socket.io.js:3290Polling.write @ socket.io.js:1828close @ socket.io.js:1797Polling.doClose @ socket.io.js:1802Transport.close @ socket.io.js:841Socket.onClose @ socket.io.js:711Socket.onError @ socket.io.js:689(anonymous function) @ socket.io.js:279Emitter.emit @ socket.io.js:2556Transport.onError @ socket.io.js:814(anonymous function) @ socket.io.js:1333Emitter.emit @ socket.io.js:2556Request.onError @ socket.io.js:1502(anonymous function) @ socket.io.js:1449
ol.js:3 Object {hello: "world"}
socket.io.js:1456 GET http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=LHYM_vM&sid=acVsGs32BSeXRB6aAAAA 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:2108 WebSocket connection to 'ws://127.0.0.1:8000/socket.io/?EIO=3&transport=websocket&sid=acVsGs32BSeXRB6aAAAA' failed: WebSocket is closed before the connection is established.
ol.js:3 Object {hello: "world"}
答案 0 :(得分:1)
我遇到了完全相同的问题。
socket.io 现在实际上有一整个部分专门用于在使用多个 worker 时使 socket.io 工作。 https://socket.io/docs/v4/using-multiple-nodes/
最简单和最快的解决方案是禁用 HTTP 长轮询传输,如下所示:
// client side
const socket = io("https://example.com", { transports: ["websocket"] });
另一种解决方案是实现粘性会话并为 socket.io 使用 redis 适配器。您可以在上面的链接中阅读更多相关信息。
希望这对任何人都有帮助:)
答案 1 :(得分:0)
socket.io握手需要粘性会话。将socket.io传输仅限于websockets(禁用轮询)或实现粘性会话。