socket.io不能在heroku上工作
每当我尝试使用require("socket.io");
在heroku它失败,并显示消息“无法find模块socket.io”。
我认为这是我的设置问题,因为在我的本地node.js服务器中运行良好。
我需要改变什么?
您必须更改PaaS提供商。 Heroku不支持websocket。 作为nodejitsu被称为支持websockets。
另外,你可能忘记了在package.json依赖列表中添加socket.io
。
雪松上的Heroku不支持websocket
无论如何,你可以使用socket.io
io.set("transports", ["xhr-polling"]); io.set("polling duration", 10);
https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
package.json修改为
"dependencies": { "async": "0.1.18", "ejs": "0.4.3", "express": "2.4.6", "faceplate": "0.0.4", "socket.io": "latest" },
而服务器端的代码是:
var port=process.env.PORT || 3000; var http=require('http'); var app=http.createServer(function(req,res){ res.write("server listening to port:"+port); res.end(); }).listen(port); socket=require("socket.io"); io=socket.listen(app); io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); io.sockets.on("connection",function(socket){ console.log("new connection"); socket.on("eventA",function(data){ io.sockets.emit("eventB",data); }); });
像魅力一样工作!
根据这个项目的wiki页面 ,你需要将socket.IO上的传输选项更改为xhr-polling(10)秒。
websocket运输工作在testing状态的Heroku。 您可以使用heroku labs:enable websockets -a YOUR_APP_NAME
启用它heroku labs:enable websockets -a YOUR_APP_NAME
- 错误:使用Next.js在Heroku上产生EACCES
- 快递应用程序:webpack bundle.js不提供
- 你可以使用节点检查器来debugging与领class运行的node.js应用程序
- 当版本与父模块相同时,节点不会安装子模块依赖项
- 是否有可能使Heroku与C ++ BSONparsing器一起安装mongodb NPM软件包?
- Heroku – 用Node.js创build了一个facebook应用程序,它给出了types错误
- 错误R14(超过内存配额)heroku节点
- 在试图将我的第一个node.js应用程序部署到Heroku时,Web dynos不断崩溃
- bower安装是否需要与bower.json运行在同一个文件夹中?