コード例 #1
0
ファイル: TCPChat.cs プロジェクト: GrigoryanArtem/Sockets
        public void SendMessage(string message)
        {
            ChatMessageText messageText = ChatMessageText.Create(message);

            mChatCore.SendMessage(ChatMessage.Create(GetMessageCodeByRecipient(RoomsManager.CurrentRoom.Recipient),
                                                     mUser, RoomsManager.CurrentRoom.Recipient, DateTime.Now, messageText));
        }
コード例 #2
0
        private void OnRegistration(ChatMessage message)
        {
            Logger.Debug($"Registration new user: {message.Sender.Name}.");

            Clients[message.Sender.Id].Name = message.Sender.Name;


            ChatMail.SendMessage(ChatMessage.Create(MessageCode.NewUser, ServerUser, null, DateTime.Now, ChatMessageText.Create(message.Sender.ToString())));
            ChatMail.SendMessage(ChatMessage.Create(MessageCode.ServerUsers, ServerUser, message.Sender,
                                                    DateTime.Now, ChatMessageText.Create(String.Join(" ", Clients.RegestredUsers.Where(user => user.Id != message.Sender.Id).Select(user => user.User)))), message.Sender.Id);
        }
コード例 #3
0
        public void AddChatMessage(Guid chatRoomId, ChatMessageText chatMessageText)
        {
            ChatRoom chatRoom = _chatRoomRepository
                                .Get(new ChatRoomSpecification(chatRoomId))
                                .FirstOrDefault();

            if (chatRoom != default(ChatRoom))
            {
                chatRoom.AddMessage(chatMessageText);
                _chatRoomRepository.Update(chatRoom);
            }
            _logger.LogInformation($"Chat room with id {chatRoomId.ToString()} not found.", null);
        }
コード例 #4
0
        public void TestSendPaymentChat()
        {
            var paymentChatMap = new Dictionary <string, object>();
            var chatId         = PaymentChat.Create(API_CONTEXT, paymentChatMap, USER_ID, MONETARTY_ACCOUNT_ID,
                                                    CreatePaymentAndGetId()).Value;

            var chatMessageMap = new Dictionary <string, object>
            {
                { ChatMessageText.FIELD_TEXT, MESSAGE_TEXT }
            };

            ChatMessageText.Create(API_CONTEXT, chatMessageMap, USER_ID, chatId);
        }
コード例 #5
0
        void ReleaseDesignerOutlets()
        {
            if (ChatMessageAuthor != null)
            {
                ChatMessageAuthor.Dispose();
                ChatMessageAuthor = null;
            }

            if (ChatMessageText != null)
            {
                ChatMessageText.Dispose();
                ChatMessageText = null;
            }

            if (ChatTable != null)
            {
                ChatTable.Dispose();
                ChatTable = null;
            }

            if (MessageTextField != null)
            {
                MessageTextField.Dispose();
                MessageTextField = null;
            }

            if (PeerId != null)
            {
                PeerId.Dispose();
                PeerId = null;
            }

            if (PeerName != null)
            {
                PeerName.Dispose();
                PeerName = null;
            }

            if (PeersTable != null)
            {
                PeersTable.Dispose();
                PeersTable = null;
            }
        }
コード例 #6
0
        private void HandleClients(object clientId)
        {
            int       id = (int)clientId;
            TcpClient client;

            lock (Locker)
                client = (Clients[id] as ChatClient)?.Client;

            try
            {
                while (true)
                {
                    NetworkStream stream     = client.GetStream();
                    byte[]        buffer     = new byte[Constants.MessageSize];
                    int           byte_count = stream.Read(buffer, 0, buffer.Length);

                    if (byte_count == 0)
                    {
                        break;
                    }

                    string data = Encoding.UTF8.GetString(buffer, 0, byte_count);
                    HandleMessage(ChatMessage.Parse(data));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                Logger.Error($"Error: #{id} lost connection.");
            }
            finally
            {
                lock (Locker)
                    Clients.Remove(id);

                ChatMail.SendMessage(ChatMessage.Create(MessageCode.UserLeave, ServerUser,
                                                        null, DateTime.Now, ChatMessageText.Create(id.ToString())));

                Logger.Info($"#{id} has left.");

                client.Client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
        }
コード例 #7
0
 public static string IsValid(ChatMessageText chatMessageText)
 {
     return(string.IsNullOrEmpty(chatMessageText.Text) ? "Message cannot be empty!" : string.Empty);
 }
コード例 #8
0
ファイル: TCPChat.cs プロジェクト: GrigoryanArtem/Sockets
        private void OnRegistration(ChatMessage message)
        {
            ServerName = message.Sender.Name;

            mUser = new ChatUser(message.Recipient.Id, mUser.Name);
            SendMessage(ChatMessage.Create(MessageCode.Registration, mUser, new ChatUser(0, mServerName), DateTime.Now, ChatMessageText.CreateEmpty()));
        }
コード例 #9
0
 public void RemoveChatMessage(ChatMessageText removing)
 {
     this.Messages.Remove(removing);
     Destroy(removing.gameObject);
 }
コード例 #10
0
        public void Start()
        {
            int currentId = Constants.FirstUserId;

            TcpListener ServerSocket = new TcpListener(IPAddress.Any, Port);

            ServerSocket.Start();

            Logger.Info($"Server \"{ServerUser.Name}\" started at {GlobalIPAddress.GetGlobalIPAddress()}:{Port}");

            while (true)
            {
                TcpClient client = ServerSocket.AcceptTcpClient();

                var chatClient = ChatClient.Create(currentId, client);
                chatClient.Locker = Locker;

                lock (Locker)
                    Clients.Add(chatClient);

                ChatMail.SendMessage(ChatMessage.Create(MessageCode.ServerName, ServerUser,
                                                        new ChatUser(currentId), DateTime.Now, ChatMessageText.CreateEmpty()), currentId);

                Logger.Info($"#{currentId} connected!");

                Thread t = new Thread(HandleClients);
                t.Start(currentId);

                currentId += Constants.UserIdIncrementValue;
            }
        }
コード例 #11
0
        public void TestSendPaymentChat()
        {
            var chatId = PaymentChat.Create(CreatePaymentAndGetId()).Value;

            ChatMessageText.Create(chatId, MESSAGE_TEXT);
        }