2024-08-27 19:44:15 +00:00
|
|
|
import asyncio
|
|
|
|
import socket
|
|
|
|
import websockets
|
|
|
|
import hashlib
|
|
|
|
|
2024-08-27 20:26:35 +00:00
|
|
|
import conn
|
|
|
|
|
2024-08-27 19:44:15 +00:00
|
|
|
HOST = '192.168.21.30'
|
|
|
|
PORT = 6443
|
|
|
|
WEBSOCKET_URL = 'ws://localhost:9999/data'
|
|
|
|
|
|
|
|
async def handle_client(tcpreader, tcpwriter, ws):
|
2024-08-27 20:26:35 +00:00
|
|
|
sockets = {}
|
|
|
|
|
2024-08-27 19:44:15 +00:00
|
|
|
print(f"New client connected: {tcpreader} {tcpwriter}")
|
|
|
|
try:
|
|
|
|
# Forwarding data from client to WebSocket
|
|
|
|
async def tcp_to_websocket():
|
|
|
|
print("tcp_to_websocket...")
|
|
|
|
while True:
|
|
|
|
data = await tcpreader.read(2024)
|
2024-08-27 20:26:35 +00:00
|
|
|
c = conn.Conn(0, data)
|
|
|
|
socketid = c.socketid
|
|
|
|
print(f'TCP@{socketid}>WS: ', hashlib.md5(data).hexdigest())
|
2024-08-27 19:44:15 +00:00
|
|
|
if not data:
|
|
|
|
break
|
2024-08-27 20:26:35 +00:00
|
|
|
await ws.send(c.to_ws_bytes())
|
2024-08-27 19:44:15 +00:00
|
|
|
|
|
|
|
# Forwarding data from WebSocket to client
|
|
|
|
async def websocket_to_tcp():
|
|
|
|
print("websocket_to_tcp...")
|
|
|
|
while True:
|
|
|
|
message = await ws.recv()
|
2024-08-27 20:26:35 +00:00
|
|
|
c = conn.Conn.from_ws_bytes(message)
|
|
|
|
socketid = c.socketid
|
|
|
|
print(f'WS>TCP@{socketid}: ', hashlib.md5(message).hexdigest())
|
|
|
|
tcpwriter.write(c.data)
|
2024-08-27 19:44:15 +00:00
|
|
|
await tcpwriter.drain()
|
|
|
|
|
|
|
|
print("Running both tasks concurrently...")
|
|
|
|
# Run both tasks concurrently
|
|
|
|
await asyncio.gather(tcp_to_websocket(), websocket_to_tcp())
|
|
|
|
print('done')
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(f"ASYNCIOD Error: {e}")
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
|
|
|
|
|
|
|
tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT)
|
|
|
|
|
|
|
|
ws = await websockets.connect(WEBSOCKET_URL)
|
|
|
|
|
|
|
|
await handle_client(tcpreader, tcpwriter, ws)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|