*意外と解説が長くなったのでArduinoを操作する部分は次回の記事で書くことにしました。
まず、index.jsの解説を行います。
ソースは、var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req,res){
res.sendfile('index.html');
});
io.on('connection',function(socket){
socket.on('chat message', function(msg){
io.emit('chat message',msg);
});
});
http.listen(3000,function(){
console.log('listenning on localhost:3000');
});
となっていました。上から順番に説明してきます。まず、
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
の部分では使用するパッケージの読み込みをしています。おまじないと思っても使うことはできます。次に、
app.get('/', function(req,res){
res.sendfile('index.html');
});
の部分では同ディレクトリのindex.htmlを読み込みます。また、
io.on('connection',function(socket){
socket.on('chat message', function(msg){
io.emit('chat message',msg);
});
});
の部分ではwebサイト(localhost:3000)に接続したことを検知した際の処理を記述しています。ここでは接続状態であるときにindex.html側のスクリプトから'chat message'を受け取ったら、その内容(msg)をindex.htmlに対して返す。というような内容になっています。続いてindex.htmlについて
index.htmlのhtmlの要素としては2つしかありません。まずひとつはチャットの表示部分であるList要素、それから発言BOXとしてのForm要素です。index.htmlのソースを見てみましょう。
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
<script src="/socket.io/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="m" autocomplete="off"/><button id="btn">Send</button>
</form>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message',function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
まず重要なのが、<script src="/socket.io/socket.io.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>の部分です。外部ファイルとしてsocketio.jsとjquery.min.jsを読み込んでいます。jqueryはhtmlの要素を楽に操作するために使用しています。
次に重要なのは
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message',function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
の部分です。前半ではform要素の中でsumbitアクションが起きた時の動作を決めています。この書き方はjqueryのものなのですが、#が着くとidを指定、何もつかない場合はその要素を全て指定という解釈をしています。必要になったらググります。後半の部分では、index.jsからのchat messageを検知して、msgの内容をlist要素に追加するというようになっています。理解するために図を書くと…
という感じになっています。
意外と長くなってしまったので今回の記事は2.5回として、Arduinoとの通信は次回の記事にしたいと思います。
※追記2014/07/2123:19
画像の紫の部分がindex.htmlとなっていますが正しくはindex.jsです。

0 件のコメント:
コメントを投稿