Example #1
0
        static void Main(string[] args)
        {
            const string HOST = "localhost";
            const int    PORT = 8080;

            LoginServiceServer   loginService   = new LoginServiceServer();
            TCPServer            tCPServer      = new TCPServer(HOST, PORT, loginService);
            MessageServiceServer messageService = new MessageServiceServer();


            tCPServer.addHandler("CHAT", delegate(Message request)
            {
                Message message = new Message();
                foreach (Tuple <string, TcpClient> client in tCPServer.ActiveClients)
                {
                    request.WriteTo(client.Item2.GetStream());
                }

                message.Header = "OK2";
                message.Body   = request.Body;
                return(message);
            });

            tCPServer.addHandler("IS_AUTHENTICATED", delegate(Message response)
            {
                TcpClient client = tCPServer.GetTCPClient(response.Body);
                response.Header  = "OK";
                response.WriteTo(client.GetStream());

                return(response);
            });


            tCPServer.addHandler("AUTHENTICATION_FAILED", delegate(Message response)
            {
                TcpClient client = tCPServer.GetTCPClient(response.Body);
                response.WriteTo(client.GetStream());

                return(response);
            });

            tCPServer.Start();
        }
Example #2
0
        static void Main(string[] args)
        {
            const string HOST = "localhost";
            const int    PORT = 8080;

            LoginServiceServer loginService = new LoginServiceServer();
            TCPServer          tCPServer    = new TCPServer(HOST, PORT);


            tCPServer.addHandler("CHAT", delegate(Message request)
            {
                //save message into db

                return(null);
            });

            tCPServer.addHandler("CHECK_LOGIN", delegate(Message request)
            {
                string username = request.Body.Split('?')[0];
                Console.WriteLine("username : "******"password : "******"OK";
                    response.Body   = username;
                }
                else
                {
                    response.Header = "404";
                    response.Body   = "Username or password is invalid";
                }

                return(response);
            });

            tCPServer.Start();
        }
 public TCPServer(string hOST, int pORT, LoginServiceServer loginServiceServer)
 {
     this.hOST          = hOST;
     this.pORT          = pORT;
     LoginServiceServer = loginServiceServer;
 }