2016-04-29 2 views
3

Я пытаюсь работать с кластерным модулем pm2 с socketio, но я получаю ошибки при посещении/привет. Учитывая, что кластер использует все ядра ЦП, сокетка запутывается, поэтому я установил модуль socket.io-redis, чтобы позаботиться об этом, но похоже, что он ничего не делает, связанный с Redis (он пуст) или сокет .io, я, вероятно, пропустил что-то очень очевидное.Использование кластерного модуля pm2 с socket.io и socket.io-redis

Я использую Nginx в качестве веб-сервера и моего экспресс-сервера, 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; 

бен/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) 

Если я изменю URL-адрес 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

Вы решили это? – lucaswxp

ответ

0

Рукопожатие socket.io требует липких сеансов. Ограничьте подключение socket.io только к веб-окнам (отключите опрос) или выполните липкие сеансы.

Смежные вопросы