简体   繁体   English

如何在python-socketio上发送消息

[英]How to send message on python-socketio

The API documentation ( https://python-socketio.readthedocs.io/en/latest/intro.html ) provides examples of the server and client. API 文档 ( https://python-socketio.readthedocs.io/en/latest/intro.html ) 提供了服务器和客户端的示例。 But if you run them, you will not start messaging.但是如果你运行它们,你将不会开始消息传递。 And I'm not sure how to set it up.而且我不确定如何设置。

How do I set up messaging so I can output their body through the print function?如何设置消息传递以便我可以通过print功能输出他们的身体?

client.py客户端.py

import socketio

sio = socketio.Client()


@sio.event
def connect():
    print('connection established')


@sio.event
def my_message(data):
    print('message received with ', data)
    sio.emit('my response', {'response': 'my response'})


@sio.event
def disconnect():
    print('disconnected from server')


sio.connect('http://localhost:5000')
sio.wait()

server.py服务器.py

import eventlet
import socketio


sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})


@sio.event
def connect(sid, environ):
    print('connect ', sid)
    my_message(sid, {'Test': 'Message'})


@sio.event
def my_message(sid, data):
    sio.send(data)
    print('Send message ', data)


@sio.event
def disconnect(sid):
    print('disconnect ', sid)


if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 5000)), app)

Trace client.py跟踪客户端.py

venv) user@debian:~/work/self_prj/socket_io$ python3 client.py
connection established

Trace server.py跟踪服务器.py

(13043) wsgi starting up on http://0.0.0.0:5000
(13043) accepted ('127.0.0.1', 50352)
127.0.0.1 - - [11/Sep/2019 15:24:08] "GET /socket.io/?transport=polling&EIO=3&t=1568204648.5529237 HTTP/1.1" 200 385 0.000619
connect  cff05ec678794128a6541f33bf3ef6dd
Send message  {'Test': 'Message'}
(13043) accepted ('127.0.0.1', 50356)
127.0.0.1 - - [11/Sep/2019 15:24:08] "POST /socket.io/?transport=polling&EIO=3&sid=cff05ec678794128a6541f33bf3ef6dd HTTP/1.1" 200 167 0.000148
127.0.0.1 - - [11/Sep/2019 15:24:08] "GET /socket.io/?transport=polling&EIO=3&sid=cff05ec678794128a6541f33bf3ef6dd&t=1568204648.5590577 HTTP/1.1" 200 183 0.002194
127.0.0.1 - - [11/Sep/2019 15:24:33] "POST /socket.io/?transport=polling&EIO=3&sid=cff05ec678794128a6541f33bf3ef6dd HTTP/1.1" 200 167 0.000372
127.0.0.1 - - [11/Sep/2019 15:24:33] "GET /socket.io/?transport=polling&EIO=3&sid=cff05ec678794128a6541f33bf3ef6dd&t=1568204648.5659044 HTTP/1.1" 200 183 24.994966
127.0.0.1 - - [11/Sep/2019 15:24:58] "POST /socket.io/?transport=polling&EIO=3&sid=cff05ec678794128a6541f33bf3ef6dd HTTP/1.1" 200 167 0.000355

You established a connection.您建立了连接。 But you didn't print the message.但是您没有打印消息。 In the client side, instead of this:在客户端,而不是这样:

@sio.event
def my_message(data):
    print('message received with ', data)
    sio.emit('my response', {'response': 'my response'})

Use that:使用那个:

@sio.event
def message(data):
    print('message received with ', data)
    sio.emit('my response', {'response': 'my response'})

I have recently set up a python socketio system to communicate between a local server and an AWS server.我最近设置了一个 python socketio 系统来在本地服务器和 AWS 服务器之间进行通信。 On the AWS I used Flask-SocketIO for the server, because I wanted to provide web page access, and python-socketio for the client.在 AWS 上,我使用 Flask-SocketIO 作为服务器,因为我想提供网页访问,并为客户端提供 python-socketio。

I'm not sure of the preferred way to do this, but I used (on the server)我不确定这样做的首选方法,但我使用过(在服务器上)

@sio.on('my_new_message')
def process_message(data):
    logging.warning("Data received: "+data)

Then on the client:然后在客户端:

sio.emit('my_new_message', "Test message")

The point being that the emit references the name given in the handler on the server (and the other way round if you have handlers on the client as well).关键是发出引用服务器上处理程序中给出的名称(如果客户端上也有处理程序,则相反)。 Using it this way, you can call the handlers what ever you want as long as you reference it with the emit.以这种方式使用它,只要您使用发射引用它,您就可以随意调用处理程序。

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

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