コード例 #1
0
        /// <summary>
        /// Writes a server message.
        /// </summary>
        /// <param name="data"><see cref="ServerMessage"/> to process.</param>
        public void Write(ServerMessage data)
        {
            lock (this)
            {
                Console.CursorLeft = 0;

                if (data.IsAServerAnouncement)
                {
                    Logger.Warn(data.Message);
                }
                else
                {
                    UserData           user  = data.User;
                    ConsoleColorScheme color = user.Color;

                    Console.BackgroundColor = color.background;
                    Console.ForegroundColor = color.text;
                    Console.Write($" {data.TimeStamp.ToShortTimeString()} {user.Name} ");

                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.ForegroundColor = color.background;
                    Console.WriteLine($" {data.Message}");

                    Console.ResetColor();
                }
            }
        }
コード例 #2
0
 protected virtual void SwitchColorScheme(ConsoleColorScheme newColorScheme)
 {
     if (!Platform.IsWindows)
     {
         throw new PlatformNotSupportedException("it need WinAPI");
     }
     else if (newColorScheme == null)
     {
         throw new ArgumentNullException(nameof(newColorScheme));
     }
     else
     {
         //后面一片白眼睛看着好难受,可能有哪里写错了。
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Black, newColorScheme.Black.R, newColorScheme.Black.G, newColorScheme.Black.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Blue, newColorScheme.BrightBlue.R, newColorScheme.BrightBlue.G, newColorScheme.BrightBlue.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Cyan, newColorScheme.BrightCyan.R, newColorScheme.BrightCyan.G, newColorScheme.BrightCyan.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkBlue, newColorScheme.Blue.R, newColorScheme.Blue.G, newColorScheme.Blue.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkCyan, newColorScheme.Cyan.R, newColorScheme.Cyan.G, newColorScheme.Cyan.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkGray, newColorScheme.BrightBlack.R, newColorScheme.BrightBlack.G, newColorScheme.BrightBlack.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkGreen, newColorScheme.Green.R, newColorScheme.Green.G, newColorScheme.Green.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkMagenta, newColorScheme.Magenta.R, newColorScheme.Magenta.G, newColorScheme.Magenta.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkRed, newColorScheme.Red.R, newColorScheme.Red.G, newColorScheme.Red.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.DarkYellow, newColorScheme.Yellow.R, newColorScheme.Yellow.G, newColorScheme.Yellow.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Gray, newColorScheme.White.R, newColorScheme.White.G, newColorScheme.White.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Green, newColorScheme.BrightGreen.R, newColorScheme.BrightGreen.G, newColorScheme.BrightGreen.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Magenta, newColorScheme.BrightMagenta.R, newColorScheme.BrightMagenta.G, newColorScheme.BrightMagenta.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Red, newColorScheme.BrightRed.R, newColorScheme.BrightRed.G, newColorScheme.BrightRed.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.White, newColorScheme.BrightWhite.R, newColorScheme.BrightWhite.G, newColorScheme.BrightWhite.B);
         WinAPI.ReplaceConsoleColor(System.ConsoleColor.Yellow, newColorScheme.BrightYellow.R, newColorScheme.BrightYellow.G, newColorScheme.BrightYellow.B);
     }
 }
コード例 #3
0
 public void Log(LogLevel level, string message)
 {
     if (level <= this.level)
     {
         ConsoleColorScheme scheme = this.SelectColorScheme(level);
         this.FormatHeader(scheme);
         Console.Write(this.GetHeader(level));
         this.FormatBody(scheme);
         Console.WriteLine(message);
         this.ResetFormat();
     }
 }
コード例 #4
0
        private ConsoleColorScheme SelectColorScheme(LogLevel level)
        {
            ConsoleColorScheme reti = Format.BasicFormat;

            switch (level)
            {
            case LogLevel.TINY:

            case LogLevel.BASIC:
                reti = Format.BasicFormat;
                break;

            case LogLevel.INFO:
                reti = Format.InfoFormat;
                break;

            case LogLevel.SUCCESS:
                reti = Format.SuccessFormat;
                break;

            case LogLevel.WARNING:
                reti = Format.WarningFormat;
                break;

            case LogLevel.ERROR:
                reti = Format.ErrorFormat;
                break;

            case LogLevel.CRITICAL:
                reti = Format.CriticalFormat;
                break;

            default:
                reti = Format.BasicFormat;
                break;
            }
            return(reti);
        }
コード例 #5
0
        /// <summary>
        /// Create server instance.
        /// </summary>
        /// <param name="address">Address to create server.</param>
        public void Create(string address)
        {
            Logger.Info("Creating server...");

            this._server = new WebSocketServer(address);
            this._server.Start(socket =>
            {
                // Using OnOpen event to catch the new connections and sends the socket id to start login operations:
                socket.OnOpen = () =>
                {
                    Guid id = socket.ConnectionInfo.Id;
                    Logger.Info($"New conection from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort} with id {id}.");
                    socket.Send(id.ToByteArray());
                };

                // Using OnClose event to catch the connections was closed and to remove the user and socket from the server.
                socket.OnClose = () =>
                {
                    this.RemoveSession(socket.ConnectionInfo.Id);
                };

                // Using OnMessage event to catch any message received from any active connections, to process it, and send back to all connections:
                socket.OnMessage = message =>
                {
                    this.SendServerMessage(new ServerMessage(this.Users[socket.ConnectionInfo.Id], message, false));
                };

                // Using OnBinary to complete the login process started in OnOpen event:
                socket.OnBinary = data =>
                {
                    try
                    {
                        var logReq = new LoginRequest(data);

                        // Check for duplicate usernames.
                        foreach (var user in this.Users.Values)
                        {
                            if (user.Name.ToLower() == logReq.UserName.ToLower())
                            {
                                socket.Send(SharedConstants.SERVER_USERNAME_IN_USE.ToByteArray()); // Notify that the username is already registered.
                                return;
                            }
                        }

                        socket.Send(SharedConstants.SERVER_USERNAME_AVAILABLE.ToByteArray()); // Notify that the username is available.

                        var newUser = new UserData(logReq.UserName, ConsoleColorScheme.GetNextColorScheme());
                        this.RegisterSession(logReq.Id, newUser, socket);
                        socket.Send(newUser.GetSerializedData().ToArray());

                        this.SendAnouncement($"{logReq.UserName} has entered in conversation.");
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"OnBinary(): {ex.Message}", ex);
                    }
                };

                // Using OnError to catch forced disconnections from clients and removing their sessions:
                socket.OnError = ex =>
                {
                    if (ex is IOException &&
                        ex.InnerException is SocketException &&
                        (ex.InnerException as SocketException).SocketErrorCode == SocketError.ConnectionReset)
                    {
                        this.RemoveSession(socket.ConnectionInfo.Id);
                    }
                };
            });

            Logger.Warn("Press any key to stop server...");
            Console.ReadKey();
        }
コード例 #6
0
 private void FormatHeader(ConsoleColorScheme colors)
 {
     Console.ForegroundColor = colors.header_foreground;
     Console.BackgroundColor = colors.header_background;
 }
コード例 #7
0
 private void FormatBody(ConsoleColorScheme colors)
 {
     Console.ForegroundColor = colors.body_foreground;
     Console.BackgroundColor = colors.body_background;
 }
コード例 #8
0
 public void Setup()
 {
     this._id          = Guid.NewGuid();
     this._colorScheme = new ConsoleColorScheme(ConsoleColor.Black, ConsoleColor.White);
     this._userData    = new UserData(SerializedObjectTest.USERNAME, this._colorScheme);
 }