示例#1
0
        public async Task <APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
        {
            try
            {
                //ConnectionSocketInputModel connectionInput = JsonConvert.DeserializeObject<ConnectionSocketInputModel>(request.Body);

                var connectionId = request.RequestContext.ConnectionId;
                context.Logger.LogLine($"ConnectionId: {connectionId}");

                ConnectionSocketModel connection = new ConnectionSocketModel {
                    connection_id = connectionId,
                    // channel = connectionInput.channel,
                    // user_id = connectionInput.user_id
                };

                var connectionService = _service.GetService <ConnectionSocketService>();
                var response          = await connectionService.AddConnection(connection);

                return(response);
            }
            catch (Exception e)
            {
                context.Logger.LogLine("Error connecting: " + e.Message);
                context.Logger.LogLine(e.StackTrace);
                return(new APIGatewayProxyResponse
                {
                    StatusCode = 500,
                    Body = $"Failed to connect: {e.Message}"
                });
            }
        }
        public async Task <APIGatewayProxyResponse> AddConnection(ConnectionSocketModel data)
        {
            try {
                data.add_datetime = DateTime.Now;

                _context_db.Connections.Add(data);
                _context_db.SaveChanges();

                APIGatewayProxyResponse respond = new APIGatewayProxyResponse {
                    StatusCode = (int)HttpStatusCode.OK,
                    Headers    = new Dictionary <string, string>
                    {
                        { "Content-Type", "application/json" },
                        { "Access-Control-Allow-Origin", "*" }
                    },
                    Body = "Connected"
                };

                return(await Task.FromResult(respond));
            }
            catch (Exception e) {
                return(new APIGatewayProxyResponse {
                    StatusCode = 500,
                    Body = $"Service Fail: {e.Message}"
                });
            }
        }