websocket & change broker decimal to int
This commit is contained in:
0
apps/authentication/websocket/__init__.py
Normal file
0
apps/authentication/websocket/__init__.py
Normal file
30
apps/authentication/websocket/consumer.py
Normal file
30
apps/authentication/websocket/consumer.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from channels.generic.websocket import AsyncWebsocketConsumer
|
||||
from apps.product.models import SaleUnit
|
||||
from asgiref.sync import sync_to_async
|
||||
import json
|
||||
|
||||
|
||||
@sync_to_async
|
||||
def get_unit(unit_id=None):
|
||||
unit = SaleUnit.objects.get(id=int(unit_id))
|
||||
return unit
|
||||
|
||||
|
||||
class MyConsumer(AsyncWebsocketConsumer):
|
||||
async def connect(self):
|
||||
await self.accept()
|
||||
await self.send(text_data=json.dumps({"message": "Connected!"}))
|
||||
|
||||
async def disconnect(self, close_code):
|
||||
print("WebSocket disconnected!")
|
||||
|
||||
async def receive(self, text_data):
|
||||
data = json.loads(text_data)
|
||||
message = data.get("message")
|
||||
unit = await get_unit(int(message))
|
||||
print("Received:", message)
|
||||
|
||||
# پاسخ به کلاینت
|
||||
await self.send(text_data=json.dumps({
|
||||
"message": unit
|
||||
}))
|
||||
6
apps/authentication/websocket/routing.py
Normal file
6
apps/authentication/websocket/routing.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.urls import re_path
|
||||
from apps.authentication.websocket import consumer
|
||||
|
||||
websocket_urlpatterns = [
|
||||
re_path(r"ws/somepath/$", consumer.MyConsumer.as_asgi()),
|
||||
]
|
||||
Reference in New Issue
Block a user