Channel API is the API to synchronize clients in real time.
The procedure is following.
1.Client side:request a Channel API token and client ID from GAE.
2.GAE side:create the token and return it to the client.
3.Client side:open the client's channel with the token.
4.GAE side:send message to the client with the client ID, not the token.
5.Client side:get message.
1.Client side:request a Channel API token and client ID from GAE.
html:
<span id="token" style="display:none;">none-token</span>
javascipt:
function take_token() {
console.debug('taking a token');
$.get("/ChannelAPI", {}, function(results){
$("#token").text(results.token)//save token to "#token".
make_channel(results.token)
},"json");
}
2.GAE side:create the token and return it to the client.
def get(self):
client_id = "user1"
token = channel.create_channel(client_id)
self.response.headers.add_header('Content-Type', 'application/json; charset=utf-8')
self.response.out.write(simplejson.dumps({'token': token}))
3.Client side:open the client's channel with the token.
function make_channel(token){
channel = new goog.appengine.Channel(token);
socket = channel.open();
socket.onopen = onOpened;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
}
4.GAE side:send message to the client with the client ID, not the token.
def post(self):
channel.send_message("user1", simplejson.dumps({"tokens":tokens}))
5.Client side:get message.
function onMessage(message){
a = new Array()
console.debug('received a message: ', message);
console.debug("receive a message.data:",message.data);
var obj = JSON.parse(message.data);
console.debug(obj.tokens)
console.debug(obj.tokens[0])
}
No comments:
Post a Comment