2013-05-07 89 views
2

我想用node.js中的serverside逻辑在html5中做一个游戏,并使用原始websockets(不是Socket.IO,我需要二进制数据)。我希望有多个“房间”,因此有多个websocket服务器,都有独立的URL。目前,我只找到了将每个websocket服务器连接到特定端口的方法,然后根据url将正确的端口代理升级请求(不完全确定它是如何工作的)。Nodejs:基于URL的websocket路由,无需端口代理

它适用于我的电脑。问题是,当我尝试将它提交给PaaS提供商(AppFog)时,代码失败,因为它们不允许打开除提供的http端口以外的任何端口。

这里是我的代码一个漂亮的清理版本:

//start web server (basic static express server) on 8080 
// ... 


//start game server and listen to port 9000 
// I use the ws module for websockets 
// I plan to have a couple of these "game servers" 
// ... 


//open the proxy server. 

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

var webProxyServer = httpProxy.createServer(function (req, res, proxy){ 

    // I need http requests to be redirected to the "game servers" 

    if(req.url.substring(0, "/room1".length) === "/room1") // if starts with "/room1" 

     proxy.proxyRequest(req, res, { 
     host: 'localhost', 
     port: 9000 
     }); 


    else 

     proxy.proxyRequest(req, res, { 
     host: 'localhost', 
     port: 8080 
     }); 

} 

webProxyServer.on('upgrade', function (req, socket, head) { 

    //redirecting logic goes here 

    if(req.url=="/room1/"){ 

     webProxyServer.proxy.proxyWebSocketRequest(req, socket, head, { 
     host: 'localhost', 
     port: 9000 
     }) 
    } 

}); 


webProxyServer.listen(8000); //the "outside port". 

我的问题:它在某种程度上可以打开的WebSocket服务器不听任何特定的端口,并且可以手动附加插槽给他们,让我不要不需要打开基本http端口以外的任何端口?我知道Socket.IO以某种方式做它。也许有一种方法来监听http服务器的升级事件并将套接字传递给正确的websocket服务器?

我很新的服务器端的东西,所以额外的信息在这里和那里会受到欢迎。

回答

1

不幸的是,Appfog不支持websockets。

Feature Roadmap page - 页面底部:显示websockets作为即将发生的事情(即他们不支持它)。