[英]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.