Exemplo n.º 1
0
        /// <summary>
        /// 创建与 LeanMessage 云端的长连接
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task ConnectAsync(CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(clientId))
            {
                throw new Exception("当前 ClientId 为空,无法登录服务器。");
            }
            state = Status.Connecting;
            return(RouterController.GetAsync(cancellationToken).OnSuccess(_ =>
            {
                return OpenAsync(_.Result.server);
            }).Unwrap().OnSuccess(t =>
            {
                var cmd = new SessionCommand()
                          .UA(platformHooks.ua + Version)
                          .Option("open")
                          .AppId(AVClient.ApplicationId)
                          .PeerId(clientId);

                return AttachSignature(cmd, this.SignatureFactory.CreateConnectSignature(this.clientId)).OnSuccess(_ =>
                {
                    return AVIMClient.AVCommandRunner.RunCommandAsync(cmd);
                }).Unwrap();
            }).Unwrap().OnSuccess(s =>
            {
                state = Status.Online;
                var response = s.Result.Item2;
                websocketClient.OnMessage += WebsocketClient_OnMessage;
                websocketClient.OnClosed += WebsocketClient_OnClosed;
                websocketClient.OnError += WebsocketClient_OnError;
                RegisterNotices();
            }));
        }
Exemplo n.º 2
0
 private void WebsocketClient_OnClosed()
 {
     state = Status.Offline;
 }