mirror of
https://github.com/welton89/RRBEC.git
synced 2026-04-05 13:35:42 +00:00
bug: envio msg para servidor wesocket
This commit is contained in:
@@ -9,21 +9,24 @@ from products.models import Product
|
|||||||
from payments.models import Payments
|
from payments.models import Payments
|
||||||
from typePay.models import TypePay
|
from typePay.models import TypePay
|
||||||
from gestaoRaul.decorators import group_required
|
from gestaoRaul.decorators import group_required
|
||||||
|
from websocket_client.websocketClient import enviar_mensagem
|
||||||
|
|
||||||
import asyncio
|
from asgiref.sync import async_to_sync
|
||||||
import websockets
|
# import asyncio
|
||||||
|
|
||||||
async def enviar_mensagem(msg):
|
# import websockets
|
||||||
try:
|
|
||||||
uri = "ws://websocket_server:8765"
|
|
||||||
async with websockets.connect(uri) as websocket:
|
|
||||||
await websocket.send(msg)
|
|
||||||
# print(f"> Enviado: {msg}")
|
|
||||||
|
|
||||||
# resposta = await websocket.recv()
|
# async def enviar_mensagem(msg):
|
||||||
# print(f"< Recebido: {resposta}")
|
# try:
|
||||||
except Exception as e:
|
# uri = "ws://websocket_server:8765"
|
||||||
print(f"Erro ao enviar mensagem via websocket: {e}")
|
# async with websockets.connect(uri) as websocket:
|
||||||
|
# await websocket.send(msg)
|
||||||
|
# # print(f"> Enviado: {msg}")
|
||||||
|
|
||||||
|
# # resposta = await websocket.recv()
|
||||||
|
# # print(f"< Recebido: {resposta}")
|
||||||
|
# except Exception as e:
|
||||||
|
# print(f"Erro ao enviar mensagem via websocket: {e}")
|
||||||
|
|
||||||
|
|
||||||
def somar(consumo:ProductComanda, comanda:Comanda):
|
def somar(consumo:ProductComanda, comanda:Comanda):
|
||||||
@@ -84,8 +87,16 @@ def addProduct(request, product_id, comanda_id):
|
|||||||
'id':order.id,
|
'id':order.id,
|
||||||
'speak': f'Novo pedido! {product.name}, para {comanda.name}.'
|
'speak': f'Novo pedido! {product.name}, para {comanda.name}.'
|
||||||
})
|
})
|
||||||
|
try:
|
||||||
asyncio.run(enviar_mensagem(msg))
|
# Chama a função async dentro da view normal
|
||||||
|
async_to_sync(enviar_mensagem)(mensagem)
|
||||||
|
|
||||||
|
# return JsonResponse({"status": "Mensagem enviada com sucesso"})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print("Erro add product websocket: ",e)
|
||||||
|
# return JsonResponse({"status": "Erro", "erro": str(e)}, status=500)
|
||||||
|
# asyncio.run(enviar_mensagem(msg))
|
||||||
consumo = ProductComanda.objects.filter(comanda=comanda_id)
|
consumo = ProductComanda.objects.filter(comanda=comanda_id)
|
||||||
valores = somar(consumo,comanda)
|
valores = somar(consumo,comanda)
|
||||||
|
|
||||||
|
|||||||
30
gestaoRaul/websocket_client/websocketClient.py
Normal file
30
gestaoRaul/websocket_client/websocketClient.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import websockets
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
|
async def enviar_mensagem(msg):
|
||||||
|
uri = "ws://websocket_server:8765"
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with websockets.connect(uri) as websocket:
|
||||||
|
# Garante que msg seja JSON
|
||||||
|
if isinstance(msg, dict):
|
||||||
|
mensagem_json = json.dumps(msg)
|
||||||
|
else:
|
||||||
|
mensagem_json = str(msg)
|
||||||
|
|
||||||
|
await websocket.send(mensagem_json)
|
||||||
|
print(f"> Enviado: {mensagem_json}")
|
||||||
|
|
||||||
|
# Se quiser esperar uma resposta (opcional)
|
||||||
|
# resposta = await websocket.recv()
|
||||||
|
# print(f"< Recebido: {resposta}")
|
||||||
|
|
||||||
|
except websockets.exceptions.InvalidURI as e:
|
||||||
|
print(f"URI inválida: {e}")
|
||||||
|
except websockets.exceptions.InvalidHandshake as e:
|
||||||
|
print(f"Handshake WebSocket falhou: {e}")
|
||||||
|
except ConnectionRefusedError:
|
||||||
|
print("Conexão recusada. Verifique se o servidor WebSocket está rodando.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Erro ao enviar mensagem via websocket: {e}")
|
||||||
Reference in New Issue
Block a user