我已经在 localhost 上测试了代码,它可以工作。但是,当我将它推送到 heroku 时,代码不会发出事件。
我在 python 客户端使用这个库:https ://python-socketio.readthedocs.io/en/latest/client.html#installation
这是我的代码:服务器:(我在故障上托管此代码)
onst express = require('express') // to host web server
const socketIO = require('socket.io')// to provide us the realtime communication
const cors = require('cors')
const port = process.env.PORT
// const port = process.env.PORT || 80
const app = express()
app.use(cors())
const server = require('http').createServer(app).listen(port)
const io = socketIO.listen(server,{
pingInterval: 10000,
pingTimeout: 5000,
})
io.set('origins', '*:*')
console.log('Server has started...')
app.get('/', function (req, res) {
res.sendStatus(200)
})
// Whenever a socket/user joins the server (io),
io.on('connection', (socket) => {
console.log(socket.id)
socket.on('online',()=>{
console.log('online')
//do nothing
})
socket.on('NEW_ORDER', (data,id)=>{
console.log('receive order',data)
console.log(data['order'])
})
}
客户端:(我在heroku上托管这些代码)
import socketio
sio = socketio.Client()
@hawker_centers_blueprint.route('/payment_successful', methods=['POST'])
def pymt_succesful():
print('connected to socket', sio.sid) #this shows on heroku log and i can know the socket's id
sio.emit('online')#this does not show on server side's log
sio.emit('NEW_ORDER',{'order':order, 'eatery_id':order['eatery_id']}) #This does not show on server side's log
print('emit order')# this shows on heroku's log
sio.connect('https://magenta-buttery-silver.glitch.me/')
当我检查日志时,我知道我的 python 客户端已连接到套接字服务器。当 python 客户端发出事件时服务器没有反应,或者 python 客户端没有发出事件。
谁能帮我这个?