2013-04-14 61 views
0

我使用node.js设置了Socket.IO,它通过侦听/连接到端口8000(或另一个不是我的服务器的端口的端口)工作在本地开发计算机上正在运行)。在Heroku上使用Node.js的Socket.IO

当我尝试在heroku上做同样的事情时,我的客户端脚本导入失败。

我已经试过相对路径

<script src="/socket.io/socket.io.js"></script> 

我也试图绑定到端口8000在Heroku和使用的全路径名

<script type="text/javascript"> 
     var href = document.location.protocol + document.location.hostname + ':8000/socket.io/socket.io.js'; 
     console.log("href = " + href); 
     document.write("<script src=" + href + "><\/script>"); 
    </script> 

我没有读到的WebSockets不要在Heroku工作,所以我在后端的after_start.js文件中有以下代码。除了配置socket.io以使用传输/轮询之外,还需要做其他事吗?

var port = process.env.PORT || 8000; //I've tried hardcoding to 8000 and using process.env.PORT and then I used the relative path for the script import 
io = require('socket.io').listen(port); 
//Configue io to work with heroku 
io.configure(function() { 
    io.set("transports", ["xhr-polling"]); 
    io.set("polling duration", 10); 
}); 

我得到这个错误,当我打开我的Heroku应用程序:

GET http://<myAppName>.herokuapp.com/socket.io/socket.io.js 404 (Not Found) 

编辑:我实际使用geddy MVC框架,并希望得到这个工作,我怎么把它设置(基本上只是像socket.io)在heroku上,我发现这个答案,使得它看起来像我可以使用它类似: GeddyJS & socket.io: catching and emitting events from server side

+0

你需要使socket.io监听你的主'服务器'实例(与Express一起使用) – SLaks

+0

你是否启动了一个http服务器,如express,或者只是socket.io? –

+0

已更新原始帖子。我正在使用Geddy。 –

回答

0

啊哈!我忘了在配置中打开geddy的实时设置。

这里是我做过什么:

添加

, socketIo: true 
, realtime: true 

到配置文件

新增

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

到after_start.js文件

使用geddy.io.sockets.emit(从后端发送事件。

<script src="/socket.io/socket.io.js"></script>进口的前端

var href = document.location.protocol + document.location.hostname; 
console.log('href = ' + href); 
var socket = io.connect(href); 

IO要打开从前端的插座。

相关问题