You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
raiseSocketIOError('%s is not a vailid port'%str(port))
74
72
ifnotisinstance(host, basestring):
75
-
raiseSocketIOError('%s is not a vailid ip'%str(host))
73
+
raiseSocketIOError('%s is not a vailid address'%str(host))
76
74
#checking if there are only numbers in the ip
77
75
try:
78
76
int(''.join(host.split('.')))
79
77
except:
80
-
raiseSocketIOError('%s is not a vailid ip'%str(host))
78
+
try:
79
+
#if the user entered a hostname like wwww.google.com it will resolve the ip
80
+
host=socket.gethostbyname(host)
81
+
except:
82
+
raiseSocketIOError('%s is not a vailid address'%str(host))
81
83
#defining all the variables
82
84
self.host=host
83
85
self.port=port
84
86
self.stop=False
87
+
self.connected=False
85
88
self.ws=None
86
89
self.data=''
87
90
self.heartbeatTimeout=20
88
91
self.datanew=False
89
92
self.debug=debug
90
93
self.secure=secure
91
94
92
-
#The heartbeat process, if you connect to a socket.io server, it returns a heartbeat timeout. If you don't respond to a heartbeat in time, i don't know what happens.
95
+
#The heartbeat process, if you connect to a socket.io server, it returns a heartbeat timeout. If you don't send a heartbeat the server will terminate the connection between you and the server
93
96
defheartbeat(self):
94
97
time=0
95
98
whilenotself.stop:
@@ -107,6 +110,8 @@ def receiver(self):
107
110
whilenotself.stop:
108
111
self.data=self.ws.recv()
109
112
self.datanew=True
113
+
ifself.data[0] =='1':
114
+
self.connected=True
110
115
ifself.debug:
111
116
print('Received packet from server which contains: %s'%str(self.data))
112
117
@@ -176,20 +181,23 @@ def connect(self):
176
181
threading.Thread(target=self.receiver).start()
177
182
ifself.heartbeatTimeout:
178
183
threading.Thread(target=self.heartbeat).start()
179
-
#Optional, check if server approved connection/respond (experimental)
184
+
#Optional, check if server approved connection/respond
180
185
sleep(0.75)
181
-
ifself.data[0] =='1':
186
+
ifself.connected:
187
+
atexit.register(self.disconnect)
182
188
return
183
189
else:
184
190
self.disconnect()
185
-
raiseSocketIOError('Server didn\'t return 1::')
191
+
raiseSocketIOError('Server didn\'t respond')
186
192
187
193
#Disconnect, just to be complete
188
194
defdisconnect(self):
189
-
#set the stop variable to true so the other processes stop asap
195
+
#set the stop variable to true so the other Threads stop asap
190
196
self.stop=True
197
+
self.connected=True
191
198
#send a disconnect message to the server
192
-
self.ws.send(self.encode('disconnect'))
199
+
ifself.ws:
200
+
self.ws.send(self.encode('disconnect'))
193
201
194
202
#Easy to understand, example: if you send 0::: the server will know you want to disconnect
0 commit comments