コード例 #1
0
        public static async Task <ClientWebSocket> ConnectToDevice(this ServiceClient serviceClient, string deviceId, CancellationToken ct, string streamName = "EdgeStream")
        {
            DeviceStreamRequest deviceStreamRequest = new DeviceStreamRequest(streamName);

            Console.WriteLine($"Connecting to {deviceId}");
            DeviceStreamResponse streamInfo = await serviceClient.CreateStreamAsync(deviceId, deviceStreamRequest, ct).ConfigureAwait(false);

            Console.WriteLine($"Stream response received: Name={deviceStreamRequest.StreamName} IsAccepted={streamInfo.IsAccepted}");

            return(await DeviceStreamWebsocket.MakeWebSocket(streamInfo.Url, streamInfo.AuthorizationToken, ct));
        }
コード例 #2
0
        public static async Task RegisterDeviceStreamCallback(this DeviceClient moduleClient, Func <ClientWebSocket, CancellationToken, Task> callback, CancellationToken ct)
        {
            int sleeptime = 1000;

            // This will run until the cancelation token is canceled
            while (!ct.IsCancellationRequested)
            {
                Console.WriteLine("Waiting for connection request");
                DeviceStreamRequest request;
                try
                {
                    request = await moduleClient.WaitForDeviceStreamRequestAsync(ct);

                    Console.WriteLine("Got connection request");
                    await moduleClient.AcceptDeviceStreamRequestAsync(request, ct).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Request got exeption:");
                    Console.WriteLine(ex);

                    Console.WriteLine($"Trying again in {sleeptime} seconds.");
                    await Task.Delay(sleeptime);

                    /* Max sleeptime is 10 minutes */
                    if (sleeptime < 1000 * 60 * 10)
                    {
                        sleeptime *= 2;
                    }
                    continue;
                }
                sleeptime = 1000;

                using (ClientWebSocket webSocket = await DeviceStreamWebsocket.MakeWebSocket(request.Url, request.AuthorizationToken, ct))
                {
                    try
                    {
                        await callback(webSocket, ct);
                    }
                    catch (WebSocketException ex)
                    {
                        Console.WriteLine("Websocket threw exception.");
                        Console.WriteLine(ex);
                    }

                    Console.WriteLine("Finished Connection Request");
                }

                Console.WriteLine("Closing websocket");
            }
        }