Ejemplo n.º 1
0
        public Socket(String endpoint, Options options = null)
        {
            SendBuffer = new List <Action>();
            channels   = new List <Channel>();

            openCallbacks    = new List <Action>();
            closeCallbacks   = new List <Action <ushort, string> >();
            errorCallbacks   = new List <Action <string> >();
            messageCallbacks = new List <Action <string> >();

            this.Endpoint = endpoint + VSN_STRING;

            Ref = 0;

            this.options = options;

            if (options != null && options.Timeout != null)
            {
                this.Timeout = (TimeSpan)options.Timeout;
            }
            else
            {
                this.Timeout = TimeSpan.FromMilliseconds(DefaultTimeout);
            }

            if (options != null && options.Logger != null)
            {
                this.Logger = options.Logger;
            }
            else
            {
                this.Logger = new Logger();
            }
            //TODO: set default encoder / decoder
            //TODO: set encoder/decoder
            if (options != null && options.HeartbeatInterval != null)
            {
                this.HeartbeatInterval = (TimeSpan)options.HeartbeatInterval;
            }
            else
            {
                this.HeartbeatInterval = TimeSpan.FromMilliseconds(30000);
            }
            if (options != null && options.ReconnectAfter != null)
            {
                this.ReconnectAfter = options.ReconnectAfter;
            }
            else
            {
                this.ReconnectAfter = ReconnectAfterDefault;
            }

            this.HeartBeatTimer      = null;
            this.pendingHeartBeatRef = null;

            this.ReconnectTimer = new CallbackTimer(
                () => { this.Disconnect(() => this.Connect()); },
                this.ReconnectAfter
                );
        }
Ejemplo n.º 2
0
        internal Channel(string topic, Socket socket, JObject options)
        {
            this.Socket      = socket;
            this.state       = State.Closed;
            this.topic       = topic;
            this.timeout     = this.Socket.Timeout;
            this.options     = options;
            this.bindings    = new List <Binding>();
            this.joinedOnce  = false;
            this.joinPush    = new Push(this, PHX_JOIN, this.options, this.timeout);
            this.pushBuffer  = new List <Push>();
            this.rejoinTimer = new CallbackTimer(() => { this.RejoinUntilConnected(); }, this.Socket.ReconnectAfter);
            this.joinPush.Receive(Channel.Ok, () =>
            {
                this.state = Channel.State.Joined;
                this.rejoinTimer.Reset();
                this.pushBuffer.ForEach(pushEvent => pushEvent.Send());
                this.pushBuffer.Clear();
            });
            this.OnClose((input) =>
            {
                this.rejoinTimer.Reset();
                this.Socket.Log("channel", String.Format("close {0} {1}", topic, JoinRef()));
                this.state = Channel.State.Closed;
                this.Socket.Remove(this);
            });

            this.OnError(reason =>
            {
                if (this.IsLeaving() || this.IsClosed())
                {
                    return;
                }
                this.Socket.Log("channel", String.Format("error {0}", reason));
                this.state = State.Errored;
                this.rejoinTimer.ScheduleTimeout();
            });

            this.joinPush.Receive(Channel.Timeout, () =>
            {
                if (!this.IsJoining())
                {
                    return;
                }
                this.Socket.Log("channel", "timeout", "");
                var leavePush = new Push(this, PHX_LEAVE, null, timeout);
                leavePush.Send();
                this.state = State.Errored;
                this.joinPush.Reset();
                this.rejoinTimer.ScheduleTimeout();
            });

            this.On(PHX_REPLY, (resp) =>
            {
                this.Trigger(this.ReplyEventName(resp.Ref), resp);
            });
        }
Ejemplo n.º 3
0
        void CancelTimeout()
        {
            if (timeoutTimer != null)
            {
                this.timeoutTimer.Reset();
            }

            this.timeoutTimer = null;
        }
Ejemplo n.º 4
0
 public Push(Channel channel, string evnt, JObject payload, TimeSpan timeout)
 {
     this.channel          = channel;
     this.sEvent           = evnt;
     this.payload          = payload;
     this.timeout          = timeout;
     this.receivedResponse = null;
     this.timeoutTimer     = null;
     this.bindings         = new List <Binding>();
 }
Ejemplo n.º 5
0
 void OnConnectionOpen(object sender, EventArgs args)
 {
     Log("websocket", "open");
     FlushSendBuffer();
     ReconnectTimer.Reset();
     if (HeartBeatTimer == null)
     {
         HeartBeatTimer = new CallbackTimer(SendHeartBuffer, this.HeartbeatInterval);
     }
     else
     {
         this.HeartBeatTimer.Reset();
     }
     this.HeartBeatTimer.ScheduleTimeout();
     this.openCallbacks.ForEach(callback => callback());
 }
Ejemplo n.º 6
0
        internal void StartTimeout()
        {
            if (this.timeoutTimer == null)
            {
                this.CancelTimeout();
            }

            this.Ref      = this.channel.Socket.MakeRef();
            this.refEvent = this.channel.ReplyEventName(this.Ref);
            this.channel.On(this.refEvent, (resp) =>
            {
                this.CancelRefEvent();
                this.CancelTimeout();
                this.receivedResponse = resp;
                this.MatchReceive(resp);
            });
            this.timeoutTimer = new CallbackTimer(() => {
                this.Trigger(this.refEvent, Message.MessageStatusTimeout(this.refEvent));
            }, timeout);
        }