Socket io和Node在本地工作但不能在heroku上工作

时间:2014-08-12 20:29:54

标签: node.js heroku socket.io

代码在本地运行。当我把它推到heroku时会出现问题。

我确信端口是正确的。我不知道问题是什么。我怀疑它是客户端代码的问题。我尝试将其更改为https://而不是http://但是这使它停止在本地和heroku上工作。

FF错误:

Firefox can't establish a connection to the server at ws://localhost/socket.io/?EIO=2&transport=websocket.

Chrome上的错误:

WebSocket connection to 'ws://localhost/socket.io/?EIO=2&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 404 

//on the client side
socket = io.connect("http://localhost", {port: PORT, transports: ["websocket"]});


//on the server side
var port = Number(process.env.PORT || 4000);
var express = require("express");
var logfmt = require("logfmt");
var app = express();
app.set('views', __dirname + '/views');

var server = require('http').createServer(app)
  io = require("socket.io").listen(server);

  io.set("transports", ["websocket"]);
  io.set("polling duration", 10)

https://bitbucket.org/hassanshaikley/amara-game/src/27b720f463f18b3ca1864fc1028d86e72928b126/scripts/game.js?at=master

请告诉我是否遗漏了细节。我有点难过。

1 个答案:

答案 0 :(得分:3)

首先,如果您希望您的客户端连接到您的heroku应用程序,您应该使用您的heroku应用程序主机而不是localhost。

var host = location.origin;
io.connect(host, {port: PORT, transports: ["websocket"]});