Esempio n. 1
0
    // ****************************************************************************************************



    // ****************************************************************************************************
    // Subscribe to a Pubnub Channel
    // ****************************************************************************************************
    public void Subscribe(string channel, stringCallback theCallback)
    {
        if (!threadPool.ContainsKey(channel))
        {
            Thread oThread = new Thread(new ThreadStart(() => { SubscribeThread(channel, theCallback); }));
            oThread.IsBackground = true;
            threadPool.Add(channel, oThread);
            oThread.Start();
        }
        else
        {
            Thread oThread = (Thread)threadPool[channel];
            if (!oThread.IsAlive)
            {
                threadPool.Remove(channel);
                oThread = new Thread(new ThreadStart(() => { SubscribeThread(channel, theCallback); }));
                oThread.IsBackground = true;
                threadPool.Add(channel, oThread);
                oThread.Start();
            }
        }
    }
Esempio n. 2
0
    private void PublishThread(string channel, object theMessage, stringCallback theCallback)
    {
        string output      = "";
        string queryString = "";

        try
        {
            queryString = "publish/" + pubnubPubKey + "/" + pubnubSubKey + "/0/" + channel + "/0/";
            if (theMessage is string)
            {
                queryString += "\"" + (string)theMessage + "\"";
            }
            else if ((theMessage is ArrayList) || (theMessage is Hashtable))
            {
                queryString += JSON.JsonEncode(theMessage);
            }
            else // Probably a number
            {
                queryString += theMessage.ToString();
            }

            WebRequest objRequest = (HttpWebRequest)WebRequest.Create(PubnubURL + queryString);

            WebResponse objResponse = (WebResponse)objRequest.GetResponse();
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                output += sr.ReadToEnd();
                sr.Close();
            }
        }
        catch (Exception e)
        {
            output = "error";
        }

        theCallback(output);
    }
Esempio n. 3
0
    public void PublishThread(string channel, object theMessage, stringCallback successFunction, stringCallback failureFunction)
    {
        string output = "";

        try
        {
            string queryString = "publish/" + pubnubPubKey + "/" + pubnubSubKey + "/0/" + channel + "/0/";
            if (theMessage is string)
            {
                queryString += "\"" + (string)theMessage + "\"";
            }
            else if ((theMessage is ArrayList) || (theMessage is Hashtable))
            {
                queryString += JSON.JsonEncode(theMessage);
            }
            else // Probably a number
            {
                queryString += theMessage.ToString();
            }

            WebClient client = new MyWebClient();
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            Stream       data   = client.OpenRead(PubnubURL + queryString);
            StreamReader reader = new StreamReader(data);
            output = reader.ReadToEnd();
            data.Close();
            reader.Close();

            successFunction(output);
        }
        catch (Exception e)
        {
            output = "error : " + e.Message + " " + e.Source + " " + e.StackTrace;
            failureFunction(output);
        }
    }
Esempio n. 4
0
    private void SubscribeThread(string channel, stringCallback theCallback)
    {
        string     output      = "";
        string     queryString = "";
        string     timeToken   = "0";
        WebRequest objRequest  = null;

        Debug.Log("Thread " + channel + " started");

        while (!stopped && threadPool.ContainsKey(channel))
        {
            try
            {
                // Create the Query URL
                queryString = "subscribe/" + pubnubSubKey + "/" + channel + "/0/" + timeToken;

                WebClient client = new MyWebClient();
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                Stream       data   = client.OpenRead(PubnubURL + queryString);
                StreamReader reader = new StreamReader(data);
                output = reader.ReadToEnd();
                data.Close();
                reader.Close();

                // Convert it to a form we can work with, namely an Array with the first element an Array of messages
                // And the second element the timeToken
                ArrayList outputArray = (ArrayList)JSON.JsonDecode(output);
                if (outputArray != null)
                {
                    // The timeToken, used to make sure we get new messages next time around
                    timeToken = (string)outputArray[1];

                    // The messages
                    ArrayList messageArray = (ArrayList)outputArray[0];

                    // Call the Callback function for each message, turning it into text on the way if necessary
                    foreach (object message in messageArray)
                    {
                        if (message is string)
                        {
                            theCallback((string)message);
                        }
                        else if ((message is Hashtable) || (message is ArrayList))
                        {
                            theCallback(JSON.JsonEncode(message));
                        }
                        else // Probably a number
                        {
                            theCallback(message.ToString());
                        }
                    }
                }
            }
            catch (WebException w)
            {
                if (objRequest != null)
                {
                    objRequest.Abort();
                }
            }
            catch (ThreadAbortException a)
            {
                if (objRequest != null)
                {
                    objRequest.Abort();
                }
            }
            catch (ThreadInterruptedException i)
            {
                if (objRequest != null)
                {
                    objRequest.Abort();
                }
            }
            catch (Exception e)
            {
                if (objRequest != null)
                {
                    objRequest.Abort();
                }
            }

            // Give some other threads a chance
            Thread.Sleep(100);
        }

        if (objRequest != null)
        {
            objRequest.Abort();
        }
        Debug.Log("Thread " + channel + " stopped");
    }
