websocket with codeigniter and android



我有一个在CodeIgniter PHP中开发的Web应用程序,并且一个用于事件管理的Android应用程序我想做的是,每当admin在Web创建一个事件上,应生成通知并显示在Android中应用程序,因此所有使用Android应用程序的用户都可以在没有任何中断的情况下接收该通知。

所以任何人都知道如何实现此功能?我正在考虑使用Web套接字,但在CodeIgniter和Android中对此一无所知,因此任何形式的建议都会有所帮助。

我将使用以下组件:

  • Zeromq用于传递消息http://zeromq.org
  • Web插座服务器的棘轮http://socketo.me
  • Web-Socke客户端的Autobahn http://autobahn.ws/android/

我不知道您可以在Android方面使用什么来订阅Web-Service Server。

代码很简单,这是我在项目中使用的东西的一个示例。

Web插座服务器:

composer.json

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

push-server.php

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = ReactEventLoopFactory::create();
$pusher = new MyAppPusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new ReactZMQContext($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 ReactSocketServer($loop);
$webSock->listen(8081, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new RatchetServerIoServer(
        new RatchetHttpHttpServer(
        new RatchetWebSocketWsServer(
        new RatchetWampWampServer(
        $pusher
        )
        )
        ), $webSock
);
$loop->run();

然后在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));

我在上面使用此消息将消息发送给特定用户,但是如果您建立了一个连接所有用户的新频道,那么所有用户都会收到消息。

我的基于Web的应用程序使用http://autobahn.ws/js/在视图中订阅Web-Socket Feeds。我看到它也具有Android实现,但是我从未尝试过: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>

最新更新