102 lines
3.7 KiB
Python
102 lines
3.7 KiB
Python
import pydash
|
|
from asgiref.sync import async_to_sync
|
|
from channels.exceptions import StopConsumer
|
|
from channels.generic.websocket import JsonWebsocketConsumer
|
|
from channels.layers import get_channel_layer
|
|
|
|
from sixGuoDjango import redis_5
|
|
|
|
# channel_layer: RedisChannelLayer = get_channel_layer("default")
|
|
channel_layer = get_channel_layer(alias="default")
|
|
default_group_name = "default_group_name"
|
|
|
|
|
|
class websocket(JsonWebsocketConsumer):
|
|
|
|
def connect(self) -> None:
|
|
"""
|
|
首次连拉时触发方法前端请求的语法为 "wss://wecom.fujugongxiang.com/web/room_name_value/"
|
|
room_name_value: 就是前端路由配置的room_name的值 path("web/<str:room_name>/", tableConsumer.websocket.as_asgi()),
|
|
并且最末尾要带有 / 斜杠
|
|
"""
|
|
user_openid = self.scope["url_route"]["kwargs"].get("room_name", "")
|
|
print("websocket:=>", user_openid, self.channel_name)
|
|
if not user_openid:
|
|
self.close(code=4001) # 关闭连接(可选)
|
|
return
|
|
self.accept()
|
|
# 将进来的用户保存到redis数据库中
|
|
redis_5.set(value=self.channel_name, name=user_openid)
|
|
# 将用户添加到自己的组里面 (只能异步,否则添加不成功的)
|
|
self.send_json(content={"channel_name": self.channel_name})
|
|
async_to_sync(awaitable=self.channel_layer.group_add)(group=default_group_name, channel=self.channel_name)
|
|
|
|
def disconnect(self, ):
|
|
user_openid = self.scope["url_route"]["kwargs"].get("room_name", None)
|
|
async_to_sync(self.channel_layer.group_discard)(group=user_openid, channel=self.channel_name, ) # 只能异步的删除;
|
|
async_to_sync(self.channel_layer.group_discard)(group=default_group_name, channel=self.channel_name, )
|
|
raise StopConsumer()
|
|
|
|
def receive_json(self, content, **kwargs) -> None:
|
|
print(content, "是在这里吗")
|
|
# self.send()
|
|
self.send_json(content={"xiao": "xiaozizi"})
|
|
pass
|
|
|
|
def chat_messages(self, event) -> None:
|
|
"""
|
|
:param event:
|
|
"""
|
|
data = pydash.get(obj=event, path="message")
|
|
self.send_json(content="messagemessagemessagemessage")
|
|
|
|
def chat_subMsg(self, event):
|
|
"""
|
|
|
|
:param event:
|
|
:type event:
|
|
"""
|
|
# 处理消息回调的函数
|
|
print("事件没有调用了吗")
|
|
self.send_json(content=event)
|
|
|
|
def chat_hongBao(self, event):
|
|
"""
|
|
:param event:
|
|
:type event:
|
|
"""
|
|
# 处理首次领红包的业务
|
|
user_openid = self.scope["url_route"]["kwargs"].get("room_name", None)
|
|
print(user_openid)
|
|
print("我得调用一下:chat_hongBao")
|
|
channelID = pydash.get(obj=event, path="message.channelID")
|
|
self.send_json(content={"chat": "chat_hongBao", })
|
|
|
|
def chat_Lucky拼团(self, event):
|
|
print("socked事件调用幸运拼团数据更新: 🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏", event)
|
|
self.send_json(content=event)
|
|
|
|
def commandsSendStr(self, event):
|
|
"""
|
|
:param event:
|
|
:type event:
|
|
"""
|
|
self.send_json(content={"commands": "demo这是个测试的数据", "type": "commands"})
|
|
|
|
def chat_demo(self, event):
|
|
"""
|
|
:param event:
|
|
:type event:
|
|
"""
|
|
print(event, "函数被调用了,")
|
|
self.send_json(content={"demo": "demo_test"})
|
|
|
|
def 消息提示(self, event):
|
|
"""事件"""
|
|
print(event)
|
|
print("这只是个用来测试的方法")
|
|
self.send_json(content="这只是个用来测试的方法")
|
|
|
|
def update_更新用户头像(self, event):
|
|
print(event)
|