- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我完全陷入无法使用 Channels 2 进行群组消息传递的困境!我已经按照我能找到的所有教程和文档进行操作,但遗憾的是我还没有找到问题所在。我现在想做的是拥有一个特定的 URL,当访问该 URL 时应该向名为“events”的组广播一条简单的消息。
首先,这里是我在 Django 中使用的相关和当前设置:
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('localhost', 6379)],
},
}
}
ASGI_APPLICATION = 'backend.routing.application'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'channels',
'channels_redis',
'backend.api'
]
接下来,这是我的 EventConsumer,它以非常基本的方式扩展了 JsonWebsocketConsumer。所有这一切都是在收到消息时回显,这很有效!因此,简单的 send_json 响应如期到达,只有群组广播不起作用。
class EventConsumer(JsonWebsocketConsumer):
groups = ["events"]
def connect(self):
self.accept()
def disconnect(self, close_code):
print("Closed websocket with code: ", close_code)
self.close()
def receive_json(self, content, **kwargs):
print("Received event: {}\nFrom: {}\nGroups:
{}".format(content,
self.channel_layer,
self.groups))
self.send_json(content)
def event_notification(self, event):
self.send_json(
{
'type': 'test',
'content': event
}
)
这是我要触发广播的 URL 的 URL 配置:
项目 urls.py
from backend.events import urls as event_urls
urlpatterns = [
url(r'^events/', include(event_urls))
]
事件应用 urls.py
from backend.events.views import alarm
urlpatterns = [
url(r'alarm', alarm)
]
最后,群组广播应该发生的 View 本身:
from django.shortcuts import HttpResponse
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
def alarm(req):
layer = get_channel_layer()
async_to_sync(layer.group_send)('events', {'type': 'test'})
return HttpResponse('<p>Done</p>')
最佳答案
我在写这个问题时找到了解决方案,并认为其他人也可以使用它!由于此处的大多数问题都是关于 2.0 及更高版本之前的 channel 版本,因此这就是您应该如何处理消费者中的 group_send 事件。
问题不仅在于我如何使用 group_send
函数,我错误地认为将 groups 类变量添加到我的 EventConsumer 应该会自动将它添加到那个/那些组,它确实不是!您必须在 connect
类函数中手动添加组,并在 disconnect
函数中删除组!
问题还在于我的消费者没有指定适当的事件处理程序。在接收警报请求的 View 文件中,我将“类型”设置为“测试”。测试未反射(reflect)在我的 EventConsumer 类中,因此无法处理该事件。如多聊天示例中所述 here在第 146 行,根据发送的事件类型调用辅助函数。因此,'event.alarm' 事件类型在您的消费者中应该具有相应的 event_alarm
功能!简单,但没有很好的记录:)。这是最终解决方案的样子:
在consumers.py
中,注意connect中的group_add
和disconnect中的group_discard
!
class EventConsumer(JsonWebsocketConsumer):
def connect(self):
async_to_sync(self.channel_layer.group_add)(
'events',
self.channel_name
)
self.accept()
def disconnect(self, close_code):
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("Received event: {}".format(content))
self.send_json(content)
# ------------------------------------------------------------------------------------------------------------------
# Handler definitions! handlers will accept their corresponding message types. A message with type event.alarm
# has to have a function event_alarm
# ------------------------------------------------------------------------------------------------------------------
def events_alarm(self, event):
self.send_json(
{
'type': 'events.alarm',
'content': event['content']
}
)
因此,上面的函数 events_alarm
从以下 group_send
调用:
from django.shortcuts import HttpResponse
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
def alarm(req):
layer = get_channel_layer()
async_to_sync(layer.group_send)('events', {
'type': 'events.alarm',
'content': 'triggered'
})
return HttpResponse('<p>Done</p>')
如果您需要对问题/答案进行更多说明,请告诉我!干杯!
关于django - 在 Django Channels 2 中向群组发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855417/
我很好奇为什么以下不起作用。一般select用default:防止死锁,但在这种情况下不是: package main import "fmt" func main () { a := mak
我一生都无法弄清楚如何切换图像排序。图像以 (x,x,3) 格式读取,theano 要求它是 (3,x,x) 格式。我尝试更改顺序numpy.array([img[:,:,i] for i in ra
我正在向 libnl 发送单个 SSID 和频率进行扫描,但我得到了多个扫描结果以及我请求的 SSID 和频率,但我需要单个扫描结果(仅适用于请求的 SSID),如何实现这一点。请帮助我,我也发送了我
我是 Golang 的新手,但正在努力理解这门伟大的语言!请帮帮我.. 我有 2 个 channel 。 “进”和“出” channel in, out := make(chan Work),
例如我有这段代码: package main import ( "fmt" ) func main() { c1 := make(chan interface{}) close
我们使用以下调用来获取经过身份验证的用户的 ChannelID,它适用于大多数情况。一些 YouTube 用户将他们的 channel 连接到 Google+ 信息页,但在这种情况下,我们的一位用户无
case 'sinfo': const sinfo = new Discord.MessageEmbed() .addField('Server Name 🔎 :', message.guild.n
我需要让所有 channel 来创建一个 bunker 命令,这使得所有 channel 都是只读的。 最佳答案 他们变了Client.servers至 Client.guilds在 newer ve
为什么当第二个值通过另一个 go routine 发送并且没有收到发送的第一个值时, channel c 没有缓冲? package main import "fmt" func sum(s []in
据我所知,内置的 split 会将一个 3 channel Mat 拆分为三个 1 channel Mat。结果,这三个 Mat 只是具有一些不同强度的灰度。 我的意图是获得三个 3 channel
如何检测当前的 RAM 配置?我需要询问 Windows RAM 当前是在单 channel 、双 channel 还是四 channel 中运行。 我搜索了很多,并没有在这个网站或其他网站上找到任何
我需要拆分一个多 channel wav 文件并将每个 channel 编码为 mp3 文件。 我知道 gtresamer 的 deinterleave 插件,但我不确定如何将它用于 wav 文件以及
关闭。这个问题需要details or clarity .它目前不接受答案。 想要改进这个问题吗? 通过 editing this post 添加详细信息并澄清问题. 关闭 8 年前。 Improve
我正在尝试运行 Hyperledger Fabric 网络,它由单个订购者、单个对等节点和一个 cli 组成。为了学习启动 Hyperledger Fabric 网络的过程,从创建与加密相关的工件到将
我在 Laravel 中使用事件广播。我正在使用基于角色的通知访问权限。我有用于广播的自定义 auth guard。当用户连接到 channel 时,客户端将具有内部权限的 access_token
我正在编写一个使用 Elixir Channels 来处理实时事件的应用程序。我知道每个客户端将打开 1 个套接字,并且可以在其上多路复用多个 channel 。所以我的应用程序是一个聊天应用程序,其
我有一些 .wav 文件,我想转换它们的频率 (fs) 和 channel 数 (nchannels)。我在jupyter笔记本python3.6上使用ffmpeg。我使用了以下命令并且它有效。 cm
我有一个视频渲染器,它需要两个 H265 流(YUV420),我需要烘焙它们以使它们中的一个与另一个形成 alpha 蒙版。这一切都已解决并且效果很好,但是如果我按照此处的说明进行操作: ffmpeg
我运行此命令以便能够将 udp 直播流传输到可使用正在构建的移动应用程序播放的 http 直播流。 它只是一个只有音频流的流。 ffmpeg -i udp://@localhost:1111 -map
我在我的 discord.js 机器人中创建了 nuke 命令,它创建了具有相同名称、权限、主题等的 channel ,并删除了“原始” channel 。但是有一个问题,如何使 channel 与“
我是一名优秀的程序员,十分优秀!