Пример #1
0
        private void AddUser(string ipPort, JoinPayload payload)
        {
            Logger.Info($"Client from '{ipPort}' wants to join chat server.");
            if (string.IsNullOrWhiteSpace(payload.Name) || string.IsNullOrWhiteSpace(payload.Color))
            {
                Logger.Debug($"Client '{ipPort}' has no name or no color.");
                _server.DisconnectClient(ipPort, MessageStatus.Failure);
                return;
            }

            var user = new User
            {
                Id     = Guid.NewGuid(),
                Name   = payload.Name,
                Color  = payload.Color,
                IpPort = ipPort
            };
            var clientPayload = new JoinedPayload(user.Id, user.Name, user.Color);
            var clientPacket  = new ClientPacket(ClientAction.Joined, clientPayload);

            SendToAllUsers(clientPacket);

            Users.Add(user);
            InvokeUserJoinEvent(user);
        }
Пример #2
0
 public void setJoinPayload(JoinPayload payload)
 {
     if (!users.ContainsKey(payload.ownerID))
     {
         users.Add(payload.ownerID, payload);
     }
 }
Пример #3
0
        public void JoinServer(string name, string color)
        {
            var checkUser = Users.Find(existingUser => existingUser.Name.Equals(name));

            if (checkUser != null)
            {
                throw new UserAlreadyExistsException(name);
            }

            var user = new User
            {
                Name  = name,
                Color = color
            };

            var payload      = new JoinPayload(user.Name, user.Color);
            var serverPacket = new ServerPacket(ServerAction.Join, payload);

            _client.Connect();
            _client.SendAsync(serverPacket.ToJson());
            InvokeServerJoinEvent();
        }
Пример #4
0
        private void Worker_packetCallback(string sessionId, string data)
        {
            try
            {
                SocketRequest  socketRequest = JsonConvert.DeserializeObject <SocketRequest>(data);
                SocketResponse response      = new SocketResponse();

                SessionWorker worker = workers[sessionId];
                response.method = socketRequest.method;

                if (socketRequest.method == SocketPacket.SOCKET_METHOD_CREATE)
                {
                    string name = socketRequest.payload.ToString();

                    worker.setName(name);
                    Console.WriteLine("Session:" + sessionId + " Created By:" + name);
                    response.payload = new JoinPayload()
                    {
                        sessionID = sessionId, name = worker.name, ownerID = worker.ownerID
                    };
                    writeWorker(response, worker);
                }
                else if (socketRequest.method == SocketPacket.SOCKET_METHOD_JOIN)
                {
                    JoinPayload joinPayload = JsonConvert.DeserializeObject <JoinPayload>(socketRequest.payload.ToString());
                    if (workers.ContainsKey(joinPayload.sessionID))
                    {
                        worker.setName(joinPayload.name);

                        sendBroadCast(workers[joinPayload.sessionID], new SocketResponse()
                        {
                            isValid = true,
                            payload = new chatPayload()
                            {
                                message   = joinPayload.name + " Join Session",
                                ownerID   = workers[joinPayload.sessionID].ownerID,
                                sessionID = workers[joinPayload.sessionID].sessionID
                            },
                            method = SocketPacket.SOCKET_METHOD_MESSAGE
                        });

                        workers[joinPayload.sessionID].setJoinPayload(new JoinPayload()
                        {
                            name      = joinPayload.name,
                            ownerID   = worker.ownerID,
                            sessionID = worker.sessionID
                        });

                        Console.WriteLine("Session:" + joinPayload.sessionID + " Join:" + joinPayload.name);
                        response.payload = new JoinPayload()
                        {
                            sessionID = joinPayload.sessionID,
                            name      = worker.name,
                            ownerID   = worker.ownerID
                        };
                    }
                    else
                    {
                        response.isValid = false;
                        response.payload = "Session not exists";
                    }

                    writeWorker(response, worker);
                }
                else if (socketRequest.method == SocketPacket.SOCKET_METHOD_CHAT)
                {
                    chatPayload chatPayload = JsonConvert.DeserializeObject <chatPayload>(socketRequest.payload.ToString());

                    if (workers.ContainsKey(chatPayload.sessionID))
                    {
                        response.method = SocketPacket.SOCKET_METHOD_MESSAGE;
                        chatPayload chat = new chatPayload()
                        {
                            sessionID = chatPayload.sessionID,
                            name      = chatPayload.name,
                            ownerID   = chatPayload.ownerID,
                            message   = chatPayload.message
                        };

                        sendBroadCast(workers[chatPayload.sessionID], new SocketResponse()
                        {
                            isValid = true,
                            payload = chat,
                            method  = SocketPacket.SOCKET_METHOD_MESSAGE
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }