Example #1
0
		protected override void OnReceived(dotnet2.Newtonsoft.Json.Linq.JToken message)
		{
			var invocation = message.ToObject<HubInvocation>();
			HubProxy hubProxy;
			if (_hubs.TryGetValue(invocation.Hub, out hubProxy))
			{
				if (invocation.State != null)
				{
					foreach (var state in invocation.State)
					{
						hubProxy[state.Key] = state.Value;
					}
				}

				hubProxy.InvokeEvent(invocation.Method, invocation.Args);
			}

			base.OnReceived(message);
		}
 public DisposableAction(dotnet2::System.Action action)
 {
     _action = action;
 }
 protected override void OnStart(IConnection connection, string data, dotnet2::System.Action initializeCallback, Action<Exception> errorCallback)
 {
     OpenConnection(connection, data, initializeCallback, errorCallback);
 }
        private void OpenConnection(IConnection connection, string data, dotnet2::System.Action initializeCallback, Action<Exception> errorCallback)
        {
            // If we're reconnecting add /connect to the url
            bool reconnecting = initializeCallback == null;

            var url = (reconnecting ? connection.Url : connection.Url + "connect");

            Action<IRequest> prepareRequest = PrepareRequest(connection);

            EventSignal<IResponse> signal;

            if (shouldUsePost(connection))
            {
                url += GetReceiveQueryString(connection, data);

                Debug.WriteLine(string.Format("SSE: POST {0}", url));

                signal = _httpClient.PostAsync(url, request =>
                                                        {
                                                            prepareRequest(request);
                                                            request.Accept = "text/event-stream";
                                                        }, new Dictionary<string, string> {{"groups", GetSerializedGroups(connection)}});
            }
            else
            {
                url += GetReceiveQueryStringWithGroups(connection, data);

                Debug.WriteLine(string.Format("SSE: GET {0}", url));

                signal = _httpClient.GetAsync(url, request =>
                {
                    prepareRequest(request);

                    request.Accept = "text/event-stream";
                });
            }

            signal.Finished += (sender,e)=> {
                if (e.Result.IsFaulted)
                {
                    var exception = e.Result.Exception.GetBaseException();
                    if (!IsRequestAborted(exception))
                    {
                        if (errorCallback != null &&
                            Interlocked.Exchange(ref _initializedCalled, 1) == 0)
                        {
                            errorCallback(exception);
                        }
                        else if (reconnecting)
                        {
                            // Only raise the error event if we failed to reconnect
                            connection.OnError(exception);
                        }
                    }

                    if (reconnecting)
                    {
                        // Retry
                        Reconnect(connection, data);
                        return;
                    }
                }
                else
                {
                    // Get the reseponse stream and read it for messages
                    var response = e.Result;
                    var stream = response.GetResponseStream();
                    var reader = new AsyncStreamReader(stream,
                                                       connection,
                                                       () =>
                                                       {
                                                           if (Interlocked.CompareExchange(ref _initializedCalled, 1, 0) == 0)
                                                           {
                                                               initializeCallback();
                                                           }
                                                       },
                                                       () =>
                                                       {
                                                           response.Close();

                                                           Reconnect(connection, data);
                                                       });

                    if (reconnecting)
                    {
                        // Raise the reconnect event if the connection comes back up
                        connection.OnReconnected();
                    }

                    reader.StartReading();

                    // Set the reader for this connection
                    connection.Items[ReaderKey] = reader;
                }
            };

            if (initializeCallback != null)
            {
                Thread.Sleep(ConnectionTimeout);
                if (Interlocked.CompareExchange(ref _initializedCalled, 1, 0) == 0)
                {
                    // Stop the connection
                    Stop(connection);

                    // Connection timeout occured
                    errorCallback(new TimeoutException());
                }
            }
        }
 public AsyncStreamReader(Stream stream, IConnection connection, dotnet2::System.Action initializeCallback, dotnet2::System.Action closeCallback)
 {
     _initializeCallback = initializeCallback;
     _closeCallback = closeCallback;
     _stream = stream;
     _connection = connection;
     _buffer = new ChunkBuffer();
 }