/// <summary>
 /// Init constructor.
 /// </summary>
 public BayeuxException(string message, BayeuxRequest request, BayeuxResponse response, IJSonObject responseObject)
     : base(message)
 {
     Request = request;
     Response = response;
     JSonResponse = responseObject;
 }
 /// <summary>
 /// Init constructor.
 /// </summary>
 public BayeuxConnectionEventArgs(BayeuxConnection connection, HttpStatusCode statusCode, string statusDescription, string data, IJSonObject message, BayeuxResponse response)
 {
     Connection = connection;
     StatusCode = statusCode;
     StatusDescription = statusDescription;
     Data = data;
     Message = message;
     Response = response;
 }
        /// <summary>
        /// Process given response. Request is also provided.
        /// The default behavior is that it only passes response to request, however there might be
        /// a situation, when no request was given (when a 'text' message has been sent).
        /// Returning 'true' as a result will filter out the ResponseReceived event.
        /// </summary>
        protected virtual bool ProcessResponse(BayeuxRequest request, BayeuxResponse response)
        {
            if (request != null)
                request.ProcessResponse(response);
            else
            {
                DebugLog.WriteBayeuxLine("BayeuxConnection: Processing response ignored for null request!");
            }

            return false;
        }
        private void ProcessResponseMessage(BayeuxRequest request, HttpStatusCode httpStatusCode, string httpStatusDescription, IJSonObject message, string rawMessage)
        {
            try
            {

                string channel = message["channel"].StringValue; // each Bayuex message must have a channel associated!
                BayeuxResponse response = null;

                if (string.IsNullOrEmpty(channel))
                    throw new BayeuxException("Unexpected message with empty channel!");
                if (request != null && (channel.StartsWith("/meta", StringComparison.Ordinal) && channel != request.Channel))
                    throw new BayeuxException(string.Format(CultureInfo.InvariantCulture, "Unexpected response with channel: '{0}'", channel), request, null, message);
                if (request != null && channel.StartsWith("/meta", StringComparison.Ordinal) && request.ID != message["id"].StringValue)
                    throw new BayeuxException(string.Format(CultureInfo.InvariantCulture, "Invalid response ID, current: '{0}', expected: '{1}'", request.ID, message["id"].StringValue), request, null,
                                              message);

                ///////////////////////////////////////////////////////////////////////////////////////////
                // identify meta messages:
                if (channel == HandshakeRequest.MetaChannel)
                {
                    var handshakeResponse = new HandshakeResponse(message);
                    response = handshakeResponse;

                    // inform, that connection succeeded:
                    if (handshakeResponse.Successful)
                    {
                        ClientID = response.ClientID;

                        if (string.IsNullOrEmpty(ClientID))
                        {
                            throw new BayeuxException("Invalid ClientID received from server", request, handshakeResponse, message);
                        }

                        OnConnected(new BayeuxConnectionEventArgs(this, httpStatusCode, httpStatusDescription, rawMessage, message, handshakeResponse));
                    }
                    else
                    {
                        // inform that Handshake failed, via dedicated event:
                        ClientID = null;
                        OnConnectionFailed(new BayeuxConnectionEventArgs(this, response.Successful ? HttpStatusCode.OK : HttpStatusCode.BadRequest, null, rawMessage, message, response));
                    }
                }

                if (channel == DisconnectRequest.MetaChannel)
                {
                    response = new BayeuxResponse(message);

                    // inform that disconnection succeeded:
                    _state = BayeuxConnectionState.Disconnected;
                    ClientID = null;

                    OnDisconnected(new BayeuxConnectionEventArgs(this, response.Successful ? HttpStatusCode.OK : HttpStatusCode.BadRequest, null, rawMessage, message, response));
                }

                if (channel == SubscribeRequest.MetaChannel)
                {
                    var subscribeResponse = new SubscribeResponse(message);
                    response = subscribeResponse;

                    if (subscribeResponse.Successful)
                        _subscribedChannels.Add(subscribeResponse.SubscriptionChannel);
                }

                if (channel == UnsubscribeRequest.MetaChannel)
                {
                    var unsubscribeResponse = new UnsubscribeResponse(message);
                    response = unsubscribeResponse;

                    if (unsubscribeResponse.Successful)
                        _subscribedChannels.Remove(unsubscribeResponse.SubscriptionChannel);
                }

                if (_subscribedChannels.Contains(channel))
                {
                    response = new BayeuxResponse(message);

                    // event from server:
                    OnEventReceived(new BayeuxConnectionEventArgs(this, httpStatusCode, httpStatusDescription, rawMessage, message, response));
                }

                // process generic response:
                if (response == null)
                    response = ProvideResponse(message);

                // allow filtering of ResponseReceived event:
                if (ProcessResponse(request, response))
                    return;

                // one place to process all responses:
                OnResponseReceived(new BayeuxConnectionEventArgs(this, httpStatusCode, httpStatusDescription, rawMessage, message, response));
            }
            catch (Exception ex)
            {
                DebugLog.WriteBayeuxException(ex);

                string statusDescription = string.Concat(httpStatusDescription, "; unexpected exception: \"", ex.Message, "\"");
                OnDataFailed(new BayeuxConnectionEventArgs(this, httpStatusCode, statusDescription, rawMessage, message));
            }
        }
Example #5
0
 /// <summary>
 /// Processes response received from remote server.
 /// </summary>
 public virtual void ProcessResponse(BayeuxResponse response)
 {
 }