$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>