public Channel(string channel_name, Pusher pusher)
 {
     this.pusher           = pusher;
     this.name             = channel_name;
     this.callbacks        = new Dictionary <string, Callbacks>();
     this.global_callbacks = new Callbacks();
     this.subscribed       = false;
 }
Beispiel #2
0
 public static void Initialize()
 {
     if (!_initialized)
     {
         _initialized = true;
         Pusher.Ready();
     }
 }
 internal void DispatchWithAll(string event_name, object data)
 {
     if (this.name != "pusher_global_channel")
     {
         Pusher.Log("Pusher : event recd (channel,event,data)", this.name, event_name, data);
     }
     this.Dispatch(event_name, data);
     this.DispatchGlobalCallbacks(event_name, data);
 }
 private void buttonConnect_Click(object sender, EventArgs e)
 {
     _pusher = new Pusher(textBoxApiKey.Text);
     _pusherChannel = _pusher.Subscribe(textBoxChannel.Text);
     foreach (string channel in textBoxEvents.Text.Split(','))
         _pusherChannel.Bind(channel.Trim(), d => MessageBox.Show(Pusher.JSON.stringify(d)));
     buttonConnect.Enabled = false;
     buttonDisconnect.Enabled = true;
 }
Beispiel #5
0
        //static string channel_auth_transport = "ajax";

        public static object Parser(string data)
        {
            try
            {
                return(JSON.parse(data));
            }
            catch
            {
                Pusher.Log("Pusher : data attribute not valid JSON - you may wish to implement your own Pusher.parser");
                return(data);
            }
        }
Beispiel #6
0
        public void Connect()
        {
            string url;

            if (this.encrypted || this.secure)
            {
                url = "wss://" + Pusher.host + ":" + Pusher.wss_port + this.path;
            }
            else
            {
                url = "ws://" + Pusher.host + ":" + Pusher.ws_port + this.path;
            }

            Pusher.allow_reconnect = true;
            Pusher.Log("Pusher : connecting : " + url);

            var self = this;

            IWebSocket ws = CreateWebSocket(url);

            // Timeout for the connection to handle silently hanging connections
            // Increase the timeout after each retry in case of extreme latencies
            int interval = Pusher.connection_timeout + (this.retry_counter * 1000);

            var timerRef = new TimerRef();

            timerRef.Ref = new Timer(delegate(object state)
            {
                Pusher.Log("Pusher : connection timeout after " + interval + "ms");
                ws.Close();
                try { timerRef.Ref.Dispose(); }
                catch { }
            }, null, interval, interval);

            ws.OnData  += (sender, e) => OnMessage(e);
            ws.OnClose += (sender, e) =>
            {
                try { timerRef.Ref.Dispose(); }
                catch { }
                OnClose();
            };
            ws.OnOpen += (sender, e) =>
            {
                try { timerRef.Ref.Dispose(); }
                catch { }
                OnOpen();
            };

            this.connection = ws;

            ws.Open();
        }
Beispiel #7
0
 public void ToggleSecure()
 {
     if (this.secure == false)
     {
         this.secure = true;
         Pusher.Log("Pusher : switching to wss:// connection");
     }
     else
     {
         this.secure = false;
         Pusher.Log("Pusher : switching to ws:// connection");
     };
 }
 public Channel Add(string channel_name, Pusher pusher)
 {
     if (!this.ContainsKey(channel_name))
     {
         var channel = Channel.factory(channel_name, pusher);
         this[channel_name] = channel;
         return(channel);
     }
     else
     {
         return(this[channel_name]);
     }
 }
        private void InitializePusher()
        {
            Pusher.host = "ws.staging.pusherapp.com";
            Pusher.ws_port = 4502;
            Pusher.wss_port = 4503;
            string rootUri = Application.Current.Host.Source.Scheme + "://" + Application.Current.Host.Source.Host;
            string authUri = rootUri + "/pusher/auth/";
            Pusher.channel_auth_endpoint = authUri;

            Pusher.OnLog += new PusherLogHandler(Pusher_OnLog);
            _pusher = new Pusher("YOUR_APP_KEY");

            _pusher.Bind("open", Opened);
        }
 internal void Dispatch(string event_name, object event_data)
 {
     if (this.callbacks.ContainsKey(event_name))
     {
         foreach (Action <object> callback in this.callbacks[event_name])
         {
             callback(event_data);
         }
     }
     else if (!this.global)
     {
         Pusher.Log("Pusher : No callbacks for " + event_name);
     }
 }
