前言:
websocket和socketIO是全然不同的两个东西,websocket的话 使用自带ws的库就可以完成一些功能,但是socketIO属于sw的另外一块内容
工作中遇到了一个监控socketIO传输的聊天信息监控的需求.研究了一阵 话不多说 上代码!
from socketIO_client import SocketIO, BaseNamespace
import time
import requests
import logging
from app.test_report.constant import VIEWER_DATA, TEACHER_DATA, VIEWER_DATA_MUTIL, TEACHER_DATA_MUTIL # 这里引入四个变量 放在flask里面起一个单独文件防止有坑!
logging.getLogger('socketIO-client').setLevel(logging.DEBUG)
logging.basicConfig() # 调试时候可以自定义日志
class TeacherNamespace(BaseNamespace):
def on_teacher_response(self, *args):
print('qqq', args, type(args))
class ViewerNamespace(BaseNamespace):
def on_viewer_response(self, *args):
print(args, type(args))
def get_sessionid(userid, roomid):
pass
def on_connect(*args):
# print(*args)
print('connect')
def on_disconnect():
print('## disconnect ##')
def on_reconnect():
print('reconnect')
def revc_message(*args): # 这个函数很重要 是socketio监听信息获取的函数, 这里面socketio会自行调用这个函数
response_data_format = {}
response_data = eval(args[0])
print(response_data)
def revc_message_mutil(*args): # 这个函数很重要 是socketio监听信息获取的函数, 这里面socketio会自行调用这个函数
response_data_format = {}
response_data = eval(args[0])
print(response_data)
def create_params(userid,roomid): # 这个方法类似requests库的封装方法 看底层貌似与requests的urllib3方法一样
vsessionid, tsessionid = get_sessionid(userid, roomid)
vparams = {
'sessionid': vsessionid,
}
tparams = {
'sessionid': tsessionid,
}
return vparams, tparams
def sio(userid, roomid, host=' xxx.net', vparams='', tparams=''):
if not vparams and not tparams:
vparams, tparams = create_params(userid, roomid)
vsocketIO = SocketIO(host, params=vparams)
tsocketIO = SocketIO(host, params=tparams)
viewer = vsocketIO.define(ViewerNamespace, path=f'/asjbfasbfk') # 这个path类似于信道的路径 很重要
teacher = tsocketIO.define(TeacherNamespace, path=f'/asjbfasbfk') # 这个path类似于信道的路径 很重要
viewer.on('connect', on_connect)
viewer.on('chat_message', revc_message)
viewer.on('disconnect', on_disconnect)
viewer.on('reconnect', on_reconnect)
teacher.on('connect', on_connect)
teacher.on('chat_message', revc_message)
teacher.on('disconnect', on_disconnect)
teacher.on('reconnect', on_reconnect)
return teacher, vsocketIO, tsocketIO
def send_msg(domain_host, featurestype, sendmessage, userid, roomid, timer=1):
teacher, vsocketIO, tsocketIO = sio(userid, roomid, host=domain_host)
for i in range(timer):
teacher.emit('chat_message', f'{sendmessage}', revc_message)
vsocketIO.wait(seconds=1)
tsocketIO.wait(seconds=1)
return VIEWER_DATA, TEACHER_DATA
def send_msg_mutil(domain_host, featurestype, sendmessage, userid, roomid, timer=1, vparams='', tparams=''):
teacher, vsocketIO, tsocketIO = sio(userid, roomid, host=domain_host, vparams=vparams, tparams=tparams)
for i in range(timer):
teacher.emit('chat_message', f'{sendmessage}', revc_message_mutil)
vsocketIO.wait(seconds=1)
tsocketIO.wait(seconds=1)
return VIEWER_DATA, TEACHER_DATA
def clear_data():
VIEWER_DATA.clear()
TEACHER_DATA.clear()
if __name__ == '__main__':
s = time.time()
print(send_msg('xxxx.net', 1, '发送信息1', '7848DB3F76g7ts7057F', 'E301DFFFCDDAG7S79GY9A01307461'))
print(time.time()-s)
clear_data()