Esempio n. 5
0
    // ****************************************************************************************************



    // ****************************************************************************************************
    // Publish a text, Array or Hashtable message to the Pubnub Channel
    // ****************************************************************************************************
    public void Publish(string channel, object theMessage, stringCallback successFunction, stringCallback failureFunction)
    {
        Thread oThread = new Thread(new ThreadStart(() => { PublishThread(channel, theMessage, successFunction, failureFunction); }));

        oThread.Start();
    }
Esempio n. 6
0
    private void SubscribeThread(string channel, stringCallback theCallback)
    {
        string output      = "";
        string queryString = "";
        string timeToken   = "0";

        Debug.Log("Thread " + channel + " started");

        while (!stopped && threadPool.ContainsKey(channel))
        {
            try
            {
                // Create the Query URL
                queryString = "subscribe/" + pubnubSubKey + "/" + channel + "/0/" + timeToken;
                WebRequest objRequest = (HttpWebRequest)WebRequest.Create(PubnubURL + queryString);

                objRequest.Timeout = 10000;

                // Send the URL and get the Response
                WebResponse objResponse = (WebResponse)objRequest.GetResponse();

                // Get the Response.  Note that this will pause here if no new messages are waiting, and keep
                // the http channel open.
                output = "";
                using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                {
                    output += sr.ReadToEnd();
                    sr.Close();
                }

                // Convert it to a form we can work with, namely an Array with the first element an Array of messages
                // And the second element the timeToken
                ArrayList outputArray = (ArrayList)JSON.JsonDecode(output);
                if (outputArray != null)
                {
                    // The timeToken, used to make sure we get new messages next time around
                    timeToken = (string)outputArray[1];

                    // The messages
                    ArrayList messageArray = (ArrayList)outputArray[0];

                    // Call the Callback function for each message, turning it into text on the way if necessary
                    foreach (object message in messageArray)
                    {
                        if (message is string)
                        {
                            theCallback((string)message);
                        }
                        else if ((message is Hashtable) || (message is ArrayList))
                        {
                            theCallback(JSON.JsonEncode(message));
                        }
                        else // Probably a number
                        {
                            theCallback(message.ToString());
                        }
                    }
                }
            }
            catch (WebException w)
            {
                //Debug.Log ("Time out");
            }
            catch (Exception e)
            {
                //theCallback(e.Message);
            }

            // Give some other threads a chance
            Thread.Sleep(100);
        }
        Debug.Log("Thread " + channel + " stopped");
    }
Esempio n. 7
0
    // ****************************************************************************************************



    // ****************************************************************************************************
    // Publish a text, Array or Hashtable message to the Pubnub Channel
    // ****************************************************************************************************
    public void Publish(string channel, object theMessage, stringCallback theCallback)
    {
        Thread oThread = new Thread(new ThreadStart(() => { PublishThread(channel, theMessage, theCallback); }));

        oThread.Start();
    }
Esempio n. 8
0
    private void SubscribeThread(string channel, stringCallback theCallback)
    {
        string output = "";
        string queryString = "";
        string timeToken = "0";
        Debug.Log ("Thread " + channel + " started");

        while (!stopped && threadPool.ContainsKey (channel))
        {
            try
            {
                // Create the Query URL
                queryString = "subscribe/" + pubnubSubKey + "/" + channel + "/0/" + timeToken;
                WebRequest objRequest = (HttpWebRequest)WebRequest.Create(PubnubURL + queryString);

                objRequest.Timeout = 10000;

                // Send the URL and get the Response
                WebResponse objResponse = (WebResponse)objRequest.GetResponse();

                // Get the Response.  Note that this will pause here if no new messages are waiting, and keep
                // the http channel open.
                output = "";
                using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                {
                    output += sr.ReadToEnd();
                    sr.Close();
                }

                // Convert it to a form we can work with, namely an Array with the first element an Array of messages
                // And the second element the timeToken
                ArrayList outputArray = (ArrayList)JSON.JsonDecode(output);
                if (outputArray != null)
                {
                    // The timeToken, used to make sure we get new messages next time around
                    timeToken = (string)outputArray[1];

                    // The messages
                    ArrayList messageArray = (ArrayList)outputArray[0];

                    // Call the Callback function for each message, turning it into text on the way if necessary
                    foreach (object message in messageArray)
                    {
                        if (message is string)
                        {
                            theCallback((string)message);
                        }
                        else if ((message is Hashtable) || (message is ArrayList))
                        {
                            theCallback(JSON.JsonEncode(message));
                        }
                        else // Probably a number
                        {
                            theCallback(message.ToString());
                        }
                    }
                }
            }
            catch (WebException w)
            {
                //Debug.Log ("Time out");
            }
            catch (Exception e)
            {
                //theCallback(e.Message);
            }

            // Give some other threads a chance
            Thread.Sleep(100);
        }
        Debug.Log ("Thread " + channel + " stopped");
    }
Esempio n. 9
0
    private void PublishThread(string channel, object theMessage, stringCallback theCallback)
    {
        string output = "";
        string queryString = "";

        try
        {
            queryString = "publish/" + pubnubPubKey + "/" + pubnubSubKey + "/0/" + channel + "/0/";
            if (theMessage is string)
            {
                queryString += "\"" + (string)theMessage + "\"";
            }
            else if ((theMessage is ArrayList) || (theMessage is Hashtable))
            {
                queryString += JSON.JsonEncode(theMessage);
            }
            else // Probably a number
            {
                queryString += theMessage.ToString();
            }

            WebRequest objRequest = (HttpWebRequest)WebRequest.Create(PubnubURL + queryString);

            WebResponse objResponse = (WebResponse)objRequest.GetResponse();
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                output += sr.ReadToEnd();
                sr.Close();
            }
        }
        catch (Exception e)
        {
            output = "error";
        }

        theCallback(output);
    }
Esempio n. 10
0
 // ****************************************************************************************************
 // ****************************************************************************************************
 // Subscribe to a Pubnub Channel
 // ****************************************************************************************************
 public void Subscribe(string channel, stringCallback theCallback)
 {
     if (!threadPool.ContainsKey(channel))
     {
         Thread oThread = new Thread(new ThreadStart(() => { SubscribeThread(channel, theCallback); }));
         oThread.IsBackground = true;
         threadPool.Add(channel, oThread);
         oThread.Start();
     }
     else
     {
         Thread oThread = (Thread)threadPool[channel];
         if (!oThread.IsAlive)
         {
             threadPool.Remove(channel);
             oThread = new Thread(new ThreadStart(() => { SubscribeThread(channel, theCallback); }));
             oThread.IsBackground = true;
             threadPool.Add(channel, oThread);
             oThread.Start();
         }
     }
 }
Esempio n. 11
0
 // ****************************************************************************************************
 // ****************************************************************************************************
 // Publish a text, Array or Hashtable message to the Pubnub Channel
 // ****************************************************************************************************
 public void Publish(string channel, object theMessage, stringCallback theCallback)
 {
     Thread oThread = new Thread(new ThreadStart(() => { PublishThread(channel, theMessage, theCallback); }));
     oThread.Start();
 }