【问题标题】:Autobahn component with .NET router (WampSharp)带有 .NET 路由器的高速公路组件 (WampSharp)
【发布时间】:2017-02-20 19:08:11
【问题描述】:

目前我正在使用 WAMP 原型,我很好奇这样的事情是否可行,因为我无法让它工作:

1.拥有用 c# (WampSharp) 编写的 WAMP 路由器:

const string location = "ws://127.0.0.1:9999/wsdemo";

try
{
    using (IWampHost host = new DefaultWampHost(location))
    {
        IWampHostedRealm realm = host.RealmContainer.GetRealmByName("realm1");
        host.Open();
        Console.WriteLine("Host is running on : " + location);
    }
}
catch(Exception exc)
{
    Console.WriteLine("Error : " + exc.ToString());
}

2.用python编写组件:

import random
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.util import sleep
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner

class Component(ApplicationSession):
    """
    An application component that publishes events with no payload
    and with complex payload every second.
    """

    @inlineCallbacks
    def onJoin(self, details):
        print("session attached")

        counter = 0
        while True:
            num = random.randint(0, 100)
            print("publishing : com.myapp.topic1", num)
            self.publish(u'com.myapp.topic1', num)
            counter += 1
            yield sleep(1)


if __name__ == '__main__':
    runner = ApplicationRunner(url=u"ws://127.0.0.1:9999/wsdemo", realm=u"realm1")
    runner.run(Component)

当我运行 python 脚本时,我得到了错误:

2017-02-20T19:49:46+0100 Main loop terminated.
2017-02-20T19:49:46+0100 Traceback (most recent call last):
2017-02-20T19:49:46+0100   File "C:\Program Files (x86)\JetBrains\PyCharm Educational Edition 1.0.1\helpers\pydev\pydevd.py", line 2199, in <module>
2017-02-20T19:49:46+0100     globals = debugger.run(setup['file'], None, None)
2017-02-20T19:49:46+0100   File "C:\Program Files (x86)\JetBrains\PyCharm Educational Edition 1.0.1\helpers\pydev\pydevd.py", line 1638, in run
2017-02-20T19:49:46+0100     pydev_imports.execfile(file, globals, locals)  # execute the script
2017-02-20T19:49:46+0100   File "D:/Programming/Astronomy/Dev/ZenithPlatform/backbone/local/tests/wamp.py", line 41, in <module>
2017-02-20T19:49:46+0100     runner.run(Component)
2017-02-20T19:49:46+0100   File "C:\Python27\lib\site-packages\autobahn\twisted\wamp.py", line 312, in run
2017-02-20T19:49:46+0100     raise connect_error.exception
2017-02-20T19:49:46+0100 twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 10061: No connection could be made because the target machine actively refused it..

根据http://autobahn.ws/python/wamp/programming.html#running-a-wamp-router

我们创建的组件尝试连接到 WAMP 路由器 在本地运行,它接受端口 8080 上的连接,并且对于 领域领域1。

我们建议的方法是使用 Crossbar.io 作为您的 WAMP 路由器。有 除了 Crossbar.io 之外的其他 WAMP 路由器。

这样的事情能实现吗?

谢谢,
奇瓦

【问题讨论】:

    标签: python autobahnws wampsharp


    【解决方案1】:

    您的 using 语句在程序结束之前处理路由器。

    只需添加一个 Console.ReadLine();在 Console.WriteLine() 语句之后的 using 范围内的语句。应该可以的。

    【讨论】:

    • 哦,亲爱的主... :D 完全忽略了这一点,当我快速复制演示代码以赢得应用程序时。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多