通过 Django 视图将数据发送到 Django 频道组

Sending data to django channels groups via django views

TL;DR

想要这个流程:

                     ws://...
websocket client 1 <-----------> websocket client 2
                         ^
                         |
                       server (send messages via views)

所以我有以下内容:

views.py

def alarm(request):
    layer = get_channel_layer()
    async_to_sync(layer.group_send)('events', {
    'type': 'events.alarm',
    'content': 'triggered'
    })
    return HttpResponse('<p>Done</p>')

consumers.py

class EventConsumer(JsonWebsocketConsumer):
  def connect(self):
    print('inside EventConsumer connect()')
    async_to_sync(self.channel_layer.group_add)(
        'events',
        self.channel_name
    )
    self.accept()

  def disconnect(self, close_code):
    print('inside EventConsumer disconnect()')
    print("Closed websocket with code: ", close_code)
    async_to_sync(self.channel_layer.group_discard)(
        'events',
        self.channel_name
    )
    self.close()

  def receive_json(self, content, **kwargs):
    print('inside EventConsumer receive_json()')
    print("Received event: {}".format(content))
    self.send_json(content)

  def events_alarm(self, event):
    print('inside EventConsumer events_alarm()')
    self.send_json(
        {
            'type': 'events.alarm',
            'content': event['content']
        }
    )

在routing.py

application = ProtocolTypeRouter({
    'websocket': AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                chat.routing.websocket_urlpatterns,

            )
        )
    ),
})

其中 websocket_urlpatterns 是

websocket_urlpatterns = [
url(r'^ws/chat/(?P<room_name>[^/]+)/$', consumers.ChatConsumer),
url(r'^ws/event/$', consumers.EventConsumer),
]

urls.py

urlpatterns = [
    url(r'^alarm/$', alarm, name='alarm'),
]

当我调用 /alarm/ 时,只发出了 HTTP 请求,并没有将消息发送到 websocket

以下是日志:

[2018/09/26 18:59:12] HTTP GET /alarm/ 200 [0.07, 127.0.0.1:60124]

我的目的是让 django 视图发送到一个组(用例是服务器向组中所有连接的成员发送通知)。

我在这里缺少什么设置。

我是 运行 django channels 2.1.3,以 redis 作为后端。 CHANNELS_LAYERS等都设置好了

参考链接:

  1. github issue
  2. channels docs

EDIT: I could send the message using the websocket-client from the view

from websocket import create_connection
ws = create_connection("ws://url")
ws.send("Hello, World")

但是是否可以不使用上述方法发送(不想创建连接)?

源代码:chat-app

感谢@Matthaus Woolard 使概念非常清晰。

这就是问题所在:

The client had disconnected when I tried to send the message from the django view. This happened as the server restarted upon code change. I refreshed the browser and it started to work.

愚蠢的错误

总结一下:

如果是同步消费者,请在 connect() 中添加以下内容:

async_to_sync(self.channel_layer.group_add)('events', self.channel_name)

或者在异步消费者的情况下添加:

await self.channel_layer.group_add('events', self.channel_name)

创建视图如下:

def alarm(request):
   layer = get_channel_layer()
   async_to_sync(layer.group_send)('events', {
           'type': 'events.alarm',
           'content': 'triggered'
        })
   return HttpResponse('<p>Done</p>')