private void SocketMessageReceived(object sender,
                                           MessageReceivedArgs e)
        {
            var     array      = JsonConvert.DeserializeObject <JArray>(e.Message);
            dynamic messageObj = array[0];
            var     newAdvice  = ParseAdvice(messageObj);

            if (newAdvice != null)
            {
                _advice = newAdvice;
                SetRetry(_advice,
                         _connection);
            }
            if (HandleSynchronousReply(messageObj,
                                       e))
            {
                return;
            }
            if (HandleConnectResponse(messageObj))
            {
                return;
            }
            var message     = Converter.Deserialize <DataMessage>(e.Message);
            var channel     = message.Channel;
            var messageData = message.Data.ToString(CultureInfo.InvariantCulture);

            _logger.Debug("Message data received for channel '{0}' is '{1}",
                          channel,
                          messageData);
            _subscribedChannels[channel].ForEach(handler => handler(messageData));
        }
Esempio n. 2
0
 public FayeClient(IWebSocket socket,
                   string connectionId = "standard") : base(messageCounter: FIRST_MESSAGE_INDEX)
 {
     _connectionId    = connectionId;
     _transportClient = new WebsocketTransportClient(socket,
                                                     connectionId);
     _advice = DefaultAdvice;
     _transportConnection = null;
     _logger = LoggerFetcher.GetLogger(connectionId,
                                       this);
 }
 // internal to only allow FayeClient to instantiate this class
 internal FayeConnection(ITransportConnection connection,
                         HandshakeResponseMessage handshakeResponse,
                         int messageCounter,
                         Advice advice,
                         TimeSpan handshakeTimeout,
                         string connectionId) : base(messageCounter: messageCounter,
                                                     handshakeTimeout: handshakeTimeout)
 {
     _connection = connection;
     ClientId    = handshakeResponse.ClientId;
     _connection.MessageReceived += SocketMessageReceived;
     _subscribedChannels          = new Dictionary <string, List <Action <string> > >();
     _synchronousMessageEvents    = new Dictionary <int, TaskCompletionSource <MessageReceivedArgs> >();
     _advice = advice;
     _connection.ConnectionLost          += SocketConnectionLost;
     _connection.ConnectionReestablished += SocketConnectionReestablished;
     _logger = LoggerFetcher.GetLogger(connectionId,
                                       this);
 }
Esempio n. 4
0
        private async Task <T> ExecuteSynchronousMessage <T>(BaseFayeMessage message,
                                                             TimeSpan timeoutValue) where T : BaseFayeMessage
        {
            var             json     = Converter.Serialize(message);
            var             tcs      = new TaskCompletionSource <MessageReceivedArgs>();
            MessageReceived received = (sender,
                                        args) => tcs.SetResult(args);

            _transportConnection.MessageReceived += received;
            _transportConnection.Send(json);
            var task   = tcs.Task;
            var result = await task.Timeout(timeoutValue);

            if (result == Result.Timeout)
            {
                var timeoutException = new TimeoutException(timeoutValue,
                                                            json);
                _logger.ErrorException("Timeout problem, rethrowing",
                                       timeoutException);
                throw timeoutException;
            }
            var receivedString = task.Result.Message;

            _logger.Debug("Received message '{0}'",
                          receivedString);
            _transportConnection.MessageReceived -= received;
            var     array      = JsonConvert.DeserializeObject <JArray>(receivedString);
            dynamic messageObj = array[0];
            var     newAdvice  = ParseAdvice(messageObj);

            if (newAdvice != null)
            {
                _advice = newAdvice;
                SetRetry(_advice,
                         _transportConnection);
            }
            return(Converter.Deserialize <T>(receivedString));
        }
 protected static void SetRetry(Advice advice,
                                ITransportConnection transportConnection)
 {
     transportConnection.RetryEnabled = advice.Reconnect != Reconnect.None;
 }