add simple multiclient websocket server

This commit is contained in:
randomuser 2023-05-06 21:04:47 -05:00
parent bc22af3997
commit c16e50a645
2 changed files with 32 additions and 0 deletions

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
debug:
flask --app secmsg run --debug
prod:
gunicorn -b :5000 --threads 128 secmsg:app

27
secmsg/__init__.py Normal file
View File

@ -0,0 +1,27 @@
import os
from flask import Flask
from flask_sock import Sock
app = Flask(__name__, instance_relative_config=True)
sock = Sock(app)
clients = []
@app.route('/')
def index():
return "test"
@sock.route('/client')
def sock_handler(ws):
clients.append(ws)
try:
while True:
data = ws.receive()
for client in clients:
if not client is ws:
client.send(data)
except simple_websocket.ws.ConnectionClosed:
clients.remove(ws)