private void OnEventSourceMessage(EventSource eventSource, BestHTTP.ServerSentEvents.Message message)
        {
            if (message.Data.Equals("initialized"))
            {
                base.OnConnected();

                return;
            }

            IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, message.Data);

            if (msg != null)
                Connection.OnMessage(msg);

        }
 /// <summary>
 /// Prepares the request by adding two headers to it
 /// </summary>
 public void PrepareRequest(BestHTTP.HTTPRequest request, RequestTypes type)
 {
     request.SetHeader("username", this.User);
     request.SetHeader("roles", this.Roles);
 }
Пример #3
0
        private void OnMessageReceived(EventSourceResponse resp, BestHTTP.ServerSentEvents.Message message)
        {
            if (this.State >= States.Closing)
                return;

            // 1.) Set the last event ID string of the event source to value of the last event ID buffer.
            // The buffer does not get reset, so the last event ID string of the event source remains set to this value until the next time it is set by the server.
            // We check here only for null, because it can be a non-null but empty string.
            if (message.Id != null)
                this.LastEventId = message.Id;

            if (message.Retry.TotalMilliseconds > 0)
                this.ReconnectionTime = message.Retry;

            // 2.) If the data buffer is an empty string, set the data buffer and the event type buffer to the empty string and abort these steps.
            if (string.IsNullOrEmpty(message.Data))
                return;

            // 3.) If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer.
            // This step can be ignored. We constructed the string to be able to skip this step.

            if (OnMessage != null)
            {
                try
                {
                    OnMessage(this, message);
                }
                catch (Exception ex)
                {
                    HTTPManager.Logger.Exception("EventSource", "OnMessageReceived - OnMessage", ex);
                }
            }

            if (!string.IsNullOrEmpty(message.Event))
            {
                OnEventDelegate action;
                if (EventTable.TryGetValue(message.Event, out action))
                {
                    if (action != null)
                    {
                        try
                        {
                            action(this, message);
                        }
                        catch(Exception ex)
                        {
                            HTTPManager.Logger.Exception("EventSource", "OnMessageReceived - action", ex);
                        }
                    }
                }
            }
        }
        public object[] Decode(BestHTTP.SocketIO.JsonEncoders.IJsonEncoder encoder)
        {
            if (IsDecoded || encoder == null)
                return DecodedArgs;

            IsDecoded = true;

            if (string.IsNullOrEmpty(Payload))
                return DecodedArgs;

            List<object> decoded = encoder.Decode(Payload);

            if (decoded != null && decoded.Count > 0)
            {
                if (this.SocketIOEvent == SocketIOEventTypes.Ack || this.SocketIOEvent == SocketIOEventTypes.BinaryAck)
                    DecodedArgs = decoded.ToArray();
                else
                {
                    decoded.RemoveAt(0);

                    DecodedArgs = decoded.ToArray();
                }
            }

            return DecodedArgs;
        }