コード例 #1
0
        private static async Task BaseTest(Func <SocketIoClient, CancellationToken, Task> func, params string[] additionalEvents)
        {
            using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            await using var client = new SocketIoClient();

            client.Connected              += (sender, args) => Console.WriteLine($"Connected: {args.Namespace}");
            client.Disconnected           += (sender, args) => Console.WriteLine($"Disconnected. Reason: {args.Reason}, Status: {args.Status:G}");
            client.EventReceived          += (sender, args) => Console.WriteLine($"EventReceived: Namespace: {args.Namespace}, Value: {args.Value}, IsHandled: {args.IsHandled}");
            client.HandledEventReceived   += (sender, args) => Console.WriteLine($"HandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");
            client.UnhandledEventReceived += (sender, args) => Console.WriteLine($"UnhandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");
            client.ErrorReceived          += (sender, args) => Console.WriteLine($"ErrorReceived: Namespace: {args.Namespace}, Value: {args.Value}");
            client.ExceptionOccurred      += (sender, args) => Console.WriteLine($"ExceptionOccurred: {args.Value}");

            var results = await client.WaitAllEventsAsync <EventArgs>(async cancellationToken =>
            {
                await func(client, cancellationToken);
            }, cancellationTokenSource.Token, new [] { nameof(client.Connected), nameof(client.Disconnected) }.Concat(additionalEvents).ToArray());

            Console.WriteLine();
            Console.WriteLine($"WebSocket State: {client.EngineIoClient.WebSocketClient.Socket.State}");
            Console.WriteLine($"WebSocket CloseStatus: {client.EngineIoClient.WebSocketClient.Socket.CloseStatus}");
            Console.WriteLine($"WebSocket CloseStatusDescription: {client.EngineIoClient.WebSocketClient.Socket.CloseStatusDescription}");

            foreach (var pair in results)
            {
                Assert.IsNotNull(pair.Value, $"Client event(\"{pair.Key}\") did not happen");
            }
        }
コード例 #2
0
ファイル: BaseTests.cs プロジェクト: HavenDV/H.Socket.IO
        public static async Task BaseTestAsync(
            Func <SocketIoClient, Task> func,
            CancellationToken cancellationToken = default,
            params string[] additionalEvents)
        {
#if NETCOREAPP3_1 || NETCOREAPP3_0 || NET5_0
            await using var client = new SocketIoClient();
#else
            using var client = new SocketIoClient();
#endif

            client.Connected              += (_, args) => Console.WriteLine($"Connected: {args.Namespace}");
            client.Disconnected           += (_, args) => Console.WriteLine($"Disconnected. Reason: {args.Reason}, Status: {args.Status:G}");
            client.EventReceived          += (_, args) => Console.WriteLine($"EventReceived: Namespace: {args.Namespace}, Value: {args.Value}, IsHandled: {args.IsHandled}");
            client.HandledEventReceived   += (_, args) => Console.WriteLine($"HandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");
            client.UnhandledEventReceived += (_, args) => Console.WriteLine($"UnhandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");
            client.ErrorReceived          += (_, args) => Console.WriteLine($"ErrorReceived: Namespace: {args.Namespace}, Value: {args.Value}");
            client.ExceptionOccurred      += (_, args) => Console.WriteLine($"ExceptionOccurred: {args.Value}");

            var results = await client.WaitAllEventsAsync <EventArgs>(
                async() => await func(client),
                cancellationToken,
                new [] { nameof(client.Connected), nameof(client.Disconnected) }
                .Concat(additionalEvents)
                .ToArray());

            Console.WriteLine();
            Console.WriteLine($"WebSocket State: {client.EngineIoClient.WebSocketClient.Socket.State}");
            Console.WriteLine($"WebSocket CloseStatus: {client.EngineIoClient.WebSocketClient.Socket.CloseStatus}");
            Console.WriteLine($"WebSocket CloseStatusDescription: {client.EngineIoClient.WebSocketClient.Socket.CloseStatusDescription}");

            foreach (var pair in results)
            {
                pair.Value.Should().NotBeNull(because: $"Client event(\"{pair.Key}\") did not happen");
            }
        }