Google app engine 如何让GWT接收Python GAE通道API创建的令牌?
我想将GAE通道API(Python2.7)与我的GWT应用程序(使用)一起使用,我很难弄清楚如何将在服务器端Python上创建的令牌放入我的GWT应用程序(本例中为index.html),以便客户端GWT可以打开通道。我的服务器端代码当前如下所示:Google app engine 如何让GWT接收Python GAE通道API创建的令牌?,google-app-engine,gwt,channel-api,Google App Engine,Gwt,Channel Api,我想将GAE通道API(Python2.7)与我的GWT应用程序(使用)一起使用,我很难弄清楚如何将在服务器端Python上创建的令牌放入我的GWT应用程序(本例中为index.html),以便客户端GWT可以打开通道。我的服务器端代码当前如下所示: import webapp2 import jinja2 import os import uuid from google.appengine.ext import db from google.appengine.api import user
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)
我的示例GWT GAE通道客户端代码与;但是,对于GWT ChannelFactory.createChannel函数,从服务器接收“令牌”的GWT代码是什么?python服务器是否可以将此令牌作为模板变量发送到GET请求中的GWT html,或者是否需要通过RPC或其他方法发送 多亏了,并且学习了javascript,我才明白这一点 我使用的方法是将上述python服务器端代码中的“token”作为模板变量发送到GWT html文件:
context = {'token': token}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(context))
<script type="text/javascript">
var token = "{{ token }}";
</script>
并将以下内容添加到GWT html文件中:
context = {'token': token}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(context))
<script type="text/javascript">
var token = "{{ token }}";
</script>
然后我在Java OnModuleLoad中包含了以下函数,以读取javascript变量:
private native String get_token() /*-{
return $wnd.token
}-*/;
String token = get_token().toString();
如果要从javascript发送多个变量,可以使用链接教程中描述的字典