예제 #1
0
파일: Wx.cs 프로젝트: hongweichang/x.rbt
        /// <summary>
        /// 获取用户登陆凭证
        /// </summary>
        void wxLogin()
        {
            var rsp = wc.GetStr(redirecturl + "&fun=new&version=v2");

            outLog("login->" + Serialize.ToJson(rsp));

            if (rsp.err)
            {
                if (rsp.err)
                {
                    throw new Exception("登陆失败->" + Serialize.ToJson(rsp));
                }
            }

            Regex reg = new Regex(@"<skey>(\S+?)</skey><wxsid>(\S+?)</wxsid><wxuin>(\d+)</wxuin><pass_ticket>(\S+?)</pass_ticket>");

            passticket  = String.Empty;
            baseRequest = new BaseRequest();

            var m = reg.Match(rsp.data + "");

            baseRequest.Skey = m.Groups[1].Value;
            baseRequest.Sid  = m.Groups[2].Value;
            passticket       = HttpUtility.UrlDecode(m.Groups[4].Value, Encoding.UTF8);

            lg.uin = baseRequest.Uin = Convert.ToInt64(m.Groups[3].Value);
            db.SubmitChanges();

            outLog("loged->" + lg.uin);

            Loged?.Invoke(this);
        }
예제 #2
0
        public bool Send(byte[] buffer)
        {
            if (buffer.Length == 0)
            {
                return(true);
            }

            var stream = _stream;

            if (stream.Any() == false)
            {
                return(false);
            }

            var message = new Message
            {
                Body = buffer,
            };

            try
            {
                stream
                .ForEach(o => o.WriteMessage(message));
            }
            catch (Exception e)
            {
                Loged?.Invoke(this, new LogEventArgs(e.Message));
                return(false);
            }


            return(true);
        }
예제 #3
0
        public bool StartClient(string hostName, int port)
        {
            Stop();

            _cd = new CompositeDisposable();

            TcpClient tcpClient;

            try
            {
                tcpClient = new TcpClient(hostName, port);
            }
            catch (SocketException e)
            {
                Loged?.Invoke(this, new LogEventArgs(e.Message));
                return(false);
            }

            ReceiveMessage(tcpClient)
            .Catch((IOException e) =>
            {
                Loged?.Invoke(this, new LogEventArgs(e.Message));
                return(Observable.Empty <Message>());
            })
            .Subscribe()
            .AddTo(_cd);

            return(true);
        }
예제 #4
0
 public static void Stop()
 {
     Loged?.Invoke("Stop....");
     if (_host != null)
     {
         _host.Stop();
     }
 }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        //public static Dictionary<string, Dictionary<string, MethodInfo>> ActionDict = new Dictionary<string, Dictionary<string, MethodInfo>>();


        public static void Start()
        {
            Loged?.Invoke("Start....");
            string hostUri = $"http://localhost:{ConfModel.Port}";

            _host = new NancyHost(new Uri(hostUri));
            _host.Start();
        }
예제 #6
0
 public static void ReStart()
 {
     Loged?.Invoke("ReStart....");
     try
     {
         Stop();
     }
     catch (Exception)
     {
         // ignored
     }
     System.Threading.Thread.Sleep(3000);
     Start();
 }
예제 #7
0
        /// <summary>
        /// 登陆后初始化
        /// </summary>
        void wxInit()
        {
            string url = String.Format("{0}/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket={1}&skey={2}&r={3}", gateway, passticket, baseRequest.Skey, getcurrentseconds());
            var    rsp = wc.PostStr(url, Serialize.ToJson(new { BaseRequest = baseRequest }));

            outLog("init->" + Serialize.ToJson(rsp));

            if (rsp.err)
            {
                throw new Exception("初始化失败->" + Serialize.ToJson(rsp));
            }

            user     = Serialize.FromJson <Contact>(rsp.data + "", "User");
            _syncKey = Serialize.FromJson <SyncKey>(rsp.data + "", "SyncKey");

            //lg.nickname = user.NickName;
            //db.SubmitChanges();

            Loged?.Invoke(user);
        }
예제 #8
0
        public bool StartListener(string ipAddress, int port)
        {
            Stop();

            try
            {
                _tcpListener = new TcpListener(IPAddress.Parse(ipAddress), port);
                _tcpListener.Start();
            }
            catch (SocketException e)
            {
                Loged?.Invoke(this, new LogEventArgs(e.Message));
                return(false);
            }

            _cd = new CompositeDisposable();

            Observable.Create <TcpClient>(async observer =>
            {
                var tcpClient = await _tcpListener.AcceptTcpClientAsync();
                observer.OnNext(tcpClient);
                observer.OnCompleted();

                return(Disposable.Empty);
            })
            .Repeat()
            .Select(o => ReceiveMessage(o).Catch((IOException e) =>
            {
                Loged?.Invoke(this, new LogEventArgs(e.Message));
                return(Observable.Empty <Message>());
            }))
            .Merge()
            .Subscribe()
            .AddTo(_cd);

            return(true);
        }
예제 #9
0
 public void OnLoged(string msg)
 {
     Loged?.Invoke($"[{ServiceName}]{msg}");
 }
예제 #10
0
 public static void OnLoged(string obj)
 {
     Loged?.Invoke(obj);
 }
예제 #11
0
 private static void OnLoged(string obj)
 {
     Loged?.Invoke(obj);
 }