Skip to content

Commit 14001a3

Browse files
committed
BUGFIXES!
1 parent 6439852 commit 14001a3

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

socketio.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
sio.disconnect() to disconnect
4040
'''
4141

42-
import httplib, thread, threading, json
42+
import httplib, thread, threading, json, atexit, socket
4343
from time import sleep
44-
from datetime import datetime, timedelta
45-
import signal
4644

4745
try:
4846
import websocket
@@ -72,24 +70,29 @@ def __init__(self, host, port, **kwargs):
7270
if not isinstance( port, ( int, long, basestring ) ):
7371
raise SocketIOError('%s is not a vailid port' % str(port))
7472
if not isinstance(host, basestring):
75-
raise SocketIOError('%s is not a vailid ip' % str(host))
73+
raise SocketIOError('%s is not a vailid address' % str(host))
7674
#checking if there are only numbers in the ip
7775
try:
7876
int(''.join(host.split('.')))
7977
except:
80-
raise SocketIOError('%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+
raise SocketIOError('%s is not a vailid address' % str(host))
8183
#defining all the variables
8284
self.host = host
8385
self.port = port
8486
self.stop = False
87+
self.connected = False
8588
self.ws = None
8689
self.data = ''
8790
self.heartbeatTimeout = 20
8891
self.datanew = False
8992
self.debug = debug
9093
self.secure = secure
9194

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
9396
def heartbeat(self):
9497
time = 0
9598
while not self.stop:
@@ -107,6 +110,8 @@ def receiver(self):
107110
while not self.stop:
108111
self.data = self.ws.recv()
109112
self.datanew = True
113+
if self.data[0] == '1':
114+
self.connected = True
110115
if self.debug:
111116
print('Received packet from server which contains: %s' % str(self.data))
112117

@@ -176,20 +181,23 @@ def connect(self):
176181
threading.Thread(target=self.receiver).start()
177182
if self.heartbeatTimeout:
178183
threading.Thread(target=self.heartbeat).start()
179-
#Optional, check if server approved connection/respond (experimental)
184+
#Optional, check if server approved connection/respond
180185
sleep(0.75)
181-
if self.data[0] == '1':
186+
if self.connected:
187+
atexit.register(self.disconnect)
182188
return
183189
else:
184190
self.disconnect()
185-
raise SocketIOError('Server didn\'t return 1::')
191+
raise SocketIOError('Server didn\'t respond')
186192

187193
#Disconnect, just to be complete
188194
def disconnect(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
190196
self.stop = True
197+
self.connected = True
191198
#send a disconnect message to the server
192-
self.ws.send(self.encode('disconnect'))
199+
if self.ws:
200+
self.ws.send(self.encode('disconnect'))
193201

194202
#Easy to understand, example: if you send 0::: the server will know you want to disconnect
195203
def encode(self, sort, message = 'Hello, World'):

0 commit comments

Comments
 (0)