[英]Socket.IO with Node.js on Heroku
我已经用node.js设置了Socket.IO,并且通过侦听/连接到端口8000(或另一个不是我的服务器正在运行的端口)来在我的本地开发计算机上工作。
当我尝试在heroku上执行相同的操作时,客户端脚本导入失败。
我已经尝试过相对路径
<script src="/socket.io/socket.io.js"></script>
我也尝试绑定到heroku上的端口8000并使用完整路径名
<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框架,并想让它如何在heroku上进行设置(基本上就像socket.io一样),我发现这个答案使我似乎可以类似地使用它: GeddyJS& socket.io:从服务器端捕获和发出事件
啊啊! 我忘了在配置中打开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>
以在前端导入io
和
var href = document.location.protocol + document.location.hostname;
console.log('href = ' + href);
var socket = io.connect(href);
从前端打开插座。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.