Cleanups and optimizations
This commit is contained in:
@ -61,6 +61,7 @@ deactivate FE
|
|||||||
| Funnel | Python | - |
|
| Funnel | Python | - |
|
||||||
| ServeCurrentTime | NodeJS | 50000 |
|
| ServeCurrentTime | NodeJS | 50000 |
|
||||||
| ServeSegments | Go | 50001 |
|
| ServeSegments | Go | 50001 |
|
||||||
|
| WS Server | Go | 50010 |
|
||||||
|
|
||||||
### Funnel
|
### Funnel
|
||||||
|
|
||||||
|
|||||||
@ -16,8 +16,11 @@
|
|||||||
</body>
|
</body>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
|
const WS_HOST = "localhost";
|
||||||
|
const WS_PORT = 50010;
|
||||||
|
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
const ws = new WebSocket('ws://localhost:5000');
|
const ws = new WebSocket(`ws://${WS_HOST}:${WS_PORT}`);
|
||||||
|
|
||||||
ws.onmessage = function (event) {
|
ws.onmessage = function (event) {
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(event.data);
|
||||||
|
|||||||
@ -5,6 +5,10 @@ run:
|
|||||||
@source env/bin/activate && \
|
@source env/bin/activate && \
|
||||||
python main.py
|
python main.py
|
||||||
|
|
||||||
|
run-ws:
|
||||||
|
@source env/bin/activate && \
|
||||||
|
python ws_server.py
|
||||||
|
|
||||||
proto:
|
proto:
|
||||||
@source env/bin/activate && \
|
@source env/bin/activate && \
|
||||||
python \
|
python \
|
||||||
|
|||||||
@ -2,11 +2,11 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from typing import AsyncGenerator, Optional
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
import grpc
|
import grpc
|
||||||
import websockets
|
import websockets
|
||||||
from websockets.server import WebSocketServerProtocol
|
from websockets.legacy.client import connect as ws_connect
|
||||||
|
|
||||||
from stubs.serve_segments_pb2 import (
|
from stubs.serve_segments_pb2 import (
|
||||||
GetHoursRequest,
|
GetHoursRequest,
|
||||||
@ -25,7 +25,7 @@ SERVE_CURRENTTIME_PORT = 50000
|
|||||||
SERVE_SEGMENTS_HOST = "localhost"
|
SERVE_SEGMENTS_HOST = "localhost"
|
||||||
SERVE_SEGMENTS_PORT = 50001
|
SERVE_SEGMENTS_PORT = 50001
|
||||||
WS_HOST = "localhost"
|
WS_HOST = "localhost"
|
||||||
WS_PORT = 5000
|
WS_PORT = 50010
|
||||||
|
|
||||||
TIMEZONE = "Europe/Zagreb"
|
TIMEZONE = "Europe/Zagreb"
|
||||||
|
|
||||||
@ -39,26 +39,26 @@ async def get_serve_segments_stub() -> AsyncGenerator[ServeSegmentsStub, None]:
|
|||||||
yield ServeSegmentsStub(channel)
|
yield ServeSegmentsStub(channel)
|
||||||
|
|
||||||
|
|
||||||
async def get_hours(stub: ServeSegmentsStub) -> Optional[int]:
|
async def get_hours(stub: ServeSegmentsStub) -> int:
|
||||||
response: GetHoursResponse = await stub.GetHours(GetHoursRequest(timezone=TIMEZONE))
|
response: GetHoursResponse = await stub.GetHours(GetHoursRequest(timezone=TIMEZONE))
|
||||||
return response.hours
|
return response.hours
|
||||||
|
|
||||||
|
|
||||||
async def get_minutes(stub: ServeSegmentsStub) -> Optional[int]:
|
async def get_minutes(stub: ServeSegmentsStub) -> int:
|
||||||
response: GetMinutesResponse = await stub.GetMinutes(
|
response: GetMinutesResponse = await stub.GetMinutes(
|
||||||
GetMinutesRequest(timezone=TIMEZONE)
|
GetMinutesRequest(timezone=TIMEZONE)
|
||||||
)
|
)
|
||||||
return response.minutes
|
return response.minutes
|
||||||
|
|
||||||
|
|
||||||
async def get_seconds(stub: ServeSegmentsStub) -> Optional[int]:
|
async def get_seconds(stub: ServeSegmentsStub) -> int:
|
||||||
response: GetSecondsResponse = await stub.GetSeconds(
|
response: GetSecondsResponse = await stub.GetSeconds(
|
||||||
GetSecondsRequest(timezone=TIMEZONE)
|
GetSecondsRequest(timezone=TIMEZONE)
|
||||||
)
|
)
|
||||||
return response.seconds
|
return response.seconds
|
||||||
|
|
||||||
|
|
||||||
async def get_milliseconds(stub: ServeSegmentsStub) -> Optional[int]:
|
async def get_milliseconds(stub: ServeSegmentsStub) -> int:
|
||||||
response: GetMillisecondsResponse = await stub.GetMilliseconds(
|
response: GetMillisecondsResponse = await stub.GetMilliseconds(
|
||||||
GetMillisecondsRequest(timezone=TIMEZONE)
|
GetMillisecondsRequest(timezone=TIMEZONE)
|
||||||
)
|
)
|
||||||
@ -67,7 +67,7 @@ async def get_milliseconds(stub: ServeSegmentsStub) -> Optional[int]:
|
|||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
ws_uri = f"ws://{WS_HOST}:{WS_PORT}"
|
ws_uri = f"ws://{WS_HOST}:{WS_PORT}"
|
||||||
async with websockets.connect(uri=ws_uri) as websocket:
|
async with ws_connect(uri=ws_uri) as websocket:
|
||||||
async with get_serve_segments_stub() as stub:
|
async with get_serve_segments_stub() as stub:
|
||||||
while True:
|
while True:
|
||||||
t = time.perf_counter()
|
t = time.perf_counter()
|
||||||
@ -106,22 +106,8 @@ async def main():
|
|||||||
await websocket.send(json.dumps(data))
|
await websocket.send(json.dumps(data))
|
||||||
await websocket.recv()
|
await websocket.recv()
|
||||||
|
|
||||||
# time.sleep(0.5)
|
|
||||||
|
|
||||||
print(f"RESULT: {formatted_time}, T: {comm_cycle}")
|
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__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
# asyncio.run(send_ws_message())
|
|
||||||
|
|||||||
@ -1,18 +1,17 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
from websockets.legacy.protocol import broadcast
|
||||||
import websockets
|
from websockets.legacy.server import serve as ws_serve
|
||||||
from websockets import broadcast
|
|
||||||
from websockets.server import WebSocketServerProtocol
|
from websockets.server import WebSocketServerProtocol
|
||||||
|
|
||||||
WS_HOST = "localhost"
|
WS_HOST = "localhost"
|
||||||
WS_PORT = 5000
|
WS_PORT = 50010
|
||||||
|
|
||||||
connected_clients = set()
|
connected_clients = set()
|
||||||
|
|
||||||
|
|
||||||
async def ws_handler(websocket: WebSocketServerProtocol):
|
async def ws_handler(websocket: WebSocketServerProtocol):
|
||||||
connected_clients.add(websocket)
|
connected_clients.add(websocket)
|
||||||
print("Client add: ", websocket)
|
print("Client added: ", websocket)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
@ -20,12 +19,12 @@ async def ws_handler(websocket: WebSocketServerProtocol):
|
|||||||
# await websocket.send(f"Are you talking to me? {message}")
|
# await websocket.send(f"Are you talking to me? {message}")
|
||||||
finally:
|
finally:
|
||||||
connected_clients.remove(websocket)
|
connected_clients.remove(websocket)
|
||||||
print("Client remove: ", websocket)
|
print("Client removed: ", websocket)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
print(f"Starting WS server on {WS_HOST}:{WS_PORT}")
|
print(f"Starting WS server on {WS_HOST}:{WS_PORT}")
|
||||||
async with websockets.serve(
|
async with ws_serve(
|
||||||
ws_handler=ws_handler,
|
ws_handler=ws_handler,
|
||||||
host=WS_HOST,
|
host=WS_HOST,
|
||||||
port=WS_PORT,
|
port=WS_PORT,
|
||||||
|
|||||||
Reference in New Issue
Block a user