【问题标题】:How do I get GWT to receive the token created by the Python GAE Channel API?如何让 GWT 接收 Python GAE Channel API 创建的令牌?
【发布时间】:2012-09-22 19:13:36
【问题描述】:

我想将 GAE Channel API (Python 2.7) 与我的 GWT 应用程序(使用 GWT-GAE-Channel)一起使用,但我无法弄清楚如何将在服务器端 Python 上创建的令牌放入我的 GWT 应用程序( “index.html”在这个例子中),以便客户端 GWT 可以打开通道。我的服务器端代码目前如下所示:

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)

我有与this question 相同的示例 GWT-GAE-Channel 客户端代码;但是,从服务器接收 GWT ChannelFactory.createChannel 函数的“令牌”的 GWT 代码是什么? python 服务器可以在 GET 请求中将此令牌作为模板变量发送到 GWT html,还是需要通过 RPC 或其他方法发送?

【问题讨论】:

    标签: google-app-engine gwt channel-api


    【解决方案1】:

    感谢this thread、this tutorial 和对 javascript 的了解,我能够解决这个问题。

    我使用的方法是将上述python服务器端代码中的'token'作为模板变量发送到GWT html文件:

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

    并将以下内容添加到 GWT html 文件中:

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

    在 Eclipse (GWT) 中,我添加了以下 JSNI 函数来读取 javascript 变量:

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

    然后我在 Java OnModuleLoad 中包含以下函数以读取 javascript 变量:

    String token = get_token().toString();
    

    如果您要从 javascript 发送多个变量,则可以使用链接教程中描述的字典。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      相关资源
      最近更新 更多