0

I would like to use the GAE Channel API (Python 2.7) with my GWT app (using GWT-GAE-Channel), and I am having trouble figuring out how to get the token created on the server side Python into my GWT app ("index.html" in this example) so that the client side GWT can open the channel. My server-side code currently looks like this:

import webapp2
import jinja2
import os
import uuid
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import channel

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    #The main page returns the GWT application
    def get(self):

        #Create a token when the GWT app is loaded to create a channel to return data
        client_id = uuid.uuid4().hex
        token = channel.create_channel(client_id, duration_minutes=None)     

        context = {} #There are currently no template variables in the GWT app
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(**context))

class A_handler(webapp2.RequestHandler):
    def post(self):
        client_id = self.request.get('id')
        channel.send_message(client_id, message)



app = webapp2.WSGIApplication([('/', MainPage),
                           ('/A', A_handler)],
                          debug=True)

I have the same example GWT-GAE-Channel client code as this question; however, what is the GWT code to receive "token" from the server for the GWT ChannelFactory.createChannel function? Can the python server send this token to the GWT html in the GET request as a template variable, or would this need to be sent by RPC or some other method?

Community
  • 1
  • 1
dave
  • 59
  • 5

1 Answers1

0

I was able to figure this out thanks to this thread, this tutorial, and learning about javascript.

The method I used was to send 'token' as a template variable in the above python server-side code to the GWT html file:

context = {'token': token} 
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(context))

and add the following to the GWT html file:

<script type="text/javascript">
    var token = "{{ token }}";
    </script>

In Eclipse (GWT), I added the following JSNI function to read in the javascript variable:

private native String get_token() /*-{
return $wnd.token
}-*/;

And then I included the following function in the Java OnModuleLoad to read in the javascript variable:

String token = get_token().toString();

If you have more than one variable to send from javascript, you can use a dictionary as described in the linked tutorial.

Community
  • 1
  • 1
dave
  • 59
  • 5