예제 #1
0
        private void OnMessage(WebSocketEventArgs evt)
        {
            Pusher.Log("Pusher : OnMessage : ", evt.TextData);

            JsonData paramss = JSON.parse(evt.TextData);

            if (paramss.ContainsKey("socket_id") && paramss["socket_id"].ToString() == this.socket_id)
            {
                return;
            }
            // Try to parse the event data unless it has already been decoded
            if (paramss["data"] is string)
            {
                paramss["data"] = Pusher.Parser((string)paramss["data"]);
            }
            Pusher.Log("Pusher : received message : ", paramss);

            if (paramss.ContainsKey("channel"))
            {
                this.SendLocalEvent((string)paramss["event"], paramss["data"], (string)paramss["channel"]);
            }
            else
            {
                this.SendLocalEvent((string)paramss["event"], paramss["data"]);
            }
        }
        public void Authorize(Pusher pusher, Action <object> callback)
        {
            JsonData data;

            if (IsPrivate)
            {
                if (string.IsNullOrWhiteSpace(Pusher.channel_auth_endpoint))
                {
                    throw new InvalidOperationException("Pusher.channel_auth_endpoint must be set to authorize a channel");
                }

                string url = Pusher.channel_auth_endpoint + "?socket_id=" + pusher.socket_id + "&channel_name=" + this.name;

                AutoResetEvent downloadedEvent = new AutoResetEvent(false);
                string         response        = string.Empty;

                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method      = "POST";
                    request.Accept      = "application/json";
                    request.ContentType = "application/x-www-form-urlencoded";

                    bool supportsCookieContainer = true;
#if SILVERLIGHT
                    supportsCookieContainer = request.SupportsCookieContainer;
#endif
                    if (Pusher.AuthCookieContainer != null && supportsCookieContainer)
                    {
                        request.CookieContainer = Pusher.AuthCookieContainer;
                    }

                    request.BeginGetResponse(delegate(IAsyncResult asynchronousResult)
                    {
                        HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                        Stream stream        = resp.GetResponseStream();
                        var sr   = new StreamReader(stream);
                        response = sr.ReadToEnd();
                        downloadedEvent.Set();
                    }, null);

                    bool triggered = downloadedEvent.WaitOne(Pusher.connection_timeout);
                    if (!triggered)
                    {
                        Pusher.Log("Auth call timed out after {0} milliseconds", Pusher.connection_timeout);
                        data = new JsonData();
                    }
                    else
                    {
                        data = (JsonData)Pusher.Parser(response);
                    }
                }
                catch (Exception ex)
                {
                    Pusher.Log("Exception occurred in Auth call. {0}", ex);
                    data = new JsonData();
                }
            }
            else
            {
                data = new JsonData();
            }
            callback(data);
        }