简体   繁体   English

Python:通过socket.io每10秒发送一次数据

[英]Python: Send data every 10 seconds via socket.io

I have a question very similar to this one: Send data every 10 seconds via socket.io我有一个与此非常相似的问题: 每 10 秒通过 socket.io 发送数据

But: My server is written in Python, the client is in JavaScript但是:我的服务端写在Python,客户端写在JavaScript

The goal:目标:

  • Clients connect to server via socket.io客户端通过 socket.io 连接到服务器
  • Clients receive push messages ping from server every n seconds客户端每 n 秒接收一次来自服务器的推送消息ping
  • When a client sends a ping message, the server broadcasts a pong message当客户端发送ping消息时,服务器广播pong消息

What works:什么有效:

  • Socket.io connection works fine Socket.io 连接正常
  • Client ping is received by server, answered with pong , which is again received by client客户端ping由服务器接收,用pong应答,客户端再次接收
  • Server executes ping_in_intervals every 5 seconds服务器每 5 秒执行一次ping_in_intervals

What doesn't work:什么不起作用:

  • When server executes ping_in_intervals (which triggers sending a ping ), that ping is not received by any client当服务器执行ping_in_intervals (触发发送ping )时,任何客户端都不会收到该ping
  • When ping_in_intervals loop is active, socket connections crash every minute or so.ping_in_intervals循环处于活动状态时,套接字连接每分钟左右崩溃一次。 If the method is commented out, then socket connection stays stable.如果该方法被注释掉,则套接字连接保持稳定。

Observations:观察:

  • The thread, that ping_in_intervals is running in doesn't seem to properly work together with the wsgi server thread.运行ping_in_intervals的线程似乎无法与 wsgi 服务器线程一起正常工作。
  • The ping_in_intervals thread destabilizes the server thred, causes it to loose connections (which are reestablished right away, but they do drop every minute or so) ping_in_intervals线程使服务器 thred 不稳定,导致它失去连接(立即重新建立连接,但它们确实每分钟左右断开一次)
  • I think, that I'm doing something terribly wrong with threading.我认为,我在线程方面做错了一些事情。 I have very little experience with threading in Python and don't know, where to look for the problem我在 Python 中的线程经验很少,不知道在哪里寻找问题

Server:服务器:

import eventlet
import socketio
import threading

sio = socketio.Server(cors_allowed_origins="*", async_mode='eventlet')
app = socketio.WSGIApp(sio)


def ping_in_intervals():
    threading.Timer(5.0, ping_in_intervals).start()
    print("send ping")
    sio.emit('ping')


@sio.on('ping')
def ping(*args):
    print("received ping - send pong")
    sio.emit('pong')


ping_in_intervals()
eventlet.wsgi.server(eventlet.listen(('', 8080)), app)

Client:客户:

const socket = io.connect('localhost:8080', {secure: true, transports: ['websocket']});

socket.on('pong', () => {
    console.log('received pong');
});

socket.on('ping', () => {
    console.log('received ping');
});

socket.on('connect', () => {
    socket.emit('ping')
});

Found the solution at https://github.com/miguelgrinberg/python-socketio/blob/main/examples/server/wsgi/app.py#L16-L22https://github.com/miguelgrinberg/python-socketio/blob/main/examples/server/wsgi/app.py#L16-L22找到解决方案

The thread, which pushes server messages every n seconds, shouldn't be started using threading , but instead using the start_background_task function of socketio .该线程每 n 秒推送一次服务器消息,不应使用threading启动,而应使用start_background_task的 start_background_task socketio

Here's the working code:这是工作代码:

import eventlet
import socketio

sio = socketio.Server(cors_allowed_origins="*", async_mode='eventlet')
app = socketio.WSGIApp(sio)    

def ping_in_intervals():
    while True:
        sio.sleep(10)
        sio.emit('ping')        

@sio.on('ping')
def ping(*args):
    sio.emit('pong')

thread = sio.start_background_task(ping_in_intervals)
eventlet.wsgi.server(eventlet.listen(('', 8080)), app)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM