2010/12/21

[GoogleAppEngine]Channel APIの大まかな流れ

Google App Engineにやっと念願のChannel APIが導入されました。

これは何かというと、早い話、websocketです。

というわけで、さっそく簡単な例を
(ドメインは、http://hoge.appspot.com/とします)

#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import channel
from google.appengine.api import users
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
def get(self):
#下のメソッドの引数は文字列ではないとエラーになる
token = channel.create_channel("websockettest")
template_values = {'token':token}

path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

def post(self):

msg = self.request.get('msg')
msg = msg.encode('utf-8')
self.response.out.write("var ret; ret = {result:true};")
#下のメソッドの引数は上のcreate_channelメソッドと同じでオッケー
channel.send_message("websockettest",msg)

def main():
application = webapp.WSGIApplication([('/', MainHandler)],debug=True)
util.run_wsgi_app(application)


if __name__ == '__main__':
main()

上のプログラムで、まず、from google.appengine.api import channelを使って、Python上のchannel apiをインクルードします。

次に、channel.create_channelメソッドを使って、tokenの発行を行い、クライアント側に引き渡します。

channel.send_messageメソッドは、ある特定のIDにメッセージを送るときに使用します。

<html>
<head>
<title>Sample Code</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src='http://hoge.appspot.com/_ah/channel/jsapi'></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
</script>
</head>
<body>
<script type="text/javascript">
var socket;
$(document).ready(function(){

channel = new goog.appengine.Channel('{{ token }}');
socket = channel.open();
socket.onopen = onOpened;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;

$("#msg_post").bind('click',function(){

$.ajax({
type: "POST",
url: "http://hoge.appspot.com/",
data: "msg="+encodeURIComponent($("#msg").val()),
success: function(data){
console.log(data)
},
error:function(){
console.log("失敗しました。")
}
});

});
});

function onOpened(){

}

function onClose(){
console.log("閉じます")
}

function onError(error){
console.log("エラーが発生しました。")
}

function onMessage(res){
$("#total_msg").append(res.data + "<br>");
}

</script>
<input type="text" id="msg" value="">
<input type="button" value="投稿" id="msg_post">
<div id="total_msg"></div>

</body>
</html>

まず、JavaScript側では、必ず、http://hoge.appspot.com/_ah/channel/jsapiをインクルードする必要があります。

次は、サーバー側から受け取った、new goog.appengine.Channelでインスタンス変数の作成を行い、openメソッドで、socketの開放を行います。

無事につながった場合は、onopenプロパティーで設定した関数が呼ばれます。

onmessageプロパティーが、サーバー側からメッセージを受信したときに、自動的に呼び出されるメソッドです。
ここが一番ほっとです。

重要なのは、あくまでクライアントからではなく、サーバ側から呼ばれるということ。
ajaxでもこれは実現できません。
これが、websocketのすごさです。

で、onerrorプロパティーがエラーが発生したとき、oncloseプロパティーにセットした関数はsocketをクローズするときに呼び出されます。

これで、websocket系も怖くはありませんね。

0 コメント:

コメントを投稿