公式:
http://socketo.me/
GitHub:
https://github.com/cboden/Ratchet
GitHubのスターの数もそれなりだったので、どんな感じか確認してみました。以下、手順です。尚、Macで確認しています。
(1) composer.jsonを用意して、いつものコマンドを実行します。
http://socketo.me/docs/install を参考にしています。
{
"require": {
"cboden/Ratchet": "0.2.*"
}
}
$ curl -s https://getcomposer.org/installer | php $ php composer.phar install(2) サンプルファイルを配置する為のappディレクトリを作成します。
$ mkdir app $ ls app composer.json composer.lock composer.phar vendor(3) app/chat.phpを作ります。所謂、サーバサイドの処理です。
ポートと、autoload.phpとの位置関係以外は https://github.com/cboden/Ratchet#a-quick-server-example の通りです。
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
require __DIR__.'/../vendor/autoload.php';
/**
* chat.php
* Send any incoming messages to all connected clients (except sender)
*/
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client) {
if ($from != $client) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
// Run the server application through the WebSocket protocol on port 8080
$server = IoServer::factory(new WsServer(new Chat), 9000);
$server->run();
(4) app/chat.phpを起動します。
$ php app/chat.php(5) app/chat.htmlを作ります。所謂、クライアント(ブラウザ)サイドの処理です。
http://socketo.me/docs/hello-world を参考にしています。
<html>
<head>
<script>
var conn = new WebSocket('ws://localhost:9000');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
function send() {
var msg = document.getElementById("text").value;
conn.send(msg);
}
</script>
</head>
<body>
<input type="text" id="text" />
<input type="button" id="button" value="Send" onclick="send();" />
</body>
</html>
(6) 二つのブラウザからアクセスします。* SafariとChromeで試してみました。
* 右がChromeで、コンソールを開いています。"Connection established!"と出力されているのがわかります。
Safariから適当にメッセージを入力してSendボタンを押してみます。
きました!
ちょっと前にPHP + Redis + Node.jsとかで考えてみたんですけど、暗号化/復号化周りの話で、セッションの共有が厄介そうでした。
(FuelPHPじゃなくても同じ問題は出てくるでしょう。)
でも、この方法なら、けっこういけるんじゃないだろうか。。。
P.S.
console.logしているだけなので、全然チャットサンプルではなくてすみませんm(_ _)m


No comments:
Post a Comment