简体   繁体   English

Crossbar.io:如何使用Django服务发布有关主题的消息?

[英]Crossbar.io: How to publish a message on a topic using a Django service?

I just started using Crossbar.io to implement a live stats page. 我刚刚开始使用Crossbar.io来实现实时统计页面。 I've looked at a lot of code examples, but I can't figure out how to do this: 我看了很多代码示例,但是我不知道该怎么做:

I have a Django service (to avoid confusion, you can assume I´m talking about a function in views.py) and I'd like it to publish messages in a specific topic, whenever it gets called. 我有一个Django服务(为避免混淆,您可以假设我在views.py中谈论一个函数),并且我希望它可以在调用特定主题时发布消息。 I've seen these approaches: (1) Extending ApplicationSession and (2) using an Application instance that is "runned" . 我已经看到了这些方法:(1) 扩展ApplicationSession和(2) 使用“ runned”的Application实例

None of them work for me, because the Django service doesn't live inside a class, and is not executed as a stand-alone python file either, so I don't find a way to call the "publish" method (that is the only thing I want to do on the server side). 它们对我都不起作用,因为Django服务不存在于类中,并且也不作为独立的python文件执行,因此我找不到调用“发布”方法的方法(即我唯一想在服务器端执行的操作)。

I tried to get an instance of "StatsBackend", which extends ApplicationSession, and publish something... But StatsBackend._instance is None always (even when I execute 'crossbar start' and StatsBackend. init () is called). 我试图让“StatsBackend”,延伸ApplicationSession的实例,并发布一些信息,但StatsBackend._instance是无总是(甚至当我执行“横梁开始”和StatsBackend。 的init()被调用)。

StatsBackend.py: StatsBackend.py:

from twisted.internet.defer import inlineCallbacks
from autobahn import wamp
from autobahn.twisted.wamp import ApplicationSession

class StatsBackend(ApplicationSession):

    _instance = None

    def __init__(self, config):
        ApplicationSession.__init__(self, config)
        StatsBackend._instance = self

    @classmethod
    def update_stats(cls, amount):
        if cls._instance:
            cls._instance.publish('com.xxx.statsupdate', {'amount': amount})

    @inlineCallbacks
    def onJoin(self, details):
        res = yield self.register(self)
        print("CampaignStatsBackend: {} procedures registered!".format(len(res)))

test.py: test.py:

import StatsBackend

StatsBackend.update_stats(100) #Doesn't do anything, StatsBackend._instance is None

Django is a blocking WSGI application, and that does not blend well with AutobahnPython, which is non-blocking (runs on top of Twisted or asyncio). Django是一个阻塞的WSGI应用程序,与非阻塞的AutobahnPython不能很好地融合在一起(在Twisted或asyncio上运行)。

However, Crossbar.io has a built-in REST bridge, which includes a HTTP Pusher to which you can submit events via any HTTP/POST capable client. 但是,Crossbar.io具有内置的REST桥,其中包括一个HTTP Pusher ,您可以通过任何具有HTTP / POST功能的客户端向其提交事件。 Crossbar.io will forward those events to regular WAMP subscribers (eg via WebSocket in real-time). Crossbar.io会将这些事件转发给常规的WAMP订户(例如,通过WebSocket实时)。

Crossbar.io also comes with a complete application template to demonstrate above functionality. Crossbar.io还带有完整的应用程序模板,以演示上述功能。 To try: 尝试:

cd ~/test1
crossbar init --template pusher
crossbar start

Open your browser at http://localhost:8080 (open the JS console) and in a second terminal http://localhost:8080 (打开JS控制台)和第二个终端中打开浏览器

curl -H "Content-Type: application/json" \
   -d '{"topic": "com.myapp.topic1", "args": ["Hello, world"]}' \
   http://127.0.0.1:8080/push

You can then do the publish from within a blocking application like Django. 然后,您可以从Django之类的阻止应用程序中进行发布。

I found what I needed: It is possible to do a HTTP POST request to publish on a topic. 我找到了所需的内容:可以执行HTTP POST请求以发布主题。

You can read the doc for more information: https://github.com/crossbario/crossbar/wiki/Using-the-REST-to-WebSocket-Pusher 您可以阅读该文档以获取更多信息: https : //github.com/crossbario/crossbar/wiki/使用-REST-to-WebSocket- Pusher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM