-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketCapf.py
More file actions
executable file
·157 lines (124 loc) · 4.51 KB
/
WebSocketCapf.py
File metadata and controls
executable file
·157 lines (124 loc) · 4.51 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- config:utf-8 -*-
import sys
import time
from threading import Thread
import requests
import websocket
import json
class WebSocketCapf() :
def __init__(self, loginurl, id, passwd, websockurl) :
self.loginurl = loginurl
self.id = id
self.passwd = passwd
self.websockurl = websockurl
self.token = None
self.wss = None
self.wssThread = None
self.messageCallback = None
self.connected = False
def isactive(self) :
return self.connected
def getUrl(self) :
return self.websockurl
def getLoginurl(self) :
return self.loginurl
def getLoginToken(self) :
return self.token
def setMessageCallback(self, func) :
self.messageCallback = func
def on_messageCB(self, message) :
if message == 'something' : return
if not self.messageCallback is None :
self.messageCallback(message)
def on_errorCB(self, err):
pass
print("Error :", err)
def on_openCB(self):
self.connected = True
pass
#print("opening")
def on_closeCB(self):
self.connected = False
#print("close")
def send(self, message) :
#print("SEND :", message)
#print(self.wss.sock.connected)
if not self.isactive() :
print("reconnect")
self.connect()
if self.isactive():
self.wss.send(message)
else :
print('cannnot send')
def connect(self) :
closed = False
def on_message(ws, message) :
self.on_messageCB(message)
def on_error(ws, error) :
self.on_errorCB(error)
def on_open(ws) :
self.on_openCB()
def on_close(ws, status_code, close_msg) :
closed = True
#print(status_code, close_msg)
self.on_closeCB()
result = False
payload = {"name": self.id, "password": self.passwd}
#login
self.token = None
#print(self.loginurl, payload)
try :
r = requests.post(self.loginurl, params=payload)
#print(r)
except :
print('Error : ' + self.loginurl)
return False
if r.status_code != 200:
print('Error : ' + self.loginurl + " response : " + str(r.status_code))
return False
if r.text is None :
return False
jdic = json.loads(r.text)
if "authorisation" in jdic.keys():
authorisation = jdic["authorisation"]
if authorisation is None :
return False
if "token" in authorisation.keys():
self.token = authorisation["token"]
if self.token is None :
return False
dummyws = websocket.WebSocket()
try:
dummyws.connect(self.websockurl + '?token=' + self.token)
except :
print("Error : " + self.websockurl + " connect Error")
return False
dummyws.close()
self.wss = websocket.WebSocketApp(self.websockurl + '?token=' + self.token,
on_message = on_message,
on_error = on_error,
on_open = on_open,
on_close = on_close )
self.wssThread = Thread(target = self.wss.run_forever)
self.wssThread.daemon = True
self.wssThread.start()
while not self.connected and not closed:
time.sleep(0.01)
#print('closed =', closed)
if not closed :
result = True
return result
# test
if __name__ == '__main__' :
def cb(message):
print('sutekina ', message)
#websocket.enableTrace(True)
wssock = WebSocketCapf('https://atr-dev02.ca-platform.org/api/login',
'CA001', 'CA001', 'wss://atr-dev02-websocket.ca-platform.org')
wssock.setMessageCallback(cb)
if not wssock.connect() :
print('cannot connect websocket')
sys.exit(0)
#wssock.send('test'.encode())
while True :
time.sleep(1)