在heroku上将websocket连接到htpps服务器的问题

时间:2021-07-12 12:43:10

标签: javascript node.js websocket

enter image description here

这是我在后端的代码,在前面我有

hostname = window.location.hostname;
ws = new WebSocket(`wss://${this.hostname}:8080`);

当我尝试使用时

this.ws.send('some message')

我收到以下错误:

Uncaught (in promise) DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

闲置一段时间后,我得到了

WebSocket connection to 'wss://MYURL...:8080/' failed: 

var app = express();
app.use(cookieParser());
app.set('trust proxy', 1);
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'html');
app.listen(port);
const server = https.createServer({
cert: fs.readFileSync('config/cert.pem', {
    encoding: 'utf8'
}),
key: fs.readFileSync('config/key.pem', {
    encoding: 'utf8'
})
})
const wss = new SocketServer.Server({
server,
});

wss.on('connection', function connection(ws) {
ws.on('message', (msg) => {
    wss.clients.forEach(client => {
        if (client !== ws) client.send(msg);
    });
});
});

server.listen(8080);

0 个答案:

没有答案