Nancy.LongPoll.PollService.Register C# (CSharp) Method

Register() private method

private Register ( string clientHostAddess, string sessionId ) : Message
clientHostAddess string
sessionId string
return Message
    internal Message Register(string clientHostAddess, string sessionId)
    {
      try
      {
        lock (_Clients)
        {
          #region Отключение просроченных клиентов
          try
          {
            List<Client> clients = new List<Client>(_Clients.Values);
            foreach (var client in clients)
            {
              if ((DateTime.UtcNow - client.LastSeen).TotalSeconds > CLIENT_TIMEOUT)
              {
                RemoveClientId(client.Id);
                _Clients.Remove(client.Id);
              }
            }
          }
          catch (Exception ex)
          {
            _Logger.LogException(ex, "Exception on timeouted clients disconnection");
            return new Message()
            {
              success = false,
              retcode = Message.RetCodes.ErrorOnServer
            };
          }
          #endregion

          #region Проверка числа подключенных клиентов
          if (_Clients.Count > MAX_CLIENTS)
          {
            _Logger.LogWarn("Polling clients maximum exceeded");
            return new Message()
            {
              retcode = Message.RetCodes.ClientsMaximumExceeded,
              success = false
            };
          }
          #endregion

          string clientId = Guid.NewGuid().ToString("N");
          StopClient(clientId);

          _Clients.Add(clientId, new Client(_Logger)
          {
            Id = clientId,
            Ip = clientHostAddess,
            LastSeen = DateTime.UtcNow
          });
          if (!string.IsNullOrWhiteSpace(sessionId))
          {
            if (!_ClientIdToSessId.ContainsKey(clientId)) _ClientIdToSessId.Add(clientId, new List<string>());
            _ClientIdToSessId[clientId].Add(sessionId);
            if (!_SessIdClientId.ContainsKey(sessionId)) _SessIdClientId.Add(sessionId, new List<string>());
            _SessIdClientId[sessionId].Add(clientId);
          }

          return new Message()
          {
            success = true,
            retcode = Message.RetCodes.Ok,
            data = clientId
          };
        }
      }
      catch (Exception ex)
      {
        _Logger.LogException(ex, "Exception on Register");
        return new Message()
        {
          success = false,
          retcode = "Server internal error on register client"
        };
      }
    }

Usage Example

Beispiel #1
0
    public PollModule(TinyIoc.TinyIoCContainer container, IPollService pollService = null, ISessionProvider sessionProvider = null)
    {
      if (container == null) throw new ArgumentNullException("container");
      if (!(sessionProvider is DefaultSessionProvider)) _SessionProvider = sessionProvider;

      if (pollService == null)
      {
        container.Register<IPollService, PollService>().AsSingleton();
        pollService = container.Resolve<IPollService>();
      }
      _PollService = pollService as PollService;
      if (pollService == null) throw new ApplicationException("Support Nany.LongPoll.PollService implementation only");

      Get["/Poll/Register"] = x =>
      {
        var sp = _SessionProvider;
        if (sp == null) sp = new DefaultSessionProvider(Request);

        var response = Response.AsJson(_PollService.Register(Request.UserHostAddress, sp.SessionId));
        if (sp is DefaultSessionProvider)
        {
          response = response.WithCookie(DefaultSessionProvider.SessionIdCookieName, sp.SessionId);
        }

        return response;
      };
      Get["/Poll/Wait"] = x =>
      {
        string clientId = Request.Query.clientId;
        ulong seqCode = Request.Query.seqCode;

        return Response.AsText(JsonConvert.SerializeObject(_PollService.Wait(clientId, seqCode)), "application/json");
      };
    }
All Usage Examples Of Nancy.LongPoll.PollService::Register