我正在尝试使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); }
前端
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)
如果我将http://localhost
io.connect url更改为http://127.0.0.1:8000
多次触发文本,并且会得到不同types的错误。
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"}
socket.io握手需要粘性会话。 将socket.io传输限制为仅限Websocket(禁用轮询)或实施粘性会话。