Beispiel #11
0
        public Pusher(string application_key, Dictionary <string, object> options)
        {
            Pusher.Initialize();
            if (options != null)
            {
                this.options = options;
            }
            this.path                  = "/app/" + application_key + "?client=js&version=" + Pusher.VERSION;
            this.key                   = application_key;
            this.channels              = new Channel.Channels();
            this.global_channel        = new Channel("pusher_global_channel");
            this.global_channel.global = true;
            this.secure                = false;
            this.connected             = false;
            this.retry_counter         = 0;
            if (options != null && options.ContainsKey("encrypted"))
            {
                this.encrypted = ((bool)this.options["encrypted"]) ? true : false;
            }
            if (Pusher.isReady)
            {
                this.Connect();
            }
            Pusher.instances.Add(this);

            //This is the new namespaced version
            this.Bind("pusher:connection_established", d =>
            {
                JsonData data      = (JsonData)d;
                this.connected     = true;
                this.retry_counter = 0;
                this.socket_id     = (string)data["socket_id"];
                this.SubscribeAll();
            });

            this.Bind("pusher:connection_disconnected", d =>
            {
                foreach (string channel_name in this.channels.Keys.ToList <string>())
                {
                    this.channels[channel_name].Disconnect();
                }
            });

            this.Bind("pusher:error", d =>
            {
                JsonData data = (JsonData)d;
                Pusher.Log("Pusher : error : " + (string)data["message"]);
            });
        }
        internal static Channel factory(string channel_name, Pusher pusher)
        {
            var channel = new Channel(channel_name, pusher);

            if (channel_name.IndexOf(Channel.private_prefix) == 0)
            {
                channel.IsPrivate = true;
            }
            else if (channel_name.IndexOf(Channel.presence_prefix) == 0)
            {
                throw new Exception("PusherClientDotNet: Presense channels not implemented yet");
                //Pusher.Util.extend(channel, Pusher.Channel.PrivateChannel);
                //Pusher.Util.extend(channel, Pusher.Channel.PresenceChannel);
            }
            ;
            //channel.Init();// inheritable constructor
            return(channel);
        }
Beispiel #13
0
        internal Pusher SendEvent(string event_name, JsonData data, string channel)
        {
            Pusher.Log("Pusher : event sent (channel,event,data) : ", channel, event_name, data);

            var payload = new JsonData()
            {
                { "event", event_name },
                { "data", data }
            };

            if (channel != null)
            {
                payload["channel"] = channel;
            }

            this.connection.SendMessage(JSON.stringify(payload));
            return(this);
        }
Beispiel #14
0
        private void SendLocalEvent(string event_name, object event_data, string channel_name)
        {
            event_data = Pusher.DataDecorator(event_name, event_data);
            if (channel_name != null)
            {
                if (this.channels.ContainsKey(channel_name))
                {
                    Channel channel = this.GetChannel(channel_name);
                    channel.DispatchWithAll(event_name, event_data);
                }
            }
            else
            {
                // Bit hacky but these events won't get logged otherwise
                Pusher.Log("Pusher : event recd (event,data) :", event_name, event_data);
            }

            this.global_channel.DispatchWithAll(event_name, event_data);
        }
Beispiel #15
0
 private void OnClose()
 {
     this.global_channel.Dispatch("close", null);
     Pusher.Log("Pusher : Socket closed");
     if (this.connected)
     {
         this.SendLocalEvent("pusher:connection_disconnected", new JsonData());
         if (Pusher.allow_reconnect)
         {
             Pusher.Log("Pusher : Connection broken, trying to reconnect");
             this.Reconnect();
         }
     }
     else
     {
         this.SendLocalEvent("pusher:connection_failed", null);
         this.RetryConnect();
     }
     this.connected = false;
 }
Beispiel #16
0
 public GameHost()
 {
     this.client = new Pusher(APP_KEY);
     this.server = new PusherProvider(APP_ID, APP_KEY, SECRET);
 }
        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);
        }