# location /channels-stats { push_stream_channels_statistics; push_stream_channels_path $arg_id; } # location /pub { push_stream_publisher admin; push_stream_channels_path $arg_id; push_stream_store_messages on; # , , } # location ~ ^/lp/(.*) { push_stream_subscriber long-polling; push_stream_channels_path $1; push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"tag\":\"~tag~\",\"time\":\"~time~\",\"text\":~text~}"; push_stream_longpolling_connection_ttl 30s; }
/* * $cids - ID , , - ID * $text - , */ public static function push($cids, $text) { $text = json_encode($text); $c = curl_init(); $url = 'http://example.com/pub?id='; curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_POST, true); $results = array(); if (!is_array($cids)) { $cids = array($cids); } $cids = array_unique($cids); foreach ($cids as $v) { curl_setopt($c, CURLOPT_URL, $url . $v); curl_setopt($c, CURLOPT_POSTFIELDS, $text); $results[] = curl_exec($c); } curl_close($c); }
[id ]_[ ]_[id ]
0_main_0
777_main_0
0_orderPriceChanged_777
const ACT_NEW_MESSAGE = 1; LongPolling::push($uid."_main_0", array( "act" => ACT_NEW_MESSAGE, "content" => "Hello, user ".$uid."!", ));
private final static int ACT_NEW_ORDER = 1; private final static int ACT_DEL_ORDER = 2; private final static int ACT_ATTRIBUTES_CHANGED = 3; private final static int ACT_MESSAGE = 4; private final JsonHttpResponseHandler handler = new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { JSONObject json = response.getJSONObject("text"); switch (json.getInt("act")) { case ACT_NEW_ORDER: ... break; case ACT_DEL_ORDER: ... break; case ACT_ATTRIBUTES_CHANGED: ... break; case ACT_MESSAGE: ... break; default: break; } } catch (JSONException e) { e.printStackTrace(); } } };
private LongPolling lp; private int uid = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_balance); lp = new LongPolling(getApplicationContext(), "http://example.com/lp/", Integer.toString(uid) + "_main_0", handler); }
public void onResume() { super.onResume(); lp.connect(); } public void onPause() { super.onPause(); lp.disconnect(); }
lp = new LongPolling(getApplicationContext(), "http://example.com/lp/", Integer.toString(uid) + "_main_0", handler); lp.connect();
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> ... <application> ... <receiver android:name="com.app.example.receivers.InternetStateReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" /> </intent-filter> </receiver> </application> </manifest>
public class InternetStateReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifi != null && wifi.isAvailable() || mobile != null && mobile.isAvailable()) { application.getInstance().lp.connect(); } else { application.getInstance().lp.disconnect(); } } }
http://example.com/channels-stats?id=ALL
<?xml version="1.0" encoding="UTF-8" ?> <root> <hostname>example.com</hostname> <time>2014-03-22T00:03:37</time> <channels>2</channels> <wildcard_channels>0</wildcard_channels> <uptime>818530</uptime> <infos> <channel> <name>4_main_0</name> <published_messages>0</published_messages> <stored_messages>0</stored_messages> <subscribers>1</subscribers> </channel> <channel> <name>23_main_0</name> <published_messages>0</published_messages> <stored_messages>0</stored_messages> <subscribers>1</subscribers> </channel> </infos> </root>
const STATISTICS_URL = 'http://example.com/channels-stats?id=ALL'; public static function getOnlineIds() { $str = file_get_contents(self::STATISTICS_URL); if (!$str) return; $json = json_decode($str); if (empty($json -> infos)) return; $ids = array(); foreach ($json->infos as $v) { if ($v -> subscribers > 0 && substr_count($v -> channel, '_main_0') > 0) { $ids[] = str_replace('_main_0', '', $v -> channel); } } return $ids; }
Source: https://habr.com/ru/post/216713/
All Articles