Пример #1
0
 public static SocketInstance GetInstance()
 {
     if (instance == null)
     {
         instance = new SocketInstance();
     }
     return(instance);
 }
Пример #2
0
    public void CreateConnection(string ip, int port)
    {
        IPEndPoint end_point = new IPEndPoint(IPAddress.Parse(ip), port);

        if (!socketDic.ContainsKey(end_point))
        {
            SocketInstance socketInstance = new SocketInstance();

            socketInstance.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socketInstance.socket.BeginConnect(end_point, OnConnectCallback, socketInstance);

            socketDic [end_point] = socketInstance;
        }
    }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// This method calls <see cref="LoadState"/>, where all page specific
        /// navigation and process lifetime management logic should be placed.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property provides the group to be displayed.</param>
        public async void OnNavigatedTo(NavigationEventArgs e)
        {
            var frameState = SuspensionManager.SessionStateForFrame(this.Frame);

            this._pageKey = "Page-" + this.Frame.BackStackDepth;
            var config   = DBProvider.DBConnection.Table <Config>();
            var username = config.Where(x => x.key == ConfigKey.Username).FirstOrDefault();
            var password = config.Where(x => x.key == ConfigKey.Password).FirstOrDefault();
            var phone    = config.Where(x => x.key == ConfigKey.PhoneNumber).FirstOrDefault();



            if (e.NavigationMode == NavigationMode.New)
            {
                if (String.IsNullOrEmpty(e.Parameter as String))
                {
                    if (SocketInstance.Instance == null)
                    {
                        SocketInstance.Create(phone.value, password.value, username.value);
                        App.UserName    = username.value;
                        App.PhoneNumber = phone.value;
                        App.Password    = password.value;
                    }

                    if (SocketInstance.Instance != null && SocketInstance.Instance.ConnectionStatus != WhatsAPI.UniversalApps.Libs.Constants.Enums.CONNECTION_STATUS.CONNECTED || SocketInstance.Instance.ConnectionStatus != WhatsAPI.UniversalApps.Libs.Constants.Enums.CONNECTION_STATUS.LOGGEDIN)
                    {
                        try
                        {
                            await SocketInstance.Instance.Connect();

                            SocketInstance.Instance.OnLoginSuccess += Instance_OnLoginSuccess;
                            var challengeData = await FileHelper.ReadFile(ConfigKey.WhatsAppNextChallengeFile);

                            if (challengeData != null)
                            {
                                await SocketInstance.Instance.Login(challengeData);
                            }
                            else
                            {
                                await SocketInstance.Instance.Login();
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                // Clear existing state for forward navigation when adding a new page to the
                // navigation stack
                var nextPageKey   = this._pageKey;
                int nextPageIndex = this.Frame.BackStackDepth;
                while (frameState.Remove(nextPageKey))
                {
                    nextPageIndex++;
                    nextPageKey = "Page-" + nextPageIndex;
                }

                // Pass the navigation parameter to the new page
                if (this.LoadState != null)
                {
                    this.LoadState(this, new LoadStateEventArgs(e.Parameter, null));
                }
            }
            else
            {
                // Pass the navigation parameter and preserved page state to the page, using
                // the same strategy for loading suspended state and recreating pages discarded
                // from cache
                if (this.LoadState != null)
                {
                    this.LoadState(this, new LoadStateEventArgs(e.Parameter, (Dictionary <String, Object>)frameState[this._pageKey]));
                }
            }
        }