-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_server.py
55 lines (47 loc) · 1.67 KB
/
ws_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Software Name : dynagraph
# Version: 1.0.0
# SPDX-FileCopyrightText: Copyright (c) 2021-2022 Orange
# SPDX-License-Identifier: BSD-4-Clause
#
# This software is distributed under the BSD-4-Clause, the text of which is available at https://spdx.org/licenses/ or see the "LICENSE.txt" file for more details.
#
# Author: Lionel TAILHARDAT
# Software description: The DynaGraph framework: a system combining classical traces dumping tools (i.e. the tshark tool and Firefox's Network Monitor component) and a ReactJS web app for live 3D graph rendering of streamed graph data derived from traces.
"""
Created on Sep 2021
@author: Lionel Tailhardat
:synopsis: WS server that sends messages at random intervals. Part of the DynaGraph project.
"""
import asyncio
import datetime
import random
import websockets
import json
import logging
logger = logging.getLogger('websockets')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + "Z"
data = json.dumps(
{
"source": random.randint(0, 25),
"target": random.randint(0, 25),
"time": now,
"group": random.randint(1, 5),
"value": random.randint(1, 5)
}
)
await websocket.send(data)
await asyncio.sleep(random.random() * 3)
server_ip = "127.0.0.1"
server_port = 5678
start_server = websockets.serve(
time,
server_ip,
server_port)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()