WebSocketsってなにができるの?
WebSocketsは双方向ストリーム通信ができる規格。
最もメジャーなHTTPは同期型といい、
- クライアントからサーバーへつなぎに行く。
- クライアントからサーバへデータを送信する。
- サーバーからクライアントへデータを返信する。
- 接続を切断する。
以上をセットで繰り返し、データの交換を行ないます。 HTTPではこの手順が規定されているので、手順3.に進んでから手順2.をやりたかったら 手順4.まで進めて最初から手順を踏んでいかなければなりません。 手順3.だけを繰り返したい場合も基本のこの手順セットを繰り返す必要があります。
これが双方向ストリーム通信の場合、手順1.を予め実行し繋ぎっぱなしにします。 手順2.や手順3.は随時好きなタイミングで実施できます。 これは手順4.を実施するまで任意の回数手順2.や手順3.を実行できます。
node.js socket.ioモジュールを使い、双方向通信もできる
↓↓↓↓↓
http://m.mkexdev.net/337
メモ)
・npmプロジェクト生成
express socket
・プロジェクトのフォルダに移動し、expressモジュール、socket.ioモジュールをinstall
npm install express
npm install socket.io
http://html5dev.tistory.com/346
http://qiita.com/n0bisuke/items/cb6216dbb9c3c13a10a8
https://gist.github.com/martinsik/2031681
to serve the frontend.html direct from the chat-server.js you need to do these:
install finalhandler and serve-static with the npm
npm install finalhandler serve-static
in the file chat-server.js replace line 39
// Not important for us. We're writing WebSocket server, not HTTP server
with
var done = finalhandler(request, response);
serve(request, response, done);
at line 21 add these lines
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
int the file frontend.html replace line 26
with
<script src="./frontend.js"></script>
with
<script src="./chat-frontend.js"></script>
finally call the chat page with
http://127.0.0.1:1337/frontend.html
댓글 없음:
댓글 쓰기