Heroku上的WebSocket ERR_CONNECTION_REFUSED [英] WebSocket on Heroku ERR_CONNECTION_REFUSED
问题描述
我正在尝试使用 WebSockets
在Heroku上运行节点js应用程序。但是,我无法解决此错误(如在Chrome浏览器中看到的那样)
I am trying to run a node js app on Heroku using WebSockets
. However, I am not able to resolve this error (As seen in conosle of Chrome browser)
WebSocket连接到'wss:// myappname .herokuapp.com:27225 /'失败:连接建立错误:net :: ERR_CONNECTION_REFUSED
WebSocket connection to 'wss://myappname.herokuapp.com:27225/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
自从Heroku使用'wss'在HTTPS上运行。
I am using 'wss' since Heroku runs on HTTPS.
我的客户端代码是:
$.get("https://myappname.herokuapp.com/port",function(data){
port = data;
console.log(data);
host = 'wss://myappname.herokuapp.com:' + port + '/';
ws = new WebSocket(host);
});
我的服务器端代码是:
var WebSocketServer = require("ws").Server
var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http');
var port = process.env.PORT || 5000;
var request = require('request');
var server = http.createServer(app);
var serverOnPort = server.listen(port);
console.log("Server listening on port ",port);
var wss = new WebSocketServer({server: serverOnPort});
console.log("websocket server created");
任何帮助将不胜感激。
Any help would be appreciated.
谢谢。
推荐答案
所以好像我在尝试用端口号覆盖它。只需将主机用作 wss://myappname.herokuapp.com/
效果很好。
So it seems like I was trying to over-do it with the port number. Just using the host as wss://myappname.herokuapp.com/
works well.
这篇关于Heroku上的WebSocket ERR_CONNECTION_REFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!