Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with my channel api code?
    primarykey
    data
    text
    <p>I've been working on getting a website together with GAE and the channel api to display and process real time data from a robot I'm working on. I'm pretty new to web development, and python in general, but it seems to me like this code should work I'll make it more fancy once I get it working (logging data into a database, actual data processing etc.), but for now I just want it to display the most recent occurance of the variable "val" from a post request sent from the internet connected board on the robot. Hopefully this will be helpful to anyone looking to do real time data display with the channel api.</p> <p>Right now I'm displaying a template with an input box set to send a post request to the page. Instead of updating the front page with the current value on submission however, I get a blank screen. I'm not sure if my problem actually has to do with the channel api code, or the java function for updating the displayed value.</p> <p>Python code:</p> <pre><code>import os import webapp2 import jinja2 import json import logging from google.appengine.ext import db from google.appengine.api import channel from 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}) for i in range(len(channels)): channel.send_message(channels[i], jsn) app = webapp2.WSGIApplication([ ('/', MainPage) ], debug=True) </code></pre> <p>Relevant part of HTML Template:</p> <pre><code> &lt;form method="post"&gt; &lt;label&gt; &lt;div&gt;Title&lt;/div&gt; &lt;input type="text" name="value" value="{{value}}"&gt; &lt;/label&gt; &lt;br&gt; &lt;input type="submit"&gt; &lt;div class="error"&gt;{{error}}&lt;/div&gt; &lt;/form&gt; &lt;hr&gt; &lt;script&gt; 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); &lt;/script&gt; &lt;p id="val"&gt;No value to display&lt;/p&gt; &lt;/body&gt; </code></pre> <p></p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload