2017-03-06 4 views
0

私はcodeigniter phpで開発されたWebアプリケーションを持っています。私がやりたいことは、Web上の管理者がイベントを作成するたびに通知が生成され、アンドロイドアプリに表示されるはずですAndroidアプリを使用しているユーザーは、割り込みなしでその通知を受け取ることができます。websocket with codeigniter and android

どのように私はこの機能を達成することができますか? 私はWebソケットを使用することを考えていますが、私はcodeigniterとandroidでそれについて何も考えていないので、どんな提案も役に立ちます。

答えて

1

私は、次のコンポーネントを使用します:ウェブsockeクライアントhttp://autobahn.ws/android/

Iのためのウェブソケットサーバ用のメッセージにhttp://socketo.me

  • アウトバーンをhttp://zeromq.org
  • ラチェットを通過させるための

    • ZeroMQをあなたがWebサービスサーバーに加入するためにアンドロイド側で使用できるものについては何の手がかりも持っていません。

      コードはかなりシンプルです。私のプロジェクトで使っているコードの例です。

      ウェブソケットサーバ:

      { 
          "autoload": { 
           "psr-0": { 
            "MyApp": "src" 
           } 
          }, 
          "require": { 
           "cboden/ratchet": "0.3.*", 
           "react/zmq": "0.2.*|0.3.*" 
          } 
      } 
      

      プッシュserver.phpという

      <?php 
      
      require dirname(__DIR__) . '/vendor/autoload.php'; 
      
      $loop = React\EventLoop\Factory::create(); 
      $pusher = new MyApp\Pusher; 
      
      // Listen for the web server to make a ZeroMQ push after an ajax request 
      $context = new React\ZMQ\Context($loop); 
      $pull = $context->getSocket(ZMQ::SOCKET_PULL); 
      // I assume the codeigniter installation and this server 
      // will be on the same host, hence 127.0.0.1 
      $pull->bind('tcp://127.0.0.1:5555'); 
      $pull->on('message', array($pusher, 'onMessage')); 
      
      // Set up our WebSocket server for clients wanting real-time updates 
      $webSock = new React\Socket\Server($loop); 
      $webSock->listen(8081, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect 
      $webServer = new Ratchet\Server\IoServer(
           new Ratchet\Http\HttpServer(
           new Ratchet\WebSocket\WsServer(
           new Ratchet\Wamp\WampServer(
           $pusher 
           ) 
           ) 
           ), $webSock 
      ); 
      
      $loop->run(); 
      

      composer.jsonはその後CodeIgniterの中で、あなたがメッセージを送信するために、以下を使用することができます:

      $context = new ZMQContext(); 
      $socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'pusher'); 
      $zmq_srv = 'your.domain.com:5555'; 
      $socket->connect("tcp://" . $zmq_srv); 
      
      $messageContent = array(
          'user' => 'username', 
          'type' => 'success', 
          'message' => 'Hi this is a test message.', 
      ); 
      
      $socket->send(json_encode($messageContent)); 
      

      私は特定のユーザーにメッセージを送信するためにこれを使用しますが、すべてのユーザーが接続されている新しいチャンネルを作成すると、すべてのユーザーにメッセージが送信されます。

      ウェブベースのアプリは、ビュー内でhttp://autobahn.ws/js/を使用してウェブソケットフィードを購読します。私はそれが同様にAndroidの実装を持って見たが、私は1という試みたことがありません:それはあなたにとって有用である場合にはhttp://autobahn.ws/android/

      これは私のビューのいずれかからのサンプルコードです:

      <script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script> 
      <script> 
           var conn = new ab.Session('ws://your.domain.com:8081', 
             function() { 
              // Subscribe to the "username" channel 
              // For each user this would be their own channel to receive notifications 
              // for their own events, like successful file generation.. 
              // file upload, etc... 
              conn.subscribe('username', function (topic, data) { 
               $.simplyToast(data.message, type = data.type, delay = 8000); 
              }); 
              // Subscribe to "system" channel. 
              //In my app all users are subscribed to this one to receive system-wide 
              // notifications. 
              conn.subscribe('system', function (topic, data) { 
               $.simplyToast(data.message, type = data.type, delay = 8000); 
              }); 
             }, 
             function() { 
              console.warn('WebSocket connection closed'); 
             }, 
             {'skipSubprotocolCheck': true} 
           ); 
          </script>