问题描述
我想实现一个WebSocket,它每120秒执行一次查询集并根据某种逻辑(已经存在)返回一个响应
class CarConsumer(JsonWebsocketConsumer):
def connect(self):
# Manually adding a user for Now,later from the url route.
self.user_name = '1'
self.user_group_name = 'user_%s' % self.user_name
self.group = [self.user_group_name,self.channel_name]
# Join user group
async_to_sync(self.channel_layer.group_add)(
self.group[0],self.group[1]
)
# Todo: Once working,add Auth
# accept connection
self.accept()
def car(self):
result = self.check_events() # Get updated car status
self.channel_layer.send(
self.farm_group_name,# send result to group
{'type': 'check_events','message': result
}
)
def check_events(self):
Car.objects.filter()
# rest of logic cut dows due to length.
因此,此代码在使用时会连接:
const ws = new WebSocket("ws://127.0.0.1:8000/ws/car_online/connect/");
已建立连接,但我似乎无法发送正确的消息,除此之外,我希望check_events方法(逻辑)每120秒运行一次,如何实现此行为?
如果更改了某些事件,则检查事件方法的结果始终是字典。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)