示例#1
0
        private static void InitializeTcpServer(int portNumber)
        {
            var callback = new ChatCallback
            {
                OnClientConnected = endPoint =>
                {
                    Console.WriteLine("Nowy klient połączony: {0}:{1}", endPoint.Address, endPoint.Port);
                },
                OnMessageReceived = (tcpUser, message) =>
                {
                    var endPoint         = (IPEndPoint)tcpUser.Socket.RemoteEndPoint;
                    var formattedMessage = MessageExtractor.Extract(message);
                    var information      = InformationConverter.Deserialize(formattedMessage);
                    if (information?.Type == Message)
                    {
                        Console.WriteLine(
                            $"{information.Nick} ({endPoint.Address}:{endPoint.Port}) : {information.Message}");
                    }
                },
                OnClientDisconnected = endPoint =>
                {
                    Console.WriteLine($"({endPoint.Address}:{endPoint.Port}) has disconnected!");
                }
            };
            var server = new TCPServer(portNumber, callback);

            server.Initialize();
        }