import this import socketio def sio(url,tparams): if not tparams: sio = socketio.Client() sio.connect(url) sio.on('connect', on_connect) sio.on('chat_message', revc_message) sio.on('disconnect', on_disconnect) sio.on('reconnect', on_reconnect) print(tparams) return sio 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(url=' xxx.net', vparams='', tparams=''): if not tparams: sio = socketio.Client() this.sio.connect('http://localhost:5000') print('socketio connect success!!!') sio.on('connect', on_connect) sio.on('chat_message', revc_message) sio.on('disconnect', on_disconnect) sio.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() class Socketio_client(): sio = socketio.Client() def create_client(): print('-----------start socketio.clent------------') # sio = socketio.Client() this.sio.connect('http://localhost:5000') print('socketio connect success!!!') @this.sio.event def connect(): print('connection established') this.sio.emit('client', {'msg': 'connect success'}) @this.sio.on('serve') def on_message(data): print('client received a message!',data) # @sio.event # def message(data): # print('message received with ', data) # sio.emit('client', {'response': 'my response'}) @this.sio.event def connect_error(): print("The connection failed!") this.sio.disconnect() @this.sio.event def disconnect(): print('disconnected from server') this.sio.disconnect() # sio.wait() def sendmsg(msg): this.sio.emit('client', {'msg': msg}) def connect(): this.sio.connect('http://localhost:5000') print('socketio connect success!!!') create_client()