Socket.io is not connecting on Heroku

家住魔仙堡 提交于 2020-08-10 19:09:07

问题


I am using Socket.io in my NodeJS backend. However, the sockets do not work. For example, one should receive a link and then send them to all other rooms under the same code, but the code is not executing. In my heroku logs I receive no errors but when I inspect element the page I get

polling-xhr.js:268 GET https://localhost:5000/socket.io/?EIO=3&transport=polling&t=NDADDNH net::ERR_CONNECTION_REFUSED

and

Failed to load resource: net::ERR_CONNECTION_REFUSED

I have looked into similar problems from this forum and made several changes but none of them have solved the issue. Also a bunch of the posts answer with solutions for ws in general which I don't understand at all :/

From what I read the issue might be with my ports? I followed a few of them but the same errors still occured.

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
}

and then in my client I am using

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);
        });

FYI Everything works on localhost


回答1:


Localhost will not work on the server and if you are using default namespace you no need to specify the URL. So try this, this.socket = io()




回答2:


On the client side, you're trying to connect to localhost:5000 instead of the URL Heroku provides. Try this this.socket = io('heroku url goes here').



来源:https://stackoverflow.com/questions/62885098/socket-io-is-not-connecting-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!