以下の様なHTMLとPythonファイルがあります。
クライアント接続されたらカウントを表示するプログラムです。
しかし、表示される→更新すると止まる→更新すると表示されるを繰り返します。
更新すると2回に一回はカウントが表示されません。
どの様にすれば更新しても毎回カウントが表示されますか?
html
1<html> 2<head> 3 <script src="http://code.jquery.com/jquery-latest.min.js"> 4 </script> 5 <script> 6 $(function(){ 7 var ws = new WebSocket("ws://localhost:9999/"); 8 var log = ""; 9 ws.onmessage = function(message){ 10 log = message.data; 11 document.getElementById("txt").innerHTML = log; 12 } 13 }) 14 </script> 15</head> 16 17<body> 18 message: <br> 19 <div id = "txt"></div> 20</body> 21 22</html>
python
1from websocket_server import WebsocketServer 2import time 3 4class Test: 5 def __init__(self): 6 print('開始') 7 self.count=0 8 server = WebsocketServer(port=9999, host="localhost") 9 server.set_fn_new_client(self.new_client) 10 server.run_forever() 11 12 def new_client(self,client, server): 13 while True: 14 server.send_message_to_all(str(self.count)) 15 print(self.count) 16 self.count=self.count+1 17 time.sleep(1) 18 19if __name__ == '__main__': 20 test=Test()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/18 12:36
2022/10/18 12:37
2022/10/18 13:40
2022/10/18 21:35 編集
2022/10/18 14:20
2022/10/19 01:08
2022/10/19 06:57