예제 #1
0
 public MessengerViewModel(int userId)
 {
     try
     {
         InstanceContext context = new InstanceContext(this);
         _serviceClient = new MessengerServiceClient(context);
         _userId        = userId;
         _serviceClient.ConnectAsync(_userId);
         Refresh();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
예제 #2
0
        public bool Enter(string nickname, out string message)
        {
            if (_client == null)
            {
                _client = new MessengerServiceClient(new InstanceContext(this));
            }

            EnterData enterData;

            try
            {
                enterData = _client.EnterService(nickname);
            }
            catch
            {
                HandleDisconnect();
                message = "Couldn't enter because service is unavailable.";

                return(false);
            }

            message = enterData.Message;

            if (!enterData.Status)
            {
                return(false);
            }

            _userId = enterData.Id;

            var userList = _client.GetUserList();

            UserController?.AddConnectedUsers(userList);

            try
            {
                _client.NotifyClientIsReady(_userId);
                IsConnected = true;
            }
            catch
            {
                _client.Abort();
                HandleDisconnect();
                message = "Couldn't notify server that client is ready.";

                return(false);
            }

            Task.Run(() =>
            {
                for (; ;)
                {
                    Task.Delay(PulseInterval).Wait();

                    try
                    {
                        if (_tokenSource.IsCancellationRequested)
                        {
                            break;
                        }

                        _client.Pulse(_userId);
                    }
                    catch
                    {
                        _client.Abort();
                        HandleDisconnect();
                        DisconnectController?.HandleDisconnect("Connection aborted.");
                        break;
                    }
                }
            });

            return(true);
        }
예제 #3
0
 private void HandleDisconnect()
 {
     IsConnected = false;
     _client     = null;
 }
예제 #4
0
 public ServiceConnector()
 {
     _client      = new MessengerServiceClient(new InstanceContext(this));
     _tokenSource = new CancellationTokenSource();
     IsConnected  = false;
 }
예제 #5
0
 public LoginViewModel()
 {
     _serviceClient = new MessengerServiceClient(new InstanceContext(this));
     //_user = new User();
 }