ws-testclient.py 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. # python websocket client to test with
  3. # emulator: server is at ws://127.0.0.1:9080/ws
  4. # esp8266: server is at ws:///ws
  5. # (uncomment the right line below)
  6. #uri = "ws://127.0.0.1:9080/ws"
  7. uri = "ws://arduinoWebsockets.local/ws"
  8. import websocket
  9. try:
  10. import thread
  11. except ImportError:
  12. import _thread as thread
  13. import time
  14. def on_message(ws, message):
  15. print("message");
  16. print(message)
  17. def on_error(ws, error):
  18. print("error")
  19. print(error)
  20. def on_close(ws):
  21. print("### closed ###")
  22. def on_open(ws):
  23. print("opened")
  24. def run(*args):
  25. for i in range(3):
  26. time.sleep(1)
  27. ws.send("Hello %d" % i)
  28. time.sleep(1)
  29. ws.close()
  30. print("thread terminating...")
  31. thread.start_new_thread(run, ())
  32. if __name__ == "__main__":
  33. websocket.enableTrace(True)
  34. ws = websocket.WebSocketApp(uri, on_message = on_message, on_error = on_error, on_close = on_close)
  35. ws.on_open = on_open
  36. ws.run_forever()