python - What's wrong with my channel api code? -
i've been working on getting website gae , channel api display , process real time data robot i'm working on. i'm pretty new web development, , python in general, seems me code should work i'll make more fancy once working (logging data database, actual data processing etc.), want display recent occurance of variable "val" post request sent internet connected board on robot. helpful looking real time data display channel api.
right i'm displaying template input box set send post request page. instead of updating front page current value on submission however, blank screen. i'm not sure if problem has channel api code, or java function updating displayed value.
python code:
import os import webapp2 import jinja2 import json import logging google.appengine.ext import db google.appengine.api import channel google.appengine.api import users template_dir = os.path.join(os.path.dirname(__file__), 'templates') jinja_env = jinja2.environment(loader = jinja2.filesystemloader(template_dir), autoescape=true) channels = [] class mainpage(webapp2.requesthandler): def get(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) return nickname = user.nickname() clientid = user.user_id() channel_token = channel.create_channel(clientid) channels.append(channel_token) template_values = {'channel_id': channel_token, 'nickname': nickname} template = jinja_env.get_template('front.html') self.response.out.write(template.render(template_values)) def post(self): value = self.request.get("value") if value: jsn = json.dumps({"val": value}) in range(len(channels)): channel.send_message(channels[i], jsn) app = webapp2.wsgiapplication([ ('/', mainpage) ], debug=true)
relevant part of html template:
<form method="post"> <label> <div>title</div> <input type="text" name="value" value="{{value}}"> </label> <br> <input type="submit"> <div class="error">{{error}}</div> </form> <hr> <script> onmessage = function myfunction(message) { document.getelementbyid("val").innerhtml=message; } var token = '{{ channel_id }}'; var channel = new goog.appengine.channel(token); var socket = channel.open(); socket.onmessage = onmessage(message.val); </script> <p id="val">no value display</p> </body>
thanks in advance.
on line in javascript assign socket.onmessage:
socket.onmessage = onmessage(message.val);
what you're doing assigning result of call onmessage() function. instead assign function socket.onmessage, so:
socket.onmessage = onmessage;
also in onmessage handler, use message.data
json value passed in. val
field you'll need json decode message.data
.
Comments
Post a Comment