I got this working today, and it seems fine with the Pyjamas
"JSONRPCExample" from the pyjamas distribution.

Here's the code (originally based on some of the samples above, with
several fixes and some error handling added). I changed the pyjamas
object to 'jsonrpc' instead, since this is a generic jsonrpc server,
and not really tied to pyjamas.

#### step 1, create models/jsonrpc.py #################
"""
original code from : http://trac.pyworks.org/pyjamas/wiki/DjangoWithPyJamas
"""
import gluon.contrib.simplejson as simplejson
import types

class JSONRPCService:
    def response(self, id, result):
        return simplejson.dumps({'version': '1.1', 'id':id,
'result':result, 'error':None})
    def error(self, id, code, message):
        return simplejson.dumps({'id': id,
                                 'version': '1.1',
                                 'error': {'name': 'JSONRPCError',
                                           'code': code,
                                           'message': message
                                           }
                                     })

    def __init__(self):
        self.methods={}

    def serve(self):
        import sys
        data = simplejson.loads(request.body.read())
        id, method, params = data["id"], data["method"], data
["params"]
        if method in self.methods:
            try:
                result =self.methods[method](*params)
                return self.response(id, result)
            except BaseException:
                etype, eval, etb = sys.exc_info()
                return self.error(id, 100, '%s: %s' %(etype.__name__,
eval))
            except:
                etype, eval, etb = sys.exc_info()
                return self.error(id, 100, 'Exception %s: %s' %(etype,
eval))
        else:
            return self.error(id, 100, 'method "%s" does not exist' %
method)

    def __call__(self,func):
        self.methods[func.__name__]=func
        return func

    def listmethods(self):
        return self.methods.keys()

jsonrpc=JSONRPCService()


########## step 2) create controllers/rpc.py #############

def index():
    return jsonrpc.serve()

@jsonrpc
def echo(text):
    return 'echoing: %s' % text

@jsonrpc
def reverse(text):
    return text[-1::-1]

@jsonrpc
def uppercase(text):
    return text.upper()

@jsonrpc
def lowercase(text):
    return text.lower()

#### step 3) edit JSONRPCExample service location in pyjamas
#######################################

now in the pyjamas demo, copy JSONRPCExample.py and it's public folder
over to your web2py static directory.
In JSONRPCExample.py, change the EchoServicePython class to point to
your app (below, my app is named "pyjamas_2").

class EchoServicePython(JSONProxy):
    def __init__(self):
        JSONProxy.__init__(self, "/pyjamas_2/rpc", ["echo", "reverse",
"uppercase", "lowercase"])


#### step 4) build pyjamas code

#### step 5) browse to localhost:8000/<yourapp>/static/output/
JSONRPCExample.htm


Chris



On Feb 7, 8:42 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Almost there.... this
>
> http://groups.google.com/group/web2py/web/web2py.app.pyjamas.tar
>
> works but pyjamas does not understand the json response from the
> server.
> I am not sure what I am doing wrong. The json response if built in
> models/pyjamas.py
>
> Massimo
>
> On Feb 7, 7:08 am, lkcl <luke.leigh...@googlemail.com> wrote:
>
> >http://groups.google.com/group/pyjamas-dev/browse_thread/thread/f051f...
>
> > okaaaay, so i posted a test procedure there, which gets you to the
> > "first stage".  it should turn up any day soon on web2py cos i cross-
> > posted but hey i'll link it here as well.
>
> > it is worthwhile emphasising that the first stage has _nothing_ to do
> > with pyjamas, and everything to do with jsonrpc.
>
> > making web2py jsonrpc-capable should be a real high-priority and those
> > simple, simple 16 lines of code (once corrected - well spotted
> > timothy :) should, ideally, be made a standard web2py module (or
> > JSONRPCService put straight into the web2py namespace, which seems to
> > be the way that things are done in web2py).
>
> > then, you can start writing jsonrpc services which can be talked to by
> > ohh, whatever, really.  extjs.  php.  pure AJAX.  whatever-you-choose.
>
> > once that's fixed and tested _then_ you can move on to utilising
> > pyjamas to compile some JSONRPC-client code that will talk to the
> > controllers/default.py functions, confident in the knowledge that
> > default.py does its job correctly.
>
> > l.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to