我可以在heroku上设置socket.io聊天吗?

我有一个简单的socket.io聊天应用程序,我已经上传到新的Heroku'雪松'堆栈之一。

现在我几乎有所有的工作,但我已经遇到了一个绊脚石。 在我的本地主机上,我从客户端打开一个到套接字服务器的连接:

// lots of HTML omitted socket = new io.Socket('localhost', {port: 8888}); 

但是在Heroku上,我显然必须用别的东西替代这些值。

我可以从服务器上的过程对象获取端口,如下所示:

 port = process.env.PORT || 8888 

并传递给视图。

但是,我用什么来代替'localhost'呢?

现在已经改变,截至2013年10月,heroku增加了websocket支持:

https://devcenter.heroku.com/articles/node-websockets

使用:

 heroku labs:enable websockets 

要启用websockets,不要忘记删除:

 io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); 

根据heroku上的文章正确的方法是:

 io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); socket = new io.Socket(); 

这确保了io.Socket不会尝试使用WebSocket。

通过执行以下操作,我能够使Socket.IO v0.8在Heroku Cedar上运行:

在Express应用程序(在我的情况下在CoffeeScript中):

 app = express.createServer(); socket = require("socket.io") ... io = socket.listen(app); io.configure () -> io.set("transports", ["xhr-polling"]) io.set("polling duration", 10) io.sockets.on('connection', (socket) -> socket.on('myaction', (data) -> ... socket.emit('result', {myData: data}) ### The port setting is needed by Heroku or your app won't start port = process.env.PORT || 3000; app.listen(port); 

并在您的应用程序的前端JavaScript:

 var socket = io.connect(window.location.hostname); function sendSocketRequest() { socket.emit('myaction', $("#some_field").val()); } socket.on('result', function(data) { console.log(data); } 

有用的url:

  • Heroku Node帮助
  • Heroku Socket.IO帮助

在尝试了太阳下的每一个组合之后,我终于留下了空白。 你看,那完美的作品。 你甚至不需要端口。

 socket = new io.Socket(); 

我在heroku上也有这个问题。 我能够使用主机名“myapp.herokuapp.com”(或简单的window.location.hostname,同时工作本地和生产),并将端口设置为80.我使用的是SocketIO 0.6.0。

难道你只是把你的实际主机名?

2011-06-25T21:41:31+00:00 heroku[router]: Error H13 (Connection closed without response) -> GET appxxxx.herokuapp.com/socket.io/1/websocket/4fd434d5caad5028b1af690599f4ca8e dyno=web.1 queue= wait= service= status=503 bytes=

这可能意味着应用程序的heroku路由器盈方没有configuration为处理networking套接字stream量?

[更新]它会出现在2011年6月22日答案是肯定的… heroku不支持socket.io看到这个职位: http : //blog.heroku.com/archives/2011/6/22/the_new_heroku_2_node_js_new_http_routing_capabilities /