我对 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),但它看起来相当复杂。
请帮帮我。
谢谢你的高级。