示例#1
0
        internal static Task SendMessage(string apiUrl, string connectionId, string room, JObject message)
        {
            Task result = Task.Run(() => {
                MemoryStream stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(message.ToString(Newtonsoft.Json.Formatting.None)));
                AmazonApiGatewayManagementApiConfig config = new AmazonApiGatewayManagementApiConfig()
                {
                    ServiceURL = apiUrl
                };
                AmazonApiGatewayManagementApiClient client = new AmazonApiGatewayManagementApiClient(config);
                PostToConnectionRequest postReq            = new PostToConnectionRequest()
                {
                    ConnectionId = connectionId, Data = stream
                };
                try
                {
                    Logging.LogDebug("Sending to: " + connectionId);
                    Task <PostToConnectionResponse> task = client.PostToConnectionAsync(postReq);
                    task.Wait();
                    Logging.LogDebug("Sent to: " + connectionId);
                }
                catch (Exception ex)
                {
                    Logging.LogDebug("Deleteing conneciton " + connectionId);
                    error = apiUrl + " - " + connectionId + " - " + ex.ToString();
                    Connection.Delete(apiUrl, room, connectionId);
                    Logging.LogDebug("Deleted conneciton " + connectionId);
                }
            });

            return(result);
        }
示例#2
0
        protected IAmazonApiGatewayManagementApi CreateClient(AWSCredentials credentials, RegionEndpoint region)
        {
            var config = new AmazonApiGatewayManagementApiConfig {
                RegionEndpoint = region
            };

            Amazon.PowerShell.Utils.Common.PopulateConfig(this, config);
            this.CustomizeClientConfig(config);
            var client = new AmazonApiGatewayManagementApiClient(credentials, config);

            client.BeforeRequestEvent += RequestEventHandler;
            client.AfterResponseEvent += ResponseEventHandler;
            return(client);
        }
        public static AmazonApiGatewayManagementApiClient CreateConfiguredApiGatewayManagementApiClient()
        {
            var serviceUrl = Environment.GetEnvironmentVariable("ServiceURL");

            if (string.IsNullOrEmpty(serviceUrl))
            {
                return(null);
            }
            var config = new AmazonApiGatewayManagementApiConfig {
                ServiceURL = serviceUrl
            };

            return(new AmazonApiGatewayManagementApiClient(config));
        }
示例#4
0
        internal static void SendCatchup(string serviceUrl, string connectionId, string room, JArray messages)
        {
            JObject message = new JObject()
            {
                { "action", "catchup" }, { "room", room }, { "messages", messages }
            };
            MemoryStream stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(message.ToString(Newtonsoft.Json.Formatting.None)));
            AmazonApiGatewayManagementApiConfig config = new AmazonApiGatewayManagementApiConfig()
            {
                ServiceURL = serviceUrl
            };
            AmazonApiGatewayManagementApiClient client  = new AmazonApiGatewayManagementApiClient(config);
            PostToConnectionRequest             postReq = new PostToConnectionRequest()
            {
                ConnectionId = connectionId, Data = stream
            };

            client.PostToConnectionAsync(postReq);
        }
示例#5
0
        public async Task <APIGatewayProxyResponse> Handler(APIGatewayProxyRequest input, ILambdaContext context)
        {
            var client = new AmazonDynamoDBClient();

            var scanRequest = new ScanRequest
            {
                TableName            = Environment.ExpandEnvironmentVariables("%TABLE_NAME%"),
                ProjectionExpression = "connectionId"
            };

            ScanResponse connections = null;

            try
            {
                connections = await client.ScanAsync(scanRequest);
            }
            catch (Exception e)
            {
                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = e.Message,
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "text/plain" }
                    },
                });
            }

            var data      = JObject.Parse(input.Body)["data"].ToString();
            var byteArray = Encoding.UTF8.GetBytes(data);

            var config = new AmazonApiGatewayManagementApiConfig
            {
                ServiceURL = $"https://{input.RequestContext.DomainName}/{input.RequestContext.Stage}"
            };

            var apiClient = new AmazonApiGatewayManagementApiClient(config);

            var connectionIds = connections.Items.Select(item => item["connectionId"].S).ToList();

            foreach (var connectionId in connectionIds)
            {
                var postData = new MemoryStream(byteArray);

                try
                {
                    var postToRequest = new PostToConnectionRequest
                    {
                        ConnectionId = connectionId,
                        Data         = postData
                    };

                    await apiClient.PostToConnectionAsync(postToRequest);
                }
                catch (GoneException)
                {
                    Console.WriteLine($"Found dead connection, deleting {connectionId}");

                    var attributes = new Dictionary <string, AttributeValue>();

                    attributes["connectionId"] = new AttributeValue {
                        S = connectionId
                    };

                    var deleteRequest = new DeleteItemRequest
                    {
                        TableName = Environment.ExpandEnvironmentVariables("%TABLE_NAME%"),
                        Key       = attributes
                    };

                    try
                    {
                        await client.DeleteItemAsync(deleteRequest);
                    }
                    catch (Exception e)
                    {
                        return(new APIGatewayProxyResponse
                        {
                            StatusCode = (int)HttpStatusCode.InternalServerError,
                            Body = e.Message,
                            Headers = new Dictionary <string, string> {
                                { "Content-Type", "text/plain" }
                            },
                        });
                    }
                }
                catch (Exception e)
                {
                    return(new APIGatewayProxyResponse
                    {
                        StatusCode = (int)HttpStatusCode.InternalServerError,
                        Body = e.Message,
                        Headers = new Dictionary <string, string> {
                            { "Content-Type", "text/plain" }
                        },
                    });
                }
            }

            return(new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body = "data sent",
                Headers = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                },
            });
        }