我正在尝试获取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);
});
答案 0 :(得分:0)
这是一个硬编码的解决方法
app.js
this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});
if(!this.socket){
this.socket = socketIOClient('localhost:5000');
}