This commit is contained in:
Hiroki Ohtani
2014-10-14 15:21:31 +09:00
parent d18abf3083
commit 572367d0c1
3 changed files with 26 additions and 8 deletions

View File

@@ -20,9 +20,14 @@ for case in range(1, count+1):
try:
ws = websocket.create_connection(url)
opcode, msg = ws.recv_data()
if opcode == websocket.ABNF.OPCODE_TEXT:
msg.decode("utf-8")
if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
ws.send(msg, opcode)
success += 1
except UnicodeDecodeError:
# this case is ok.
success += 1
except Exception as e:
failed += 1
print("[Faield] Test Case: " + str(case))

View File

@@ -75,6 +75,9 @@ class ABNF(object):
self.data = data
self.get_mask_key = os.urandom
if rsv1 or rsv2 or rsv3:
raise NotImplementedError("rsv is not implemented, yet")
def __str__(self):
return "fin=" + str(self.fin) \
+ " opcode=" + str(self.opcode) \

View File

@@ -723,7 +723,8 @@ class WebSocket(object):
self.send_close()
return (frame.opcode, frame.data)
elif frame.opcode == ABNF.OPCODE_PING:
self.pong(frame.data)
if len(frame.data) < 126:
self.pong(frame.data)
if control_frame:
return (frame.opcode, frame.data)
elif frame.opcode == ABNF.OPCODE_PONG:
@@ -746,17 +747,24 @@ class WebSocket(object):
# 'NoneType' object has no attribute 'opcode'
raise WebSocketException("Not a valid frame %s" % frame)
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT):
if frame.opcode == ABNF.OPCODE_CONT and not self._cont_data:
if frame.opcode == ABNF.OPCODE_CONT and not self._recving_frames:
raise WebSocketException("Illegal frame")
if self._cont_data:
self._cont_data[1].data += frame.data
else:
self._cont_data = [frame.opcode, frame]
if self._cont_data:
self._cont_data[1] += frame.data
else:
if frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY):
self._recving_frames = frame.opcode
self._cont_data = [frame.opcode, frame.data]
if frame.fin:
self._recving_frames = None
if frame.fin or self.fire_cont_frame:
data = self._cont_data
self._cont_data = None
return data
elif frame.opcode == ABNF.OPCODE_CLOSE:
self.send_close()
return (frame.opcode, frame)
@@ -844,11 +852,13 @@ class WebSocket(object):
except:
pass
self._closeInternal()
self.shutdown()
def _closeInternal(self):
def shutdown(self):
"close socket, immediately."
if self.sock:
self.sock.close()
self.sock = None
def _send(self, data):
if isinstance(data, six.text_type):