File size: 1,900 Bytes
5a79bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import threading
import urllib.parse

import websocket

"""
    初始化websocket连接
"""
def init_connection(url):
    ws = websocket.WebSocketApp(url, on_open=ClientThread.on_open, on_message=ClientThread.on_message,
                                on_close=ClientThread.on_closed, on_error=ClientThread.on_error)
    # 异步监听返回结果
    client = ClientThread(ws=ws)
    client.start()
    return client


"""
    初始化websocket连接, 并附带相关参数
"""
def init_connection_with_params(url, params):
    url_prams_builder = urllib.parse.urlencode(params)
    url = url + '?' + url_prams_builder
    return init_connection(url)


"""
    发送text message
"""
def send_text_message(ws, message):
    ws.send(message)
    print("send text message: " + message)


"""
    发送binary message
"""
def send_binary_message(ws, message):
    ws.send(message, websocket.ABNF.OPCODE_BINARY)
    print("send binary message length: " + str(len(message)))


class ClientThread(threading.Thread):
    def __init__(self, ws):
        threading.Thread.__init__(self)
        self.ws = ws
        ws.is_connect = False

    def run(self):
        self.ws.run_forever()

    def return_is_connect(self):
        return self.ws.is_connect

    def on_message(ws, message):
        print("received message: " + message)
        # 该判断方式仅用作demo展示, 生产环境请使用json解析
        if "\"errorCode\":\"0\"" not in message:
            sys.exit()

    def on_open(ws):
        print("connection open")
        ws.is_connect = True

    def on_closed(ws, close_status_code, close_msg):
        if not close_status_code:
            close_status_code = 'None'
        if not close_msg:
            close_msg = 'None'
        print("connection closed, code: " + close_status_code + ", reason: " + close_msg)

    def on_error(ws, error):
        print(error)