Пример #1
0
        /// <summary>
        /// This callback is called when the plugin is connected to the server successfully. Messages can be sent to the server after this point.
        /// </summary>
        private void Hub_OnConnected(HubConnection hub)
        {
            uiText += "Hub Connected\n";

            // Call a server function with a string param. We expect no return value.
            hub.Send("Send", "my message");

            // Call a parameterless function. We expect a string return value.
            hub.Invoke <string>("NoParam")
            .OnSuccess(ret => uiText += string.Format(" 'NoParam' returned: {0}\n", ret));

            // Call a function on the server to add two numbers. OnSuccess will be called with the result and OnError if there's an error.
            hub.Invoke <int>("Add", 10, 20)
            .OnSuccess(result => uiText += string.Format(" 'Add(10, 20)' returned: {0}\n", result))
            .OnError(error => uiText    += string.Format(" 'Add(10, 20)' error: {0}\n", error));

            hub.Invoke <int?>("NullableTest", 10)
            .OnSuccess(result => uiText += string.Format(" 'NullableTest(10)' returned: {0}\n", result))
            .OnError(error => uiText    += string.Format(" 'NullableTest(10)' error: {0}\n", error));

            // Call a function that will return a Person object constructed from the function's parameters.
            hub.Invoke <Person>("GetPerson", "Mr. Smith", 26)
            .OnSuccess(result => uiText += string.Format(" 'GetPerson(\"Mr. Smith\", 26)' returned: {0}\n", result))
            .OnError(error => uiText    += string.Format(" 'GetPerson(\"Mr. Smith\", 26)' error: {0}\n", error));

            // To test errors/exceptions this call always throws an exception on the server side resulting in an OnError call.
            // OnError expected here!
            hub.Invoke <int>("SingleResultFailure", 10, 20)
            .OnSuccess(result => uiText += string.Format(" 'SingleResultFailure(10, 20)' returned: {0}\n", result))
            .OnError(error => uiText    += string.Format(" 'SingleResultFailure(10, 20)' error: {0}\n", error));

            // This call demonstrates IEnumerable<> functions, result will be the yielded numbers.
            hub.Invoke <int[]>("Batched", 10)
            .OnSuccess(result => uiText += string.Format(" 'Batched(10)' returned items: {0}\n", result.Length))
            .OnError(error => uiText    += string.Format(" 'Batched(10)' error: {0}\n", error));

            // OnItem is called for a streaming request for every items returned by the server. OnSuccess will still be called with all the items.
            hub.Stream <int>("ObservableCounter", 10, 1000)
            .OnItem(result => uiText    += string.Format(" 'ObservableCounter(10, 1000)' OnItem: {0}\n", result.LastAdded))
            .OnSuccess(result => uiText += string.Format(" 'ObservableCounter(10, 1000)' OnSuccess. Final count: {0}\n", result.Items.Count))
            .OnError(error => uiText    += string.Format(" 'ObservableCounter(10, 1000)' error: {0}\n", error));

            // A stream request can be cancelled any time.
            var container = hub.Stream <int>("ChannelCounter", 10, 1000)
                            .OnItem(result => uiText    += string.Format(" 'ChannelCounter(10, 1000)' OnItem: {0}\n", result.LastAdded))
                            .OnSuccess(result => uiText += string.Format(" 'ChannelCounter(10, 1000)' OnSuccess. Final count: {0}\n", result.Items.Count))
                            .OnError(error => uiText    += string.Format(" 'ChannelCounter(10, 1000)' error: {0}\n", error)).value;

            // a stream can be cancelled by calling CancelStream
            hub.CancelStream(container);

            // This call will stream strongly typed objects
            hub.Stream <Person>("GetRandomPersons", 20, 2000)
            .OnItem(result => uiText    += string.Format(" 'GetRandomPersons(20, 1000)' OnItem: {0}\n", result.LastAdded))
            .OnSuccess(result => uiText += string.Format(" 'GetRandomPersons(20, 1000)' OnSuccess. Final count: {0}\n", result.Items.Count));
        }