teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

serverを追記しました

2021/01/03 00:08

投稿

taro_yamada
taro_yamada

スコア55

title CHANGED
File without changes
body CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
 
13
13
 
14
- ```python
14
+ ```client
15
15
  import asyncio
16
16
  import json
17
17
  import websockets
@@ -94,6 +94,34 @@
94
94
 
95
95
  ```
96
96
 
97
+ ```server
98
+ import asyncio
99
+ import datetime
100
+ import random
101
+ import websockets
102
+ import json
103
+
104
+
105
+ async def time(websocket, path):
106
+ count = 0
107
+ while True:
108
+ now = datetime.datetime.utcnow().isoformat() + "Z"
109
+ cid = f"{count:>6}"
110
+ char = 'longlongstr'
111
+ content = {'id':cid, 'message': char, 'time': str(now)}
112
+ j_content = json.dumps(content)
113
+ count += 1
114
+ await websocket.send(j_content)
115
+ # await asyncio.sleep(random.random() * 3)
116
+ await asyncio.sleep(1)
117
+
118
+ start_server = websockets.serve(time, "127.0.0.1", 5679)
119
+
120
+ asyncio.get_event_loop().run_until_complete(start_server)
121
+ asyncio.get_event_loop().run_forever()
122
+
123
+ ```
124
+
97
125
  <結果>
98
126
  INFO : 2021-01-02 23:59:50,151 : [TASKID: 0] タスクをキューに投入しました。
99
127
  INFO : 2021-01-02 23:59:50,151 : [TASKID: 0] 処理を開始します。