代码在本地运行。当我把它推到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)
答案 0 :(得分:3)
首先,如果您希望您的客户端连接到您的heroku应用程序,您应该使用您的heroku应用程序主机而不是localhost。
var host = location.origin;
io.connect(host, {port: PORT, transports: ["websocket"]});