websocket通信のクライアント側の実装に
websocket-clientを使いたいのですが、よく出てくるサンプルコードに不明な点があるため質問させて頂きました。
下記サンプルコードですが
•スレッドを作成する意味はなんでしょうか?
•受けとったメッセージは画面表示していますが、通信以降の処理に使うにはどう変数をとりまわしたらいいでしょうか?
Python
1import websocket 2try: 3 import thread 4except ImportError: 5 import _thread as thread 6import time 7 8def on_message(ws, message): 9 print(message) 10 11def on_error(ws, error): 12 print(error) 13 14def on_close(ws): 15 print("### closed ###") 16 17def on_open(ws): 18 def run(*args): 19 for i in range(3): 20 time.sleep(1) 21 ws.send("Hello %d" % i) 22 time.sleep(1) 23 ws.close() 24 print("thread terminating...") 25 thread.start_new_thread(run, ()) 26 27 28if __name__ == "__main__": 29 websocket.enableTrace(True) 30 ws = websocket.WebSocketApp("ws://echo.websocket.org/", 31 on_message = on_message, 32 on_error = on_error, 33 on_close = on_close) 34 ws.on_open = on_open 35 ws.run_forever()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/26 14:48 編集
2019/01/26 15:07