2013-03-21 73 views
2

我試圖讓socket.io與node-http-proxy一起工作,但它總是回退到xhr,websockets將無法工作,雖然我正在連接到node-http-proxy文檔中描述的升級事件。node-http-proxy socket.io websocket連接

var httpProxy = require ("http-proxy"); 

var server = httpProxy.createServer (function (req, res, proxy) { 
    var buffer = httpProxy.buffer (req); 
    proxy.proxyRequest (req, res, { 
     host : "localhost", 
     port : 8001, 
     buffer : buffer 
    }); 
}); 
server.listen (80); 

server.on('upgrade', function (req, socket, head) { 
    server.proxy.proxyWebSocketRequest(req, socket, head, { host : "localhost", port : 8001 }); 
}); 

該應用明顯運行在localhost:8001上,如果我允許所有傳輸方法,它將使用xhrs正常工作。如果我強迫它使用WebSockets Firefox會說

Firefox can't establish a connection to the server at ws://localhost/socket.io/1/websocket/cNp5J80KAWkXqjE6OZOt. @ http://localhost/socket.io/socket.io.js:2371 

就使用默認的方法

httpProxy.createServer (8001, "localhost").listen (80); 

導致同樣的錯誤。

+0

您沒有相同的代碼在所有我已經......你能鏈接在哪裏找到代碼中的文檔?同時你可以看看我的代碼[這個答案](http://stackoverflow.com/questions/15411042/how-to-run-node-js-app-on-port-80-using-http-proxy/15427378#15427378)這對我很有用。如果僅將它與一個域一起使用,則可以刪除switch-statement。 – 2013-03-21 14:22:52

+0

我從這裏得到了代碼:https://github.com/nodejitsu/node-http-proxy#proxying-websockets(我在那裏的緩衝線有一個實驗剩餘的餘量,我現在將其刪除:/) With你的代碼我仍然有同樣的問題。 難道是版本問題?我正在運行node-http-proxy 0.10.0,socket.io 0.9.13和節點0.10.0。 – tom 2013-03-21 17:07:08

回答

4

使用node-http-proxy代理websockets似乎在節點0.10上被破壞。這裏有一個試圖解決它的拉取請求,但看到我的評論:修復也沒有完全工作。

https://github.com/nodejitsu/node-http-proxy/pull/402

+0

Thx David ...我想知道爲什麼我的應用程序無法在新的node.js安裝上工作-.- – ayk 2013-04-20 16:12:29

2

我目前使用的(最新的?V1.0.1)節點的HTTP代理,我發現的WebSockets確係可靠的一個調整後的節點v0.10.20工作(見下文)。默認情況下,node-http-proxy似乎支持xhr-polling,同時刪除websocket連接。

在「examples」,我加入了「WS」標誌真正

var http = require('http'), 
    httpProxy = require('../../lib/http-proxy'); 

httpProxy.createServer({ 
    target:'http://somehost.tld:5000', 
    ws : true //added Websockets Flag TRUE 
}).listen(80);//arbitrary port