Updated server/server.py

Added ws_talk.py
This commit is contained in:
mirai 2022-12-02 08:14:46 +03:00
parent 7544ff8c63
commit 8cf1cb10d9
2 changed files with 16 additions and 8 deletions

View File

@ -2,7 +2,7 @@ import rsa
from server.models import Message
from cryptography.fernet import Fernet
from sanic.response import HTTPResponse
from sanic import Sanic, Request, response
from sanic import Sanic, Request, response, Websocket
app = Sanic("app")
app.config.OAS = False
@ -16,7 +16,8 @@ users: dict[str, str] = {}
key = Fernet.generate_key()
@app.route('/talk', methods=["GET", "POST"])
#@app.route('/talk', methods=["GET", "POST"])
@app.websocket("/talk")
async def talking(request: Request) -> HTTPResponse:
new_message = Message(
message=request.form.get("text")
@ -25,12 +26,19 @@ async def talking(request: Request) -> HTTPResponse:
return response.json({"status": "ok"})
@app.route('/update', methods=["GET", "POST"])
async def talking(request: Request) -> HTTPResponse:
return response.json({
#@app.route('/update', methods=["GET", "POST"])
@app.websocket("/update")
async def talking(request: Request, ws: Websocket) -> HTTPResponse:
while True:
await ws.send({
"status": [i.message for i in actual_messages],
"users_in_chat": list(users.keys())
})
await asyncio.sleep(0.2)
# return response.json({
# "status": [i.message for i in actual_messages],
# "users_in_chat": list(users.keys())
# })
@app.route('/get_key', methods=['GET', 'POST'])

0
ws_talk.py Normal file
View File