【发布时间】:2015-01-03 06:24:21
【问题描述】:
我对 Autobahn 和 WAMP(Web 应用消息传递协议)还很陌生。
我只是基于http://autobahn.ws/python/wamp/programming.html 和https://github.com/crossbario/crossbarexamples/blob/master/votes/python/votes.py 创建一个简单的应用程序组件
下面是我的服务器端 Python
from autobahn.asyncio.wamp import (
ApplicationSession,
ApplicationRunner
)
from autobahn import wamp
from asyncio import coroutine
class MyComponent(ApplicationSession):
@wamp.register("com.myapp.add2")
def add2(self, x, y):
print("added 2")
return x + y
@wamp.register("com.myapp.add3")
def add3(self, x, y, z):
print("added 3")
return x + y + z
@coroutine
def onJoin(self, details):
res = yield from self.register(self)
print("{} procedures registered.".format(len(res)))
if __name__ == '__main__':
runner = ApplicationRunner(url="ws://localhost:8080/ws", realm="realm1")
runner.run(MyComponent)
和客户端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<script>AUTOBAHN_DEBUG = false;</script>
<script src="http://autobahn.s3.amazonaws.com/autobahnjs/latest/autobahn.min.jgz"></script>
<script>
var connection = new autobahn.Connection({
url: "ws://localhost:8080/ws",
realm: "realm1"
});
connection.onopen = function (session, details) {
session.call("com.myapp.add2", [2,3]).then(session.log);
session.call("com.myapp.add3", [2,3,4]).then(session.log);
};
connection.onclose = function (reason, details) {
console.log("Connection lost: " + reason);
};
connection.open();
</script>
</body>
</html>
错误
看起来这类似于https://github.com/hwmrocker/hextest/issues/2,但我无法理解。我什至找不到有效的样本。这个 (https://github.com/tavendo/AutobahnPython/tree/master/examples/asyncio/wamp/wamplet/wamplet1) 类似,但也有同样的问题。
令人惊讶的是,当我在同一个端口上运行外部 Crossbar 示例并运行上述示例时,它就像魔术一样工作,我可以在控制台上看到结果。
我找到了这个 (https://github.com/tavendo/AutobahnPython/blob/master/examples/asyncio/wamp/basic/server.py),但它看起来很复杂。
请帮帮我。
谢谢你的高级。
【问题讨论】:
-
请注意,许多示例/文档当前需要更新:AutobahnPython 中包含的“基本 WAMP 路由器”(例如选项
standalone == True到ApplicationRunner)最近已移至 Crossbar.io :groups.google.com/d/msg/autobahnws/bCj7O2G2sxA/6-pioJZ_S_MJ。 AutobahnPython 不仅仅是 WAMP client 库 - 与所有其他 Autobahn 库一样。因此,您需要一个外部 WAMP 路由器。
标签: python autobahn python-asyncio crossbar