在同一端口上运行http-webserver和ws-socket-server

时间:2016-12-08 09:52:43

标签: node.js sockets heroku deployment network-protocols

Heroku tutorial on creating/deploying a web- and a socketserver有以下代码设置这些服务器:

const server = express()
//...
const wss = new SocketServer({ server });

换句话说,一个服务器运行在例如http://localhost:8080,另一个运行在ws:// localhost:8080。因此http服务器通过以下方式引用套接字服务器:

var HOST = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(HOST);
//...

现在虽然我没有使用完全相同的代码,但是在同一地址和端口上运行两个服务器,唯一的区别是协议(http和ws)使websocket-server无法访问:

Firefox can’t establish a connection to the server at ws://localhost:8080/.

将端口更改为其他任何内容可以解决问题。这是为什么?我应该如何实现我的应用程序,考虑到Heroku建议仅使用静态端口作为后退,如

var port = process.env.PORT || 8080;

代替?

0 个答案:

没有答案