OLD | NEW |
(Empty) | |
| 1 import websocket |
| 2 import thread |
| 3 import time |
| 4 import sys |
| 5 |
| 6 def on_message(ws, message): |
| 7 print message |
| 8 |
| 9 def on_error(ws, error): |
| 10 print error |
| 11 |
| 12 def on_close(ws): |
| 13 print "### closed ###" |
| 14 |
| 15 def on_open(ws): |
| 16 def run(*args): |
| 17 for i in range(3): |
| 18 # send the message, then wait |
| 19 # so thread doesnt exit and socket |
| 20 # isnt closed |
| 21 ws.send("Hello %d" % i) |
| 22 time.sleep(1) |
| 23 |
| 24 time.sleep(1) |
| 25 ws.close() |
| 26 print "Thread terminating..." |
| 27 |
| 28 thread.start_new_thread(run, ()) |
| 29 |
| 30 if __name__ == "__main__": |
| 31 websocket.enableTrace(True) |
| 32 if len(sys.argv) < 2: |
| 33 host = "ws://echo.websocket.org/" |
| 34 else: |
| 35 host = sys.argv[1] |
| 36 ws = websocket.WebSocketApp(host, |
| 37 on_message = on_message, |
| 38 on_error = on_error, |
| 39 on_close = on_close) |
| 40 ws.on_open = on_open |
| 41 ws.run_forever() |
OLD | NEW |