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