1.socketio和websocket 的区别
WebSocket是一种通信协议,它通过TCP连接在客户端和服务器之间提供双向通信,WebSocket连接始终保持打开状态,因此它们允许实时数据传输。当客户端向服务器触发请求时,它不会在接收到响应时关闭连接,而是会持续存在并等待客户端或服务器终止请求。
Socket.IO 是一个库,可用于在客户端和Web服务器之间进行实时和全双工通信。它使用WebSocket协议提供接口。通常,它分为两部分,WebSocket和Socket.io都是事件驱动的库.
简单说 socketio 是对websocket的封装 服务端用socketio客户端也要用socketio 服务端用websocket客户端也要用websocket
客户端连接方式
import socketio
sio = socketio.Client()
ut = "1"
@sio.event
def connect():
print('connection established')
# 监听服务端推送消息
@sio.event
def user_message(data):
print('user_message received with ', data)
# sio.emit('my response', {'response': 'my response'})
@sio.event
def disconnect():
print('disconnected from server')
# 连接服务端 IP+端口
sio.connect('http://localhost:8091')
print("000")
# 向服务端发送消息
sio.emit('sub_user_message', ut)
sio.wait()
原创文章,作者:jamestackk,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/272154.html