Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在同一端口上运行http Web服务器和ws-socket服务器_Node.js_Sockets_Heroku_Deployment_Network Protocols - Fatal编程技术网

Node.js 在同一端口上运行http Web服务器和ws-socket服务器

Node.js 在同一端口上运行http Web服务器和ws-socket服务器,node.js,sockets,heroku,deployment,network-protocols,Node.js,Sockets,Heroku,Deployment,Network Protocols,具有以下设置这些服务器的代码: const server = express() //... const wss = new SocketServer({ server }); 换句话说,一台服务器运行在,例如在,另一台运行在ws://localhost:8080。因此http服务器通过以下方式引用套接字服务器: var HOST = location.origin.replace(/^http/, 'ws') var ws = new WebSocket(HOST); //... 现在,虽

具有以下设置这些服务器的代码:

const server = express()
//...
const wss = new SocketServer({ server });
换句话说,一台服务器运行在,例如在,另一台运行在ws://localhost:8080。因此http服务器通过以下方式引用套接字服务器:

var HOST = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(HOST);
//...
现在,虽然我没有使用完全相同的代码,但在相同的地址和端口上运行两台服务器,唯一的区别是协议(http和ws)使websocket服务器无法访问:

Firefox can’t establish a connection to the server at ws://localhost:8080/.
将端口更改为任何其他端口都可以解决此问题。为什么呢?考虑到Heroku建议只使用静态端口作为备用端口,我应该如何实现我的应用程序

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