Exemple #1
0
        public Push Push(string event_, JObject payload = null)
        {
            if (!_alreadyJoinedOnce)
            {
                throw new Exception(string.Format("tried to push {0} to {1} before joining. Use Channel.Join() before pushing events", event_, payload));
            }

            var pushEvent = new Push(this, event_, payload);

            if (CanPush())
            {
                pushEvent.Send();
            }
            else
            {
                _pushBuffer.Add(pushEvent);
            }

            return(pushEvent);
        }
Exemple #2
0
        public Channel(string topic, JObject params_, Socket socket)
        {
            _state = ChannelState.Closed;
            Topic  = topic;

            Socket             = socket;
            _bindings          = new Dictionary <string, List <Action <JObject, string> > >();
            _alreadyJoinedOnce = false;
            _pushBuffer        = new List <Push>();

            JoinRef = Socket.MakeRef();

            _joinPush = new Push(this, ChannelEvents.Join, params_);
            _joinPush.Receive("ok", (x) =>
            {
                _state = ChannelState.Joined;
            });

            OnClose((o, reference) =>
            {
                _state = ChannelState.Closed;
                Socket.Remove(this);
            });

            OnError((reason, reference) => //reason is not used
            {
                _state = ChannelState.Errored;
                _rejoinTimer.Start();
            });

            On(ChannelEvents.Reply, (payload, reference) =>
            {
                Trigger(ReplyEventName(reference), payload, reference);
            });


            _rejoinTimer           = new Timer(Socket.ReconnectAfterMs);
            _rejoinTimer.AutoReset = false;
            _rejoinTimer.Elapsed  += (o, e) => RejoinUntilConnected();
            //_rejoinTimer.Enabled = true;
        }
        public Channel(string topic, JObject params_, Socket socket)
        {
            _state = ChannelState.Closed;
            Topic = topic;

            Socket = socket;
            _bindings = new Dictionary<string, List<Action<JObject, string>>>();
            _alreadyJoinedOnce = false;

            _joinPush = new Push(this, ChannelEvents.Join, params_);
            _pushBuffer = new List<Push>();

            _joinPush.Receive("ok", (x) =>
            {
                _state = ChannelState.Joined;
            });

            OnClose((o, reference) =>
            {
                _state = ChannelState.Closed;
                Socket.Remove(this);
            });

            OnError((reason, reference) => //reason is not used
            {
                _state = ChannelState.Errored;
                _rejoinTimer.Start();

            });

            On(ChannelEvents.Reply, (payload, reference) =>
            {
                Trigger(ReplyEventName(reference), payload, reference);
            });


            _rejoinTimer = new Timer(Socket.ReconnectAfterMs);
            _rejoinTimer.AutoReset = false;
            _rejoinTimer.Elapsed += (o, e) => RejoinUntilConnected();
            //_rejoinTimer.Enabled = true;
        }
        public Push Push(string event_, JObject payload = null)
        {
            if (!_alreadyJoinedOnce)
            {
                throw new Exception(string.Format("tried to push {0} to {1} before joining. Use Channel.Join() before pushing events", event_, payload));
            }

            var pushEvent = new Push(this, event_, payload);

            if (CanPush())
            {
                pushEvent.Send();
            }
            else
            {
                _pushBuffer.Add(pushEvent);
            }

            return pushEvent;
        }