// [Route("Hello")] public async Task HelloAsync(HelloAction action) { var connectionId = CurrentRequest.RequestContext.ConnectionId; LogInfo($"Hello: {connectionId}"); // retrieve websocket connection record var connection = await _dataTable.GetConnectionAsync(connectionId); if (connection == null) { LogInfo("Connection was removed"); return; } // subscribe websocket to SNS topic notifications connection.SubscriptionArn = (await _snsClient.SubscribeAsync(new SubscribeRequest { Protocol = "https", Endpoint = $"{_broadcastApiUrl}?ws={connectionId}&token={_httpApiToken}&rid={action.RequestId}", ReturnSubscriptionArn = true, TopicArn = _eventTopicArn })).SubscriptionArn; // update connection record await _dataTable.UpdateConnectionAsync(connection); }
// [Route("Hello")] public async Task HelloAsync(HelloAction action) { var connectionId = CurrentRequest.RequestContext.ConnectionId; LogInfo($"Hello: {connectionId}"); // retrieve connection record var connection = await _dataTable.GetConnectionRecordAsync(connectionId); if (connection == null) { LogInfo("Connection was removed"); throw Abort(action.AcknowledgeError("connection gone")); } if (connection.State == ConnectionState.Failed) { LogInfo("Connection is in failed state"); throw Abort(action.AcknowledgeError("connection reset required")); } if (connection.State != ConnectionState.New) { LogInfo("Client has already announced itself (state: {0})", connection.State); throw Abort(action.AcknowledgeError("client is already announced")); } if (!await _dataTable.UpdateConnectionRecordStateAsync(connection, ConnectionState.Pending)) { LogInfo($"Unable to update connection state from '{connection.State}' to '{ConnectionState.Pending}'"); throw Abort(action.AcknowledgeError("client is already being announced")); } // subscribe websocket to SNS topic notifications try { await _snsClient.SubscribeAsync(new SubscribeRequest { Protocol = "https", Endpoint = $"{_broadcastApiUrl}?ws={connectionId}&token={_httpApiToken}&rid={action.RequestId}", TopicArn = _eventTopicArn }); } catch (Exception e) { LogError(e, "failed to subscribe to SNS topic"); await _dataTable.SetConnectionRecordStateAsync(connection, ConnectionState.Failed); throw Abort(action.AcknowledgeError("internal error")); } // NOTE (2021-03-23, bjorg): the `AcknowledgeAction` response is sent by the `BroadcastFunction` when the subscription is enabled }