2013-02-25 19 views
6

Ho scritto proxy http in node.js in esecuzione sulla porta 80. Tutto ciò di cui ho bisogno è di reindirizzare il traffico socket.io alla porta 9090 e il traffico http standard ad Apache su 8080. Questo è il mio codice proxy:node.js: proxy websockets ad altra porta

httpProxy = require('http-proxy'); 

httpProxy.createServer(function (req, res, proxy) { 
    if (req.url.match(/socket.io/)) { 
     proxy.proxyRequest(req, res, { 
      host: 'localhost', 
      port: 9090 
     }); 
    } else { 
     proxy.proxyRequest(req, res, { 
      host: 'localhost', 
      port: 8080 
     }); 
    } 
}).listen(80); 

Tutto funziona, ma io.socket torna al xhr-polling.

http://localhost/client.htm - falls back to xhr-polling 

file:///C:/.../client9090.htm - still uses websocket 

socket.io applicazione è in esecuzione sulla porta 9090, Client.htm collega a 80 e client9090.htm si collega direttamente al 9090.

Sembra un nodo-http-proxy rende app socket.io lavorare in modalità xhr-polling. Cliente è Chrome V.25

codice socket.io app

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

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

     socket.on('hi!', function (data) { 
      console.log(data); 
      socket.emit('news'); 
     }); 

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

codice Client.htm

<script src="http://localhost/socket.io/socket.io.js"></script> 
<script> 
    var chat = io.connect('http://localhost') 

    chat.on('connect', function() { 
     chat.emit('hi!'); 
    }); 

    chat.on('news', function() { 
     chat.emit('ahoj',{a:1,b:2}); 
    }); 
</script> 

client9090.htm è lo stesso ma localhost è sostituito dal localhost: 9090

Come ho detto, everythig funziona bene, solo il problema è che il nodo-http-proxy fa cadere da websockets a xhr-polling. Qualcuno può aiutare?

risposta

2

Secondo https://npmjs.org/package/http-proxy, quando si aggiunge un richiamo alla httpProxy.createServer(), è necessario manualmente delega 'aggiornamento' eventi, in modo da qualcosa di simile:

httpProxy = require('http-proxy'); 

// added `var server =` here 
var server = httpProxy.createServer(function (req, res, proxy) { 
    if (req.url.match(/socket.io/)) { 
     proxy.proxyRequest(req, res, { 
      host: 'localhost', 
      port: 9090 
     }); 
    } else { 
     proxy.proxyRequest(req, res, { 
      host: 'localhost', 
      port: 8080 
     }); 
    } 
}).listen(80); 

// added upgrade listener section here: 
server.on('upgrade', function (req, socket, head) { 
    server.proxy.proxyWebSocketRequest(req, socket, head); 
}); 

Tuttavia, per l'uso che avete descritto sopra , non hai nemmeno bisogno della funzione di callback - potresti anche fare qualcosa del genere:

httpProxy = require('http-proxy'); 

var options = { 
    pathnameOnly: true, 
    router: { 
    '/wiki': '127.0.0.1:8001', 
    '/blog': '127.0.0.1:8002', 
    '/api': '127.0.0.1:8003' 
    } 
} 

var proxyServer = httpProxy.createServer(options); 
proxyServer.listen(80); 
Problemi correlati