Perl + jQuery + Ajaxでチャット的な何かを目指す
Perl,jQueryで動的なチャットぽいサイトを作ってみる。
まずは、サーバにテキストを送るところから。
サーバサイド その1.
#!/usr/bin/perl print "Content-type: text/html\n\n"; print << "END"; <html> <head> <title> TEST </title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> <script type="text/javascript"> <!-- jQuery( function(){ jQuery('#button').click( function(){ var text = jQuery('#message').val(); jQuery.get( 'jquery-sample-get.cgi', {message: text}, function(data,textStatus){ if(textStatus=='success'){ jQuery('#jquery-sample-textStatus').text('SUCCESS'); } jQuery('#jquery-sample-get').html(data); } ,'html' ); if(jQuery('#jquery-sample-textStatus').text==''){ jQuery('#jquery-sample-textStatus').text('FAILED'); } }, function(){ jQuery('#jquery-sample-get').html(''); jQuery('#jquery-sample-textStatus').text(''); } ); }); //--> </script> </head> <body> <h1> hello chat </h1> <div id="jquery-sample"> <p> <textarea id="message"></textarea> <button id="jquery-sample-button">submit</button> <span id="jquery-sample-textStatus"></span> </p> <div id="jquery-sample-get"></div> </div> </body> </html> END
その2.
#!/usr/bin/perl print "Content-type: text/html\n\n"; if($ENV{'REQUEST_METHOD'} eq "GET"){ foreach $data (split(/&/,$ENV{'QUERY_STRING'})){ ($key,$value) = split(/=/,$data); $in{$key}= $value; print "<p>".$in{'message'}."</p>"; }}
参考URL:
アルファシス – alphasis.info -
JSON - JSONデータの解析 / Perlモジュール徹底解説
Ajaxで定期的に特定部分を更新
[http://tenderfeel.xsrv.jp/perl/781/:title=[perl] 学習メモ:Hello world!! → GET値の取得と処理 → ファイルの内容出力]
jQuery & Ajax通信を使ってPHPにPOSTでデータを送信するサンプル