private async Task SendTo(Envelope envelope) { var result = await dynamo.GetItemAsync(SessionTable, ChatSession.ToKey(envelope.RoomId, envelope.ToId)); var session = ChatSession.FromAttributes(result.Item); if (session != null) { await SendJson(session, envelope); } }
private async Task Broadcast(Envelope envelope, string thisConnectionId) { var result = await dynamo.QueryAsync(new QueryRequest { TableName = SessionTable, KeyConditionExpression = "Room = :room", ExpressionAttributeValues = new Dictionary <string, AttributeValue> { { ":room", new AttributeValue(envelope.RoomId.ToString()) } } }); foreach (var attributes in result.Items) { var session = ChatSession.FromAttributes(attributes); if (session.ConnectionId != thisConnectionId) { await SendJson(session, envelope); } } }
private async Task AddConnection(Guid roomId, Guid clientId, string connectionId, Guid sessionId) { var result = await dynamo.GetItemAsync(SessionTable, ChatSession.ToKey(roomId, clientId)); var session = ChatSession.FromAttributes(result.Item); // Don't do anything if the connection doesn't have the right session ID if (session == null || session.SessionId == sessionId) { var newSession = new ChatSession { ClientId = clientId, ConnectionId = connectionId, SessionId = sessionId, RoomId = roomId, Expiry = DateTimeOffset.UtcNow.AddHours(2) // API Gateway WebSocket timeout }; // Upsert the connection ID for this client await dynamo.PutItemAsync(SessionTable, ChatSession.ToAttributes(newSession)); } }