我在server.js中有这个
//socket io config
const server = require('http').createServer(app)
const io = require('socket.io')(server)
io.on('connection', function (socket) {
socket.on('SOCKET_COMMENT_RECEIVED', ({ notification }) => {
io.emit(`SOCKET_COMMENT_RECEIVED`, notification)
})
//and many more
})
在我的客户中(反应)
import io from 'socket.io-client'
const socket = io('localhost:3001') // working in localhost
在我的产品中,我会进行检查
let socket = io('localhost:3001')
if(process.env.NODE_ENV === 'production') { socket =
io('https://api.example.com:3001')
}
为什么会这样?我认为这不是问题,因为我已经这样做了
app.use(cors({
origin: true,
credentials: true
}))
我的package.json部门
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1",