在Heroku上托管后,Python socket.io客户端无法向服务器node.js发出事件

问题描述

我已经在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客户端未发出事件。

有人可以帮我吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)