cshenfu
2023-03-14 2903954ebc0d10a81b78741bc5ddae3bc7216cf6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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()