質問編集履歴
1
修正済みのコードを記載しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -341,3 +341,93 @@
|
|
341
341
|
asyncio.get_event_loop().run_forever()
|
342
342
|
|
343
343
|
```
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
### *追記(ご指摘頂いたように修正)
|
352
|
+
|
353
|
+
```python
|
354
|
+
|
355
|
+
import asyncio
|
356
|
+
|
357
|
+
import websockets
|
358
|
+
|
359
|
+
import json
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
address = "localhost"
|
366
|
+
|
367
|
+
port = 8888
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
Users = []
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
# 受信コールバック
|
376
|
+
|
377
|
+
async def server(websocket, path):
|
378
|
+
|
379
|
+
print(websocket)
|
380
|
+
|
381
|
+
print('connect.')
|
382
|
+
|
383
|
+
Users.append(websocket)
|
384
|
+
|
385
|
+
while(True):
|
386
|
+
|
387
|
+
# 受信
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
received_packet = await websocket.recv()
|
392
|
+
|
393
|
+
# dictionary = json.loads(received_packet)
|
394
|
+
|
395
|
+
print(type(received_packet))
|
396
|
+
|
397
|
+
print("{}: {}".format(path, received_packet))
|
398
|
+
|
399
|
+
# 送信
|
400
|
+
|
401
|
+
# dictionary['message'] = "Hi! Mr:{}.".format(dictionary['username'])
|
402
|
+
|
403
|
+
# dictionary['bool'] = False
|
404
|
+
|
405
|
+
# packet = json.dumps(dictionary).encode()
|
406
|
+
|
407
|
+
print('send')
|
408
|
+
|
409
|
+
packet = 'Hi! I received :{}'.format(received_packet)
|
410
|
+
|
411
|
+
for user in Users:
|
412
|
+
|
413
|
+
await user.send(packet)
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
# print(vars(websocket))
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
start_server = websockets.serve(server, address, port)
|
428
|
+
|
429
|
+
asyncio.get_event_loop().run_until_complete(start_server)
|
430
|
+
|
431
|
+
asyncio.get_event_loop().run_forever()
|
432
|
+
|
433
|
+
```
|