public TResponseType Send <TResponseType>(JanusBase request, bool releaseOnAck = false)
            where TResponseType : JanusBase
        {
            _pendingRequests.Add(request.Transaction, new JanusMessageEvent(releaseOnAck));
            var payload  = JsonConvert.SerializeObject(request, _snakeCase);
            var listener = new Task <string>(() => _pendingRequests[request.Transaction].WaitOne(_janusTimeout));

            listener.Start();
            Send(payload).Wait();
            var response = listener.Result;

            if (response == null)
            {
                throw new TimeoutException("Janus timeout");
            }
            if (JsonConvert.DeserializeObject <JanusBase>(response, _snakeCase) is JanusBase responseBase &&
                responseBase.Janus != "Error")
            {
                return(JsonConvert.DeserializeObject <TResponseType>(response, _snakeCase));
            }

            if (JsonConvert.DeserializeObject <JanusErrorResponse>(response, _snakeCase) is JanusErrorResponse janusError &&
                !string.IsNullOrEmpty(janusError.Error.Reason))
            {
                throw new ArgumentException(janusError.Error.Reason);
            }
            if (JsonConvert.DeserializeObject <PluginErrorResponse>(response, _snakeCase) is PluginErrorResponse pluginError &&
                !string.IsNullOrEmpty(pluginError.Plugindata.Data.Error))
            {
                throw new ArgumentException(pluginError.Plugindata.Data.Error);
            }
            throw new FormatException("Unknown janus response");
        }
        private async Task <TR> SendJanusRequest <TR>(JanusBase message, bool release = false, User one = null) where TR : JanusBase
        {
            var user = one ?? await MakeUser();

            message.SessionId   = user.SessionId;
            message.Transaction = RandomString();
            return(await Task.Run(() => Send <TR>(message, release)));
        }
        private async Task HandleMessage(JanusBase response, string clientMethod, params object[] parameters)
        {
            User user;

            using (var scope = _scopeFactory.CreateScope())
            {
                user = await scope.ServiceProvider.GetService <IUsersRepository>().GetBySessionId(response.SessionId);
            }
            _logger.Trace(
                $"Janus response {clientMethod}: {JsonConvert.SerializeObject(response, _snakeCase)}\r\nSent to {JsonConvert.SerializeObject(user)}");
            await _hub.Clients.Client(user.ConnectionId).SendAsync(clientMethod, parameters);
        }