2012-05-03 27 views
9

Sto tentando di utilizzare Node.js con Socket.IO per facilitare la messaggistica tra browser e client, dopo the guide.Utilizzo di socket.io con nodejs su un server con apache come proxy inverso

Tuttavia, ho dovuto impostare Node reverse-proxy dietro Apache. Pertanto, anziché example.com:8080 per nodo, sto utilizzando example.com/nodejs/.

Questo sembra causare a Socket.IO di perdere il senso di sé. Ecco il mio nodo app

var io = require('socket.io').listen(8080); 

// this has to be here, otherwise the client tries to 
// send events to example.com/socket.io instead of example.com/nodejs/socket.io 
io.set('resource', '/nodejs/socket.io'); 

io.sockets.on('connection', function (socket) { 

    socket.emit('bar', { one: '1'}); 

    socket.on('foo', function(data) 
    { 
    console.log(data); 
    }); 

}); 

Ed ecco ciò che il mio file del client sembra

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=utf-8 /> 
<title>Socket.IO test</title> 

    <script src="http://example.com/nodejs/socket.io/socket.io.js"></script> 

    <script> 

    var socket = io.connect('http://example.com/nodejs/'); 

    console.log(socket); 

    socket.on('bar', function (data) 
    { 
    console.log(data); 
    socket.emit('foo', {bar:'baz'}); 
    }); 

    socket.emit('foo',{bar:'baz'}); 

    </script> 
</head> 
<body> 
    <p id="hello">Hello World</p> 
</body> 
</html> 

Il problema qui è il riferimento script http://example.com/nodejs/socket.io/socket.io.js. Non restituisce il contenuto previsto di javasscript - invece restituisce "Welcome to socket.io" come se avessi colpito http://example.com/nodejs/.

Qualche idea su come posso farlo funzionare?

+0

So che questo non è correlato alla domanda in questione (e, per favore, chiamami se dovrei aprire una nuova domanda SO), ma sono curioso di sapere come appare il tuo httpd.conf Apache. Ho impostato il proxy per passare al vero server node.js, tuttavia, non riesco a far funzionare il proxy websockets. Potresti per favore dare un esempio di come stai facendo questo con Apache? – pmalbu

+0

Scusa, ma non posso. Questo progetto è stato fatto in un hackathon più di 2 anni fa e il server che è stato creato non esiste più. –

risposta

7

Questo ha finito per essere una soluzione su più fronti.

In primo luogo, sul lato server delle cose, ho dovuto impostare i punti finali come questo

var io = require('socket.io').listen(8080); 

var rootSockets = io.of('/nodejs').on('connection', function(socket) 
{ 
    // stuff 
}); 

var otherSockets = io.of('nodejs/other').on('connection', function(socket) 
{ 
    // stuff 
}); 

Poi, sul lato client, per collegare correttamente aspetto come questo

var socket = io.connect(
    'http://example.com/nodejs/' 
    , {resource: 'nodejs/socket.io'} 
); 

// The usage of .of() is important 
socket.of('/nodejs').on('event', function(){}); 
socket.of('/nodejs/other').on('event', function(){}); 

Dopo questo, tutto ha funzionato. Ricordare che su questo server Apache esegue internamente un comando example.com/nodejs alla porta 8080.

+2

Quale versione di socket.io hai usato? Ho provato la soluzione citata. Ma tutte le intestazioni http vengono rimosse se la richiesta del socket Web viene instradata tramite mod_proxy di apache.Quindi, socket.io chiude la connessione come "connessione websocket non valida". –

4

Non penso che questo abbia nulla a che fare con il vostro proxy Apache, ma alcuni "quirks" con come socket.io gestisce le richieste su una sottodirectory. Vedi la mia risposta qui. NGINX configuration to work with Socket.IO

In sostanza, è necessario utilizzare questa istruzione connect invece:

var socket = io.connect('http://example.com', {resource:'nodejs/socket.io'});

+3

A partire da socket.io 1.0 '{resource: ...' è sostituito da '{percorso: ...' – ceremcem

0

Se qualcuno è interessato, solo questo ha funzionato per me. Sostituire porta 3000 con la porta Nodejs

Apache 2.2.14 all'interno del vostro VirtualHost

<IfModule mod_proxy.c> 
     <Proxy *> 
     Order allow,deny 
     allow from all 
     </Proxy> 
    </IfModule> 

    RewriteEngine on 

    RewriteCond %{QUERY_STRING} transport=polling 
    RewriteRule /(.*)$ http://localhost:3001/$1 [P] 

    ProxyRequests off 
    ProxyPass /socket.io/ ws://localhost:3001/socket.io/ 
    ProxyPassReverse /socket.io/ ws://localhost:3001/socket.io/ 

collegamento lato client:

var myIoSocket = io.connect($location.protocol() + '://' + $location.host(), {path: '/socket.io'}); 

Non c'è bisogno di cambiare nulla sul lato Node.js.

Problemi correlati