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); } }
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); }
private void HandleDisconnect() { IsConnected = false; _client = null; }
public ServiceConnector() { _client = new MessengerServiceClient(new InstanceContext(this)); _tokenSource = new CancellationTokenSource(); IsConnected = false; }
public LoginViewModel() { _serviceClient = new MessengerServiceClient(new InstanceContext(this)); //_user = new User(); }