4

Ratchet Web Socket Server を作成し、SESSIONS を使用しようとしました。

HTTP Web サーバー (ポート 80) 上の私の php ファイルで、セッション データを次のように設定します。

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;

$memcache = new Memcache;
$memcache->connect('localhost', 11211);

$storage = new NativeSessionStorage(array(), new MemcacheSessionHandler($memcache));
$session = new Session($storage);
$session->start();

$session->set('uname', $uname);

Javascriptを使用してRatchet Websocketサーバーに接続します

var RatchetClient = {

    url: "ws://192.168.1.80:7070",

    ws: null,

    init: function() {

        var root = this;
        this.ws = new WebSocket(RatchetClient.url);

        this.ws.onopen = function(e) {
            console.log("Connection established!");
            root.onOpen();
        };

        this.ws.onmessage = function(evt) {
            console.log("Message Received : " + evt.data);
            var obj = JSON.parse(evt.data);
            root.onMessage(obj);
        };

        this.ws.onclose = function(CloseEvent) {
        };

        this.ws.onerror = function() {
        };
    },

    onMessage : function(obj) {    
    },

    onOpen : function() {        
    }
};

サーバースクリプトは、ここで説明されているように機能します: http://socketo.me/docs/sessions

クライアントがメッセージを送信すると、セッションデータを取得します

$memcache = new Memcache;
$memcache->connect('localhost', 11211);

$session = new SessionProvider(
    new MyServer()
  , new Handler\MemcacheSessionHandler($memcache)
);


$server = IoServer::factory(
    new HttpServer(
        new WsServer($session)
    )
  , 7070
);

$server->run();



class MyServer implements MessageComponentInterface {

    public function onMessage(ConnectionInterface $conn, $msg) {

        $name = $conn->Session->get("uname");

    }
}

できます。websocket に接続する前にセッション データを設定すると、ソケット サーバー スクリプト内で uname を取得できます。

ajax または別のブラウザ ウィンドウからセッション データを変更すると、実行中のクライアントのセッション データが同期されません。

つまり、uname を変更したり、セッションを破棄したりすると、ソケット サーバーはこれを認識しません。Ratchet が接続時に一度セッション データを読み取り、その後セッション オブジェクトが独立している場合のようです。

その行動を確認できますか?それとも私は何か間違ったことをしていますか?memcache を使用する目的は、接続されている異なるクライアントから同じセッション データにアクセスできるようにすることだと思いました。

セッションデータを変更した後に websocket に再接続すると、データが更新されています。

4

1 に答える 1

3

Ratchet が接続時に一度セッション データを読み取り、その後セッション オブジェクトが独立している場合のようです。

はい、そのように動作します。

https://groups.google.com/d/topic/ratchet-php/1wp1U5c12sU/discussion

于 2015-02-04T02:44:22.450 に答える