public virtual Task ProcessRequestAsync(HostContext context)
{
if (!_initialized)
{
throw new InvalidOperationException("Connection not initialized.");
}
if (IsNegotiationRequest(context.Request))
{
return ProcessNegotiationRequest(context);
}
_transport = GetTransport(context);
if (_transport == null)
{
throw new InvalidOperationException("Protocol error: Unknown transport.");
}
string connectionId = _transport.ConnectionId;
// If there's no connection id then this is a bad request
if (String.IsNullOrEmpty(connectionId))
{
throw new InvalidOperationException("Protocol error: Missing connection id.");
}
IEnumerable<string> signals = GetSignals(connectionId, context.Request);
IEnumerable<string> groups = OnRejoiningGroups(context.Request, _transport.Groups, connectionId);
Connection connection = CreateConnection(connectionId, signals, groups);
Connection = connection;
Groups = new GroupManager(connection, DefaultSignal);
_transport.TransportConnected = () =>
{
var command = new ServerCommand
{
Type = ServerCommandType.RemoveConnection,
Value = connectionId
};
return _serverMessageHandler.SendCommand(command);
};
_transport.Connected = () =>
{
return OnConnectedAsync(context.Request, connectionId).OrEmpty();
};
_transport.Reconnected = () =>
{
return OnReconnectedAsync(context.Request, groups, connectionId).OrEmpty();
};
_transport.Received = data =>
{
return OnReceivedAsync(context.Request, connectionId, data).OrEmpty();
};
_transport.Disconnected = () =>
{
return OnDisconnectAsync(connectionId).OrEmpty();
};
return _transport.ProcessRequest(connection).OrEmpty().Catch(_allErrorsTotalCounter, _allErrorsPerSecCounter);
}