Additional Tryes
This commit is contained in:
parent
30df8f46d1
commit
500cc003fe
12
app.py
12
app.py
@ -11,6 +11,8 @@ import asyncio
|
||||
import websockets
|
||||
import hashlib
|
||||
import base64
|
||||
from websockets.server import WebSocketServerProtocol
|
||||
|
||||
# Define the target server to proxy requests to
|
||||
class ProxyHandler(http.server.BaseHTTPRequestHandler):
|
||||
def __init__(self, configuration, docker_client):
|
||||
@ -138,21 +140,24 @@ class ProxyHandler(http.server.BaseHTTPRequestHandler):
|
||||
server_ws = None
|
||||
try:
|
||||
client_connection = self.connection
|
||||
|
||||
# Establish server connection to backend
|
||||
server_ws = await websockets.connect(f"ws://{target_host}:{target_port}")
|
||||
|
||||
print("connected")
|
||||
# Bridge function to handle message forwarding
|
||||
async def bridge_websockets():
|
||||
try:
|
||||
while True:
|
||||
# Wait for a message from the client
|
||||
client_message = await client_connection.recv()
|
||||
print(f">: {client_message}")
|
||||
# Send it to the server
|
||||
await server_ws.send(client_message)
|
||||
# Wait for a message from the server
|
||||
server_message = await server_ws.recv()
|
||||
# Send it to the client
|
||||
await client_connection.send(server_message)
|
||||
|
||||
except websockets.exceptions.ConnectionClosed as e:
|
||||
print(f"WebSocket connection closed: {e}")
|
||||
except Exception as e:
|
||||
@ -181,6 +186,11 @@ class ProxyHandler(http.server.BaseHTTPRequestHandler):
|
||||
self.send_header("Sec-WebSocket-Accept", accept_val)
|
||||
self.end_headers()
|
||||
|
||||
# Upgrade the connection to a WebSocket connection
|
||||
self.websocket = WebSocketServerProtocol()
|
||||
self.websocket.connection_made(self.connection)
|
||||
self.websocket.connection_open()
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
threading.Thread(target=loop.run_until_complete, args=(self.websocket_proxy(target_host, target_port),)).start()
|
||||
|
||||
|
12
config.yml
12
config.yml
@ -3,9 +3,15 @@ proxy_hosts:
|
||||
- domain: ws.local
|
||||
containers:
|
||||
- container_name: web-socket-test
|
||||
- container_name: web-socket-test
|
||||
|
||||
proxy_host: localhost
|
||||
proxy_host: 10.201.0.128
|
||||
proxy_port: 8010
|
||||
proxy_timeout_seconds: 10
|
||||
proxy_load_seconds: 5
|
||||
|
||||
- domain: wp.local
|
||||
containers:
|
||||
- container_name: db
|
||||
proxy_host: 10.201.0.128
|
||||
proxy_port: 8888
|
||||
proxy_timeout_seconds: 10
|
||||
proxy_load_seconds: 5
|
13
tests.py
Normal file
13
tests.py
Normal file
@ -0,0 +1,13 @@
|
||||
import asyncio
|
||||
import time
|
||||
import websockets
|
||||
|
||||
async def main():
|
||||
async with websockets.connect('ws://localhost:8010') as ws:
|
||||
while True:
|
||||
await ws.send("testsage")
|
||||
server_message = ws.recv()
|
||||
print(server_message)
|
||||
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(main())
|
Loading…
x
Reference in New Issue
Block a user