我正在尝试在django上使用渠道项目(http://channels.readthedocs.org/en/latest/index.html)。
虽然在文档上有一个很好的教程,用于构建基于Group的websocket应用程序(聊天),但我找不到与客户特定的简单推送机制相关的内容(所以不需要使用Group)< / p>
假设我想与各种新闻提供商一起构建一个feed聚合器,当用户访问主页并等待所有的feed被解析时,我想向他发送关于服务器正在解析哪一个的信息性消息,等他的时候。
我现在得到的是: 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 :(得分:5)
频道现在为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保持不变......
呸,这有点棘手但发现它。
您必须使用邮件的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",
}