Hi,

I have defined the following pyjs class:

class WebSocket:

    def __init__(self):
        pass

    def process_event(self, data):
        pass

    def connect_websocket(self, group, server="127.0.0.1", port=8888):
        # TODO: I redefine web2py_websocket here because I can not access
        #   the one in the top iframe (I have tried "web2py_websocket" and 
"parent.web2py_websocket")
        websocket = '''function 
web2py_websocket(url,onmessage,onopen,onclose) {
  if ("WebSocket" in window) {
    var ws = new WebSocket(url);
    ws.onopen = onopen?onopen:(function(){});
    ws.onmessage = onmessage;
    ws.onclose = onclose?onclose:(function(){});
    return true; // supported
  } else return false; // not supported
}'''
        JS(""" eval(@{{websocket}}); """)
        myjs = '''
    parent.jQuery(document).ready(function(){
        var callback=function(e){process_event("self", e)};
        if(!web2py_websocket('ws://%s:%s/realtime/%s',callback)) {
            alert("html5 websocket not supported by your browser, try 
Google Chrome");
        }
    });''' % (server, port, group)
        JS(""" eval(@{{myjs}}); """)

This works as expected: 

WebSocket().connect_websocket("mygroup")

It will connect to my tornado server, group "mygroup". And the websocket 
messages are also received, as expected.
The problem is that I want to process the websocket messages (which will be 
just json data) in the pyjs method "process_event", since I must make 
extensive use of my pyjs code when processing. But this is not working:

var callback=function(e){process_event("self", e)};

I get the error message:

"Uncaught ReferenceError: process_event is not defined"

How can I call a pyjs class method from the embedded javascript?

Thanks,
Daniel

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Pyjs.org Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to