Updated server/server.py
Added ws_talk.py
This commit is contained in:
parent
7544ff8c63
commit
8cf1cb10d9
|
|
@ -2,7 +2,7 @@ import rsa
|
||||||
from server.models import Message
|
from server.models import Message
|
||||||
from cryptography.fernet import Fernet
|
from cryptography.fernet import Fernet
|
||||||
from sanic.response import HTTPResponse
|
from sanic.response import HTTPResponse
|
||||||
from sanic import Sanic, Request, response
|
from sanic import Sanic, Request, response, Websocket
|
||||||
|
|
||||||
app = Sanic("app")
|
app = Sanic("app")
|
||||||
app.config.OAS = False
|
app.config.OAS = False
|
||||||
|
|
@ -16,7 +16,8 @@ users: dict[str, str] = {}
|
||||||
key = Fernet.generate_key()
|
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:
|
async def talking(request: Request) -> HTTPResponse:
|
||||||
new_message = Message(
|
new_message = Message(
|
||||||
message=request.form.get("text")
|
message=request.form.get("text")
|
||||||
|
|
@ -25,12 +26,19 @@ async def talking(request: Request) -> HTTPResponse:
|
||||||
return response.json({"status": "ok"})
|
return response.json({"status": "ok"})
|
||||||
|
|
||||||
|
|
||||||
@app.route('/update', methods=["GET", "POST"])
|
#@app.route('/update', methods=["GET", "POST"])
|
||||||
async def talking(request: Request) -> HTTPResponse:
|
@app.websocket("/update")
|
||||||
return response.json({
|
async def talking(request: Request, ws: Websocket) -> HTTPResponse:
|
||||||
|
while True:
|
||||||
|
await ws.send({
|
||||||
"status": [i.message for i in actual_messages],
|
"status": [i.message for i in actual_messages],
|
||||||
"users_in_chat": list(users.keys())
|
"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'])
|
@app.route('/get_key', methods=['GET', 'POST'])
|
||||||
|
|
|
||||||
0
ws_talk.py
Normal file
0
ws_talk.py
Normal file
Loading…
Reference in New Issue
Block a user