Пример #1
0
        public Person[] Join(Person person, string serverIp, string port)
        {
            var chatCallback = new ChatServiceCallback();

            chatCallback.ChatCallbackEvent += ChatCallbackEvent;
            var uri = new Uri($"net.tcp://{serverIp}:{port}");

            _serviceProxy = new ChatServiceProxy(chatCallback, uri);
            var result = _serviceProxy.Join(person);

            return(result.Result);
        }
Пример #2
0
        private void CloseClient()
        {
            try
            {
                if (_callback != null)
                {
                    _callback.MessagePost     -= callback_MessagePost;
                    _callback.UsersListChange -= callback_UsersListChange;

                    _callback = null;
                }

                if (_client != null)
                {
                    _client.InnerDuplexChannel.Closing -= InnerChannel_Closed;
                    _client.InnerDuplexChannel.Closed  -= InnerChannel_Closed;
                    _client.InnerDuplexChannel.Faulted -= InnerChannel_Closed;

                    switch (_client.State)
                    {
                    case CommunicationState.Opened:
                    case CommunicationState.Opening:
                    case CommunicationState.Created:
                        _client.Abort();
                        break;
                    }

                    _client = null;
                }
            }
            catch
            {
                _callback = null;
                _client   = null;
            }
            finally
            {
                _username   = null;
                IsAdmin     = false;
                IsConnected = false;
            }
        }
Пример #3
0
        public async Task <ConnectionStatus> Connect(string serverIp, string port, Client client)
        {
            if (IsConnected)
            {
                return(ConnectionStatus.ConnectionSuccessful);
            }
            try
            {
                var callback = new ChatServiceCallback();
                callback.MessageReceived += (s, e) =>
                {
                    MessageReceived(this, e);
                };
                var instanceContext = new InstanceContext(callback);
                var binding         = new NetTcpBinding(SecurityMode.None, true)
                {
                    CloseTimeout = new TimeSpan(0, 0, 5),
                    OpenTimeout  = new TimeSpan(0, 0, 10),
                    SendTimeout  = new TimeSpan(0, 0, 10)
                };
                binding.ReliableSession.InactivityTimeout = new TimeSpan(0, 0, 5);
                var address  = $"net.tcp://{serverIp}:{port}/chat";
                var endpoint = new EndpointAddress(address);
                proxy = DuplexChannelFactory <IChatService> .CreateChannel(instanceContext, binding, endpoint);

                ((IClientChannel)proxy).Faulted += (s, e) => { Disconnect(); Disconnected(this, EventArgs.Empty); };
                ((IClientChannel)proxy).Closed  += (s, e) => { Disconnect(); Disconnected(this, EventArgs.Empty); };
                var connected = await proxy.Connect(client);

                if (connected)
                {
                    Client = client;
                    return(ConnectionStatus.ConnectionSuccessful);
                }
                return(ConnectionStatus.ClientAlreadyConnected);
            }
            catch
            {
                return(ConnectionStatus.ConnectionFailed);
            }
        }
Пример #4
0
        private void InitClient()
        {
            try
            {
                _callback                  = new ChatServiceCallback();
                _callback.MessagePost     += callback_MessagePost;
                _callback.UsersListChange += callback_UsersListChange;

                _client = new ChatServiceClient(new InstanceContext(_callback));

                _client.InnerDuplexChannel.Closing += InnerChannel_Closed;
                _client.InnerDuplexChannel.Closed  += InnerChannel_Closed;
                _client.InnerDuplexChannel.Faulted += InnerChannel_Closed;

                IsConnected = true;
            }
            catch
            {
                CloseClient();
                throw;
            }
        }
Пример #5
0
        /// <summary>
        /// Подключение к серверу
        /// </summary>
        private void Connect()
        {
            Person person = new Person {
                Name = UserName
            };

            try
            {
                var callback = new ChatServiceCallback();
                callback.ChatCallbackEvent += Callback_ChatCallbackEvent;

                var joinResult = _communicationManager.Connect(serverIP: ServerIP, serverPort: ServerPort, person: person, callback: callback);

                if (!joinResult.Success)
                {
                    Disconnect();
                    LogError(joinResult);
                    return;
                }

                //Задается основной пользователь клиента
                CurrentPerson = person;

                AllPersons.Clear();
                foreach (var per in joinResult.Result.OrderBy(x => x.Name))
                {
                    AllPersons.Add(per);
                }

                IsConnected = true;
            }
            catch (Exception ex)
            {
                LogError("Не получилось подключиться к серверу: " + ex.Message);
                IsConnected = false;
            }
        }