0
處理器實現使用JavaScript API通道
class TestHandler(RequestHandler, Jinja2Mixin):
def get(self):
channel_id = str(random.randint(1, 10000)) + str(datetime.now())
chat_token = channel.create_channel(channel_id)
context = {'channel_id': channel_id, 'chat_token': chat_token}
return self.render_response('test.html', **context)
def post(self):
channel_id = str(random.randint(1, 10000)) + str(datetime.now())
chat_token = channel.create_channel(channel_id)
context = {'channel_id': channel_id, 'chat_token': chat_token}
return self.render_response('test.html', **context)
HTML實現
<html>
<head>
<script type="text/javascript" language="javascript" src="/static/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" language="javascript" src="/static/js/backend.js"></script>
</head>
<body>
<form method="post" id="testform" name="testform">
<br><label name="channel_id" id="channel_id">{{channel_id}}</label>
<br><label name="chat_token" id="channel_id">{{chat_token}}</label>
<input type="submit" id="btnsubmit" class="btnsubmit" name="btnsubmit" value="submit" />
</form>
</body>
</html>
jQuery的執行
$(document).ready(function() {
var token =$('#channel_id').val()
alert(token)
var channel = new goog.appengine.Channel(token);
var socket = channel.open();
socket.onopen = onOpened;
onOpened = function() {
connected = true;
var xhr = new XMLHttpRequest();
xhr.open('POST','/dashboard/', true);
xhr.send();
};
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
});
調用後端URL我想打電話給後端處理。我不知道怎麼做。這就是我所做的。任何人都可以提供幫助嗎?
我認爲,Channel API與這個問題大多無關。 –