address issue #9 and other changes to the demo code
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import websocket
|
import websocket
|
||||||
import thread
|
import thread
|
||||||
import time
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
def on_message(ws, message):
|
def on_message(ws, message):
|
||||||
print message
|
print message
|
||||||
@@ -14,20 +15,27 @@ def on_close(ws):
|
|||||||
def on_open(ws):
|
def on_open(ws):
|
||||||
def run(*args):
|
def run(*args):
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
time.sleep(1)
|
# send the message, then wait
|
||||||
|
# so thread doesnt exit and socket
|
||||||
|
# isnt closed
|
||||||
ws.send("Hello %d" % i)
|
ws.send("Hello %d" % i)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
ws.close()
|
ws.close()
|
||||||
print "thread terminating..."
|
print "Thread terminating..."
|
||||||
|
|
||||||
thread.start_new_thread(run, ())
|
thread.start_new_thread(run, ())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
websocket.enableTrace(True)
|
websocket.enableTrace(True)
|
||||||
ws = websocket.WebSocketApp("ws://echo.websocket.org/",
|
if len(sys.argv) < 2:
|
||||||
|
host = "ws://echo.websocket.org/"
|
||||||
|
else:
|
||||||
|
host = sys.argv[1]
|
||||||
|
ws = websocket.WebSocketApp(host,
|
||||||
on_message = on_message,
|
on_message = on_message,
|
||||||
on_error = on_error,
|
on_error = on_error,
|
||||||
on_close = on_close)
|
on_close = on_close)
|
||||||
ws.on_open = on_open
|
ws.on_open = on_open
|
||||||
|
|
||||||
ws.run_forever()
|
ws.run_forever()
|
||||||
|
@@ -55,7 +55,6 @@ STATUS_INVALID_EXTENSION = 1010
|
|||||||
STATUS_UNEXPECTED_CONDITION = 1011
|
STATUS_UNEXPECTED_CONDITION = 1011
|
||||||
STATUS_TLS_HANDSHAKE_ERROR = 1015
|
STATUS_TLS_HANDSHAKE_ERROR = 1015
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
|
|
||||||
class WebSocketException(Exception):
|
class WebSocketException(Exception):
|
||||||
@@ -515,7 +514,11 @@ class WebSocket(object):
|
|||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
frame = self.recv_frame()
|
frame = self.recv_frame()
|
||||||
if frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY):
|
if not frame:
|
||||||
|
# handle error:
|
||||||
|
# 'NoneType' object has no attribute 'opcode'
|
||||||
|
raise WebSocketException("Not a valid frame %s" % frame)
|
||||||
|
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY):
|
||||||
return (frame.opcode, frame.data)
|
return (frame.opcode, frame.data)
|
||||||
elif frame.opcode == ABNF.OPCODE_CLOSE:
|
elif frame.opcode == ABNF.OPCODE_CLOSE:
|
||||||
self.send_close()
|
self.send_close()
|
||||||
|
Reference in New Issue
Block a user