【问题标题】:React Socket IO is not connected to heroku serverReact Socket IO未连接到heroku服务器
【发布时间】:2019-04-10 16:42:48
【问题描述】:

我正在尝试获取 heroku 的服务器或端口;这在 localhost 上运行良好,但 socket.io 功能在 heroku 上不起作用。

这是我在 heroku 上遇到的错误。

websocket.js:120 WebSocket 连接到 'wss://localhost:5000/socket.io/?EIO=3&transport=websocket' 失败: 连接建立错误:net::ERR_CONNECTION_REFUSED p.doOpen@websocket.js:120

App.js

const express = require('express');
const bodyParser = require('body-parser');
const  app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var PORT = process.env.PORT || 5000;

if (process.env.NODE_ENV === 'production') {

  app.use(express.static('client/build'));

  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  });
}

io.on('connection', (socket)=>{
    console.log(socket.id);

    socket.on('SEND_MESSAGE', (data)=>{
        io.emit('RECEIVE_MESSAGE', data);

    })


});

http.listen(PORT,function(){
  console.log('listening on port %s',PORT);
});

Chat.js

// this has to be something other than localhost, it needs to work vice versa.

this.socket = socketIOClient('localhost:5000');

this.socket.on('RECEIVE_MESSAGE', function(data){
    addMessage(data);
});

【问题讨论】:

    标签: node.js sockets heroku


    【解决方案1】:

    这是一个硬编码的解决方法

    app.js

      this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});
    
       if(!this.socket){
            this.socket = socketIOClient('localhost:5000');
       }
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 2020-04-28
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多