重用Django Channels中的现有websocket [英] Reusing an existing websocket in Django Channels

查看:158
本文介绍了重用Django Channels中的现有websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在半途而废地使用Python在受限访问系统和GAE应用程序之间创建一种网守服务器。在开始讨论与代码相关的特定问题之前,我将首先讨论一个更笼统的问题。



概述



受限访问系统是通过命令行Python应用程序设置的,该应用程序打开了与中间服务器上运行的Django Channels应用程序的WebSocket连接,并开始发送常规的心跳帧,Django应用程序会对此进行确认。到现在为止还挺好。
偶尔,Django应用程序将收到一个HTTP请求,它将进行验证,如果通过,它将向另一个系统发送一条指令。现在,为了使其高效,我想到了重新使用现有WS连接并在其上附带指令框架的想法。



问题



Django允许我轻松地在套接字上接收帧,并对所述帧执行操作。我遇到的问题是在现有管道上发送消息:我一生无法调用 WebsocketConsumer.send()方法,即使通过包装,从使用者中不是 而是在其他地方定义的方法中接收,并以使用者为参数(到目前为止,我能提出的最好的方法)。甚至有可能(此问题

预先感谢任何指针。

解决方案

也许已经来不及了,但是您需要直接将消息发送到应用程序正在收听的频道或组。渠道会将您的消息路由到适当的使用方方法。



因此,假设您想在服务其他应用程序的使用者中调用 send_message 方法(例如, channel1 ):您应该执行以下操作:

 来自channels.layers import get_channel_layer 
从asgiref.sync导入async_to_sync

channel_layer = get_channel_layer()
sync_to_async(channel_layer.send)( channel1,{
type: send.message,
text:你好!,
})

此可以在代码中的任何位置完成。



您链接的问题使用的是龙卷风websocket,与Django渠道大不相同。我不知道使用龙卷风是否是possiblle,但肯定是使用频道


I'm messing around semi-seriously with Python to create a sort of a gatekeeper server between a restricted-access system and a GAE application. I'm going to start with a more general question, before potentially moving on to a specific code-related question.

Overview

The restricted-access system is set up with a command-line Python app that opens a WebSocket connection to the Django Channels app running on an intermediate server, and begins sending out regular heartbeat frames, which the Django app acknowledges. So far so good. Every now and then, the Django app is going to receive an HTTP request, which it will verify, and if it passes, it'll send out an instruction to the other system. Now, in order to make it efficient, I had the idea of re-using the existing WS connection and piggy-back the instruction frame on that.

Question

Django allows me to receive frames on the socket easily, and act on said frames. What I'm having trouble with is sending a message down the existing pipe: I cannot for the life of me call the WebsocketConsumer.send() method, even via a wrapper, from a method that is not in the consumer, but defined elsewhere and receives the consumer as a parameter (the best I could come up with so far). Is it even possible (this question points to it actually not being possible)?

Thanks in advance for any pointers. If need be, tomorrow I can update the question with my classes.

解决方案

Maybe already be too late, but what you need to do is send a message directly to the channel or group the application is listening to. Channels will route your message to the appropriate consumer method.

So assuming you want to call the send_message method in the consumer serving your other app(say channel1): you should do this:

from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync

channel_layer = get_channel_layer()
sync_to_async(channel_layer.send)("channel1", {
    "type": "send.message",
    "text": "Hello there!",
})

This can be done, anywhere in your code.

The question you linked uses tornado websockets which is quite different from Django channels. I don't know if it is possiblle using tornado but it definitely is, using Channels

这篇关于重用Django Channels中的现有websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