WS server
This commit is contained in:
@ -5,14 +5,15 @@
|
||||
|
||||
```plantuml
|
||||
@startuml
|
||||
participant FE as "Frontend" << React >>
|
||||
participant FE as "Frontend" << JavaScript >>
|
||||
participant WS as "WS Server" << Python >>
|
||||
participant Funnel as "Funnel" << Python >>
|
||||
participant Segments as "ServeSegments" << Go >>
|
||||
participant CurrentTime as "ServeCurrentTime" << NodeJS >>
|
||||
|
||||
activate FE #hotpink
|
||||
FE -> FE: Load page
|
||||
FE -> Funnel: WS Connect
|
||||
FE -> WS: WS Connect
|
||||
|
||||
loop #ivory
|
||||
activate Funnel #gold
|
||||
@ -44,7 +45,8 @@ activate FE #hotpink
|
||||
return HH:MM:SS.ms
|
||||
return ms
|
||||
|
||||
Funnel -> FE: WS: Send formatted time
|
||||
Funnel -> WS: WS: Send complete time
|
||||
WS --> FE: WS: Send complete time
|
||||
deactivate Funnel
|
||||
end
|
||||
deactivate FE
|
||||
|
||||
44
src/frontend/index.html
Normal file
44
src/frontend/index.html
Normal file
@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Hello world</h1>
|
||||
|
||||
<h2 id="formatted-time">-</h2>
|
||||
<h3 id="comm-cycle">-</h3>
|
||||
</body>
|
||||
|
||||
<script type="module">
|
||||
window.onload = function () {
|
||||
const ws = new WebSocket('ws://localhost:5000');
|
||||
|
||||
ws.onmessage = function (event) {
|
||||
const data = JSON.parse(event.data);
|
||||
//console.log("message received:", data)
|
||||
document.getElementById("formatted-time").innerHTML = data.formattedTime;
|
||||
document.getElementById("comm-cycle").innerHTML = data.commCycleStr;
|
||||
|
||||
//ws.close();
|
||||
}
|
||||
ws.onopen = function () {
|
||||
console.log("open");
|
||||
}
|
||||
ws.onclose = function () {
|
||||
console.log("close");
|
||||
}
|
||||
ws.onerror = function () {
|
||||
console.log("error");
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
</html>
|
||||
@ -1,9 +1,12 @@
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncGenerator, Optional
|
||||
|
||||
import grpc
|
||||
import websockets
|
||||
from websockets.server import WebSocketServerProtocol
|
||||
|
||||
from stubs.serve_segments_pb2 import (
|
||||
GetHoursRequest,
|
||||
@ -21,6 +24,8 @@ SERVE_CURRENTTIME_HOST = "localhost"
|
||||
SERVE_CURRENTTIME_PORT = 50000
|
||||
SERVE_SEGMENTS_HOST = "localhost"
|
||||
SERVE_SEGMENTS_PORT = 50001
|
||||
WS_HOST = "localhost"
|
||||
WS_PORT = 5000
|
||||
|
||||
TIMEZONE = "Europe/Zagreb"
|
||||
|
||||
@ -61,6 +66,8 @@ async def get_milliseconds(stub: ServeSegmentsStub) -> Optional[int]:
|
||||
|
||||
|
||||
async def main():
|
||||
ws_uri = f"ws://{WS_HOST}:{WS_PORT}"
|
||||
async with websockets.connect(uri=ws_uri) as websocket:
|
||||
async with get_serve_segments_stub() as stub:
|
||||
while True:
|
||||
t = time.perf_counter()
|
||||
@ -81,13 +88,40 @@ async def main():
|
||||
minutes = task_minutes.result()
|
||||
seconds = task_seconds.result()
|
||||
milliseconds = task_milliseconds.result()
|
||||
|
||||
t = time.perf_counter() - t
|
||||
|
||||
print(
|
||||
f"RESULT: {hours:02d}:{minutes:02d}:{seconds:02d}:{milliseconds:03d}, T: {t}"
|
||||
formatted_time = (
|
||||
f"{hours:02d}:{minutes:02d}:{seconds:02d}:{milliseconds:03d}"
|
||||
)
|
||||
comm_cycle = time.perf_counter() - t
|
||||
|
||||
data = {
|
||||
"hours": hours,
|
||||
"minutes": minutes,
|
||||
"seconds": seconds,
|
||||
"milliseconds": milliseconds,
|
||||
"formattedTime": formatted_time,
|
||||
"commCycle": comm_cycle,
|
||||
"commCycleStr": f"{comm_cycle:0.4f}",
|
||||
}
|
||||
|
||||
await websocket.send(json.dumps(data))
|
||||
await websocket.recv()
|
||||
|
||||
# time.sleep(0.5)
|
||||
|
||||
print(f"RESULT: {formatted_time}, T: {comm_cycle}")
|
||||
|
||||
|
||||
async def send_ws_message():
|
||||
ws_uri = f"ws://{WS_HOST}:{WS_PORT}"
|
||||
async with websockets.connect(ws_uri) as websocket:
|
||||
data = {
|
||||
"message": "some text",
|
||||
"value": 12345,
|
||||
}
|
||||
|
||||
await websocket.send(json.dumps(data))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
# asyncio.run(send_ws_message())
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
pydantic
|
||||
grpcio
|
||||
grpcio-tools
|
||||
websockets
|
||||
|
||||
37
src/funnel/ws_server.py
Normal file
37
src/funnel/ws_server.py
Normal file
@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
import json
|
||||
import websockets
|
||||
from websockets import broadcast
|
||||
from websockets.server import WebSocketServerProtocol
|
||||
|
||||
WS_HOST = "localhost"
|
||||
WS_PORT = 5000
|
||||
|
||||
connected_clients = set()
|
||||
|
||||
|
||||
async def ws_handler(websocket: WebSocketServerProtocol):
|
||||
connected_clients.add(websocket)
|
||||
print("Client add: ", websocket)
|
||||
|
||||
try:
|
||||
async for message in websocket:
|
||||
broadcast(connected_clients, message)
|
||||
# await websocket.send(f"Are you talking to me? {message}")
|
||||
finally:
|
||||
connected_clients.remove(websocket)
|
||||
print("Client remove: ", websocket)
|
||||
|
||||
|
||||
async def main():
|
||||
print(f"Starting WS server on {WS_HOST}:{WS_PORT}")
|
||||
async with websockets.serve(
|
||||
ws_handler=ws_handler,
|
||||
host=WS_HOST,
|
||||
port=WS_PORT,
|
||||
):
|
||||
await asyncio.Future() # run forever
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user