【发布时间】: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);
});
【问题讨论】: