From 53e694be9a3a4c583f9b029563d06852ad6c7305 Mon Sep 17 00:00:00 2001 From: arne Date: Sat, 28 Jan 2023 16:40:46 +0100 Subject: [PATCH] add websocket router and html/js test client --- app.py | 9 +++++++++ docs/client.html | 4 ++-- docs/echo.html | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 docs/echo.html diff --git a/app.py b/app.py index 7e528e0..b7ba735 100644 --- a/app.py +++ b/app.py @@ -1,10 +1,12 @@ from flask import Flask, render_template, url_for, request, redirect, Response, abort, session +from flask_sock import Sock from flask_sqlalchemy import SQLAlchemy from werkzeug.middleware.proxy_fix import ProxyFix from datetime import datetime import magic import random import string +import json app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1) @@ -12,6 +14,7 @@ app.secret_key = '029c0gji3jfo3o8h938vhwtfmh3t39th' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///cloudvars.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) +sock = Sock(app) class variables(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) @@ -25,6 +28,12 @@ def get_date(): date = datetime.now() return { "now": date.strftime("%Y-%m-%d") } +@sock.route('/') +def echo(sock,namespace): + while True: + data = json.loads(sock.receive()) + sock.send(json.dumps({"method":"set","name":"test","value":"abcd"})) + @app.route('/', methods = ['GET']) def index(): return render_template('index.html') diff --git a/docs/client.html b/docs/client.html index 078d42d..56d430c 100644 --- a/docs/client.html +++ b/docs/client.html @@ -3,8 +3,8 @@
+ + + \ No newline at end of file