質問編集履歴

1

serverを追記しました

2021/01/03 00:08

投稿

taro_yamada
taro_yamada

スコア55

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
 
26
26
 
27
- ```python
27
+ ```client
28
28
 
29
29
  import asyncio
30
30
 
@@ -190,6 +190,62 @@
190
190
 
191
191
 
192
192
 
193
+ ```server
194
+
195
+ import asyncio
196
+
197
+ import datetime
198
+
199
+ import random
200
+
201
+ import websockets
202
+
203
+ import json
204
+
205
+
206
+
207
+
208
+
209
+ async def time(websocket, path):
210
+
211
+ count = 0
212
+
213
+ while True:
214
+
215
+ now = datetime.datetime.utcnow().isoformat() + "Z"
216
+
217
+ cid = f"{count:>6}"
218
+
219
+ char = 'longlongstr'
220
+
221
+ content = {'id':cid, 'message': char, 'time': str(now)}
222
+
223
+ j_content = json.dumps(content)
224
+
225
+ count += 1
226
+
227
+ await websocket.send(j_content)
228
+
229
+ # await asyncio.sleep(random.random() * 3)
230
+
231
+ await asyncio.sleep(1)
232
+
233
+
234
+
235
+ start_server = websockets.serve(time, "127.0.0.1", 5679)
236
+
237
+
238
+
239
+ asyncio.get_event_loop().run_until_complete(start_server)
240
+
241
+ asyncio.get_event_loop().run_forever()
242
+
243
+
244
+
245
+ ```
246
+
247
+
248
+
193
249
  <結果>
194
250
 
195
251
  INFO : 2021-01-02 23:59:50,151 : [TASKID: 0] タスクをキューに投入しました。