我在NodeJS后端中使用Socket.io。但是,套接字不起作用。例如,一个人应该收到一个链接,然后使用相同的代码将其发送到所有其他房间,但是该代码未执行。在我的heroku日志中,我没有收到任何错误,但是当我检查元素时,我得到的页面
polling-xhr.js:268 GET https://localhost:5000/socket.io/?EIO = 3&transport = polling&t = NDADDNH net::ERR_CONNECTION_REFUSED
和
无法加载资源:net::ERR_CONNECTION_REFUSED
我从这个论坛上调查了类似的问题,并进行了一些更改,但没有一个解决了该问题。还有很多帖子都回答了关于ws的解决方案,而我对此一点都不了解:/
从我读到的内容来看,问题可能出在我的端口上?我遵循了其中的一些,但是仍然发生相同的错误。
Socket.io:
/***BACKEND***/
const express = require('express');
const path = require('path');
const app = express();
let rooms = [];
/***SERVER***/
const port = process.env.PORT || 5000;
server = app.listen(port, function(){
console.log('App is listening on port ' + port)
});
/***SOCKET.IO***/
const socket = require('socket.io');
io = socket(server);
io.on('connection', (socket) => {
//bunch of functionality
}
然后我在我的客户中使用this.socket = io('localhost:5000');
//one of the functions
this.syncQueue = () => {
this.socket.emit('SYNC_QUEUE', {
activeRoom: this.props.getRoom()
});
}
this.socket.on('RECEIVE_QUEUE', newQueue => {
props.onAddToQueue(newQueue);
});
仅供引用一切都可以在本地主机上
最佳答案
Localhost将无法在服务器上运行,如果您使用的是默认 namespace ,则无需指定URL。所以试试这个,this.socket = io()
关于node.js - Socket.io未在Heroku上连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62885098/