0

I'm way out of my comfort zone here and i'm a tad confused one how i'm meant to log in.

Autobahn provides a way to use RPC calls to the Python Server, however Twisted only appears to provide documentation on how to use Twisted for the client.

In order to explain what i mean properly, this is the code:

In Javascript, you can call a RPC function like so: sess.call(url, user, pass)

Python client code:

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 server code:

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()

Basically i just need to find out how to make the login call accessible in Javascript via RPC but i have no idea how to do that and Google isn't providing me with any options.

Leinad177
  • 51
  • 1
  • 7

1 Answers1

0

The Autobahn project not only provides implementations of WebSocket (one of which is AutobahnPython), but also WAMP (The WebSocket Application Messaging Protocol):

WAMP is an open WebSocket subprotocol that provides two asynchronous messaging patterns: RPC and PubSub.

And WAMP again is supported by AutobahnPython and AutobahnJS (and a growing number of other implementations .. see here.). It seems AutobahnJS with AutobahnPython would allow you what you want to do:

  • call remote procedures (written in Python/Twisted/AutobahnPython) on a server over WebSocket from JavaScript running in the browsers
  • doing publish & subscribe, again from and to JavaScript, with the broker being in Python/Twisted/AutobahnPython (or others)

Disclaimer: I am original author of Autobahn and WAMP, and work for Tavendo.

There a multiple, complete working examples of RPC and PubSub on the Autobahn site and in the respective GitHub repos, e.g. here.

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • I've been to your site and looked at all the documentation. While there is a bunch of nice examples, there doesn't seem to be anything covering perspective broker and i don't know enough about pb to make it work with just rpc. Hence why my question is asking how you would do pb with autobahn and not if it is possible which i already know. – Leinad177 Nov 02 '13 at 19:50