Quart,聊天服务器示例
Posted
技术标签:
【中文标题】Quart,聊天服务器示例【英文标题】:Quart, chat server example 【发布时间】:2021-11-13 12:22:59 【问题描述】:我正在尝试同时使用多个 websocket 制作一个 quart 应用程序:
<script type="text/javascript">
let socket = new WebSocket('ws://localhost:5000/mensagens/ dialog_id ');
socket.onmessage = function(event)
var messages_dom = document.getElementsByTagName('ul')[0];
var message_dom = document.createElement('li');
var cotent_dom = document.createTextNode(event.data);
message_dom.appendChild(cotent_dom);
messages_dom.appendChild(message_dom);
;
</script>
<script>
let ws = new WebSocket('ws://localhost:5000/printar/12');
function myFunction()
var x = document.getElementById("myText").value;
ws.send(x);
document.getElementById("demo").innerhtml = x;
;
</script>
这是服务器端:
#Quart
from quart import Quart, render_template, url_for, websocket, redirect, request
#asyncio
import asyncio
app = Quart(__name__)
...
#websocket da conversa individual
@app.websocket('/mensagens/<dialog_id>')
async def mensagens(dialog_id):
print("123");
try:
output = await ".....function that updates from the SQLite database....";
await websocket.send(f"output");
await asyncio.sleep(1);
except Exception as e:
print("-------");
print(e);
#websocket de enviar mensagens
@app.websocket('/printar/<dialog_id>')
async def printar(dialog_id):
print("aqui");
try:
while True:
print(dialog_id);
data = await websocket.receive();
print(data + "\n=====");
except Exception as e:
print(e);
if __name__ == "__main__":
try:
app.run();
except KeyboardInterrupt:
print("=====\nAdeus!\n=====");
except Exception as e:
print(e);
但是,出于某种原因,第二个 websocket 仅在第一个 websocket 收到来自“mensagens”的更新后才开始运行。我真的不明白发生了什么以及为什么会发生。
有没有我可以看的用 Quart 制作的聊天服务器的例子?
【问题讨论】:
【参考方案1】:所以...我是白痴。我刚刚找到this,其中解释了接收和发送的用户独立(另外,我对 asyncio 不太擅长)。
不管怎样,这里有一个例子:
<!doctype html>
<html>
<head>
<title>Quart + SQLite</title>
</head>
<body>
<h1>Testes!</h1>
<div class="mensagens">
<ul></ul>
</div>
Mensagem: <input type="text" id="myText" value="Mickey">
<p>Escreva suas mensagens acima!</p>
<button onclick="myFunction()">Enviar</button>
<p id="demo"></p>
<script>
let socket = new WebSocket('ws://localhost:5000/ws/12');
function myFunction()
var x = document.getElementById("myText").value;
socket.send(x);
document.getElementById("demo").innerHTML = x;
;
socket.onmessage = function(event)
var messages_dom = document.getElementsByTagName('ul')[0];
var message_dom = document.createElement('li');
var cotent_dom = document.createTextNode(event.data);
message_dom.appendChild(cotent_dom);
messages_dom.appendChild(message_dom);
;
</script>
</body>
</html>
这里是python方面:
#https://pgjones.gitlab.io/quart/how_to_guides/websockets.html
#Quart
from quart import Quart, render_template, url_for, websocket, redirect, request
import asyncio
app = Quart(__name__)
async def sending():
while True:
await websocket.send('f1');
asyncio.sleep(1);
async def receiving():
while True:
data = await websocket.receive();
print(data + "\n=====");
@app.websocket('/ws/<num>')
async def ws(num):
print(num);
producer = asyncio.create_task(sending());
consumer = asyncio.create_task(receiving());
await asyncio.gather(producer, consumer);
@app.route('/')
async def main():
return await render_template('teste_html.html');
if __name__ == '__main__':
try:
app.run();
except KeyboardInterrupt:
print("=====\nAdeus!\n=====");
except Exception as e:
print(e);
【讨论】:
以上是关于Quart,聊天服务器示例的主要内容,如果未能解决你的问题,请参考以下文章