我正忙着尝试使用GAE和后端进行循环设置。我的目标:跟踪计时器的主要游戏循环(敌人在3秒内移动,冷却时间在5秒内准备好等)。我认为一个有线程的常驻后端是要走的路。到目前为止,我只能在appcfg.py后端更新后才能在仪表板中显示后端。现在我只是在努力弄清楚下一步。以下是一些具体问题:
最有用的是包含backends.yaml,主app .py文件和后端.py代码的基本示例。这就是我想象的一个例子:
我已经多次阅读过这些文档而且它已经不再沉没了。我试图搞清楚,但进展非常缓慢。我无法在网上找到一个有用的,面向新手的例子。任何帮助将非常感激!感谢,
答案 0 :(得分:0)
示例:
前端:
<script>
onMessage = function(msg){
console.log(msg.data)
}
channel = new goog.appengine.Channel('{{ token }}');
socket = channel.open();
socket.onmessage = onMessage;
</script>
testbackend.py
import logging, webapp2, time
from google.appengine.api import background_thread, channel
class Thread(webapp2.RequestHandler):
def post(self):
token = self.request.get("token")
t = background_thread.BackgroundThread(target=timed_message, args=[token])
t.start()
def timed_message(token, *kwargs):
logging.info(token)
time.sleep(2)
channel.send_message(token, "channel works")
pages = [
("/testbackend/start_thread", Thread),
]
app = webapp2.WSGIApplication(pages, debug=True)
main.py
import webapp2, jinja2, os, logging
from google.appengine.api import backends, urlfetch, users, channel
JINJA_ENVIRONMENT = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class IndexPage(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
token = channel.create_channel(user.user_id())
template_values = {"token":token}
template = JINJA_ENVIRONMENT.get_template('templates/index.html')
self.response.write(template.render(template_values))
class TestAReq(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
url = backends.get_url("testbackend")+"/testbackend/start_thread"
result = urlfetch.fetch(url, method="POST", payload="token="+user.user_id())
self.response.write("abc")
pages = [
('/', IndexPage),
("/ajaxtest", TestAReq),
]
app = webapp2.WSGIApplication(pages, debug=True)
app.yaml处理程序:
handlers:
- url: /static
static_dir: static
- url: /testbackend/.*
script: testbackend.app
- url: /.*
script: main.app