使用 Django 频道时如何使用 Channel 而不是 Group?
How to use Channel instead of Group when using django channels?
我正在尝试使用 django 上的频道项目 (http://channels.readthedocs.org/en/latest/index.html)。
虽然在文档上有一个关于构建基于组的 websocket 应用程序(聊天)的很好的教程,但我找不到与特定于客户端的简单推送机制相关的内容(因此无需使用组)
假设我想构建一个包含各种新闻提供商的提要聚合器,当用户访问主页并等待所有提要被解析时,我想向他发送信息性消息,告诉他哪个正在被解析服务器,他在等待。
我现在得到的是:
consumers.py
from channels import Group, Channel
from .views import sort_articles_by_date
from .soup import ProviderParser
from .models import Provider
# Connected to websocket.connect and websocket.keepalive
def ws_add(message):
Group("news_providers_loading").add(message.reply_channel)
def ws_message(message):
providers = Provider.objects.all()
articles = []
for provider in providers:
Group("news_providers_loading").send({'content': str(provider)})
parser = ProviderParser(provider)
articles.extend(parser.parse_articles())
sort_articles_by_date(articles)
# Connected to websocket.disconnect
def ws_disconnect(message):
Group("news_providers_loading").discard(message.reply_channel)
routing.py
channel_routing = {
"websocket.connect": "news_providers.consumers.ws_add",
"websocket.keepalive": "news_providers.consumers.ws_add",
"websocket.receive": "news_providers.consumers.ws_message",
"websocket.disconnect": "news_providers.consumers.ws_disconnect",
}
虽然还可以,但还是忍不住觉得有点矫枉过正(?)
有没有办法只使用 Channel 构造函数而不是 Group?
谢谢:)
更新:
频道版本 = 0.9
通道现在为 0.9,因此客户端需要进行一些更改才能从服务器接收消息:
class Content:
def __init__(self, reply_channel):
self.reply_channel = reply_channel
def send(self, json):
self.reply_channel.send({
'reply_channel': str(self.reply_channel),
'text': dumps(json)
})
def ws_message(message):
content = Content(message.reply_channel)
content.send({'hello': 'world'})
routing.py保持不变...
频道版本 < 0.9
呸,有点坑,还是找到了
您必须使用消息的 reply_channel 属性。
所以这个:
Group("news_providers_loading").send({'content': str(provider)})
变成这样:
Channel(message.reply_channel).send({'content': str(provider)})
我现在得到的是:
from channels import Channel
from .soup import ProviderParser, sort_articles_by_date
from .models import Provider
from django.template.loader import render_to_string
from json import dumps
class Content:
def __init__(self, reply_channel):
self.reply_channel = reply_channel
def send(self, json):
Channel(self.reply_channel).send({'content': dumps(json)})
def ws_message(message):
providers = Provider.objects.all()
content = Content(message.reply_channel)
content.send({'providers_length': len(providers)})
articles = []
for provider in providers:
content.send({'provider': str(provider)})
parser = ProviderParser(provider)
articles.extend(parser.parse_articles())
sort_articles_by_date(articles)
html = render_to_string('news_providers/article.html', {'articles': articles})
content.send({'html': html})
routing.py
channel_routing = {
"websocket.receive": "news_providers.consumers.ws_message",
}
看起来更轻巧,尽管您可能希望保持连接、保持活动和断开连接方法(作为简单的 foo 方法)-对此并不完全确定-!
# connect, keepalive and disconnect
def ws_foo(message):
pass
routing.py
channel_routing = {
"websocket.connect": "news_providers.consumers.ws_foo",
"websocket.keepalive": "news_providers.consumers.ws_foo",
"websocket.receive": "news_providers.consumers.ws_message",
"websocket.disconnect": "news_providers.consumers.ws_foo",
}
我正在尝试使用 django 上的频道项目 (http://channels.readthedocs.org/en/latest/index.html)。
虽然在文档上有一个关于构建基于组的 websocket 应用程序(聊天)的很好的教程,但我找不到与特定于客户端的简单推送机制相关的内容(因此无需使用组)
假设我想构建一个包含各种新闻提供商的提要聚合器,当用户访问主页并等待所有提要被解析时,我想向他发送信息性消息,告诉他哪个正在被解析服务器,他在等待。
我现在得到的是: consumers.py
from channels import Group, Channel
from .views import sort_articles_by_date
from .soup import ProviderParser
from .models import Provider
# Connected to websocket.connect and websocket.keepalive
def ws_add(message):
Group("news_providers_loading").add(message.reply_channel)
def ws_message(message):
providers = Provider.objects.all()
articles = []
for provider in providers:
Group("news_providers_loading").send({'content': str(provider)})
parser = ProviderParser(provider)
articles.extend(parser.parse_articles())
sort_articles_by_date(articles)
# Connected to websocket.disconnect
def ws_disconnect(message):
Group("news_providers_loading").discard(message.reply_channel)
routing.py
channel_routing = {
"websocket.connect": "news_providers.consumers.ws_add",
"websocket.keepalive": "news_providers.consumers.ws_add",
"websocket.receive": "news_providers.consumers.ws_message",
"websocket.disconnect": "news_providers.consumers.ws_disconnect",
}
虽然还可以,但还是忍不住觉得有点矫枉过正(?) 有没有办法只使用 Channel 构造函数而不是 Group?
谢谢:)
更新:
频道版本 = 0.9
通道现在为 0.9,因此客户端需要进行一些更改才能从服务器接收消息:
class Content:
def __init__(self, reply_channel):
self.reply_channel = reply_channel
def send(self, json):
self.reply_channel.send({
'reply_channel': str(self.reply_channel),
'text': dumps(json)
})
def ws_message(message):
content = Content(message.reply_channel)
content.send({'hello': 'world'})
routing.py保持不变...
频道版本 < 0.9
呸,有点坑,还是找到了
您必须使用消息的 reply_channel 属性。 所以这个:
Group("news_providers_loading").send({'content': str(provider)})
变成这样:
Channel(message.reply_channel).send({'content': str(provider)})
我现在得到的是:
from channels import Channel
from .soup import ProviderParser, sort_articles_by_date
from .models import Provider
from django.template.loader import render_to_string
from json import dumps
class Content:
def __init__(self, reply_channel):
self.reply_channel = reply_channel
def send(self, json):
Channel(self.reply_channel).send({'content': dumps(json)})
def ws_message(message):
providers = Provider.objects.all()
content = Content(message.reply_channel)
content.send({'providers_length': len(providers)})
articles = []
for provider in providers:
content.send({'provider': str(provider)})
parser = ProviderParser(provider)
articles.extend(parser.parse_articles())
sort_articles_by_date(articles)
html = render_to_string('news_providers/article.html', {'articles': articles})
content.send({'html': html})
routing.py
channel_routing = {
"websocket.receive": "news_providers.consumers.ws_message",
}
看起来更轻巧,尽管您可能希望保持连接、保持活动和断开连接方法(作为简单的 foo 方法)-对此并不完全确定-!
# connect, keepalive and disconnect
def ws_foo(message):
pass
routing.py
channel_routing = {
"websocket.connect": "news_providers.consumers.ws_foo",
"websocket.keepalive": "news_providers.consumers.ws_foo",
"websocket.receive": "news_providers.consumers.ws_message",
"websocket.disconnect": "news_providers.consumers.ws_foo",
}