使用AutobahnPython的Twisted Perspective Broker

时间:2013-11-01 19:27:34

标签: javascript python autobahn

我已经离开了我的舒适区,我有点困惑,我打算如何登录。

Autobahn提供了一种使用对Python服务器的RPC调用的方法,但是Twisted只提供了有关如何为客户端使用Twisted的文档。

为了解释我的意思,这是代码:

在Javascript中,您可以像这样调用RPC函数: sess.call(url, user, pass)

Python客户端代码:

from twisted.spread import pb
from twisted.internet import reactor
from twisted.cred import credentials

class Client(pb.Referenceable):

    def remote_print(self, message):
        print message

    def connect(self):
        factory = pb.PBClientFactory()
        reactor.connectTCP("localhost", 8800, factory)
        def1 = factory.login(credentials.UsernamePassword("Bob", "secret"),
                             client=self)
        def1.addCallback(self.connected)
        reactor.run()

    def connected(self, perspective):
        self.perspective = perspective

        d = perspective.callRemote("joinGroup", "#Magic")
        d.addCallback(self.gotGroup)

    def gotGroup(self, group):
        d = group.callRemote("send", "Test message.")
        d.addCallback(self.shutdown)

    def shutdown(self, result):
        reactor.stop()


Client().connect()

Python服务器代码:

from zope.interface import implements

from twisted.cred import portal, checkers
from twisted.spread import pb
from twisted.internet import reactor

class ChatServer:
    def __init__(self):
        self.groups = {'#Magic':Group('#Magic')} # indexed by name

    def joinGroup(self, groupname, user):
        if self.groups.has_key(groupname):
            self.groups[groupname].addUser(user)
        return self.groups[groupname] or None

class ChatRealm:
    implements(portal.IRealm)
    def requestAvatar(self, avatarID, mind, *interfaces):
        assert pb.IPerspective in interfaces
        avatar = User(avatarID)
        avatar.server = self.server
        avatar.attached(mind)
        return pb.IPerspective, avatar, lambda a=avatar:a.detached(mind)

class User(pb.Avatar):
    def __init__(self, name): self.name = name
    def attached(self, mind): self.remote = mind
    def detached(self, mind): self.remote = None

    def perspective_joinGroup(self, groupname):
        return self.server.joinGroup(groupname, self)

    def send(self, message):
        self.remote.callRemote("print", message)

class Group(pb.Viewable):
    def __init__(self, groupname):
        self.name = groupname
        self.users = []

    def addUser(self, user): self.users.append(user)

    def view_send(self, from_user, message):
        for user in self.users:
            user.send("%s: %s" % (from_user.name, message))

realm = ChatRealm()
realm.server = ChatServer()
checker = checkers.InMemoryUsernamePasswordDatabaseDontUse()
checker.addUser("alice", "1234")
checker.addUser("Bob", "secret")
p = portal.Portal(realm, [checker])

reactor.listenTCP(9000, pb.PBServerFactory(p))
reactor.run()

基本上我只需要了解如何通过RPC在Javascript中访问登录调用,但我不知道如何做到这一点,谷歌没有向我提供任何选项。

1 个答案:

答案 0 :(得分:0)

Autobahn project不仅提供了WebSocket的实现(其中一个是AutobahnPython),还提供了WAMP (The WebSocket Application Messaging Protocol)

  

WAMP是一个开放的WebSocket子协议,提供两个异步   消息传递模式:RPC和PubSub。

AutobahnPython和AutobahnJS再次支持WAMP(以及越来越多的其他实现......请参阅here。)。看来AutobahnJS与AutobahnPython会让你想要做的事情:

  • 通过浏览器中运行的JavaScript在WebSocket上调用远程过程(用Python / Twisted / AutobahnPython编写)
  • 做发布&订阅,再次来自JavaScript,使用Python / Twisted / AutobahnPython(或其他)代理

免责声明:我是Autobahn和WAMP的原作者,并为Tavendo工作。

在Autobahn网站和相应的GitHub存储库中有多个完整的RPC和PubSub工作示例,例如: here