ChatSystem - jimdroberts/FishMMO GitHub Wiki

Description

The ChatSystem is a server-side system in FishMMO responsible for managing all chat-related functionality, including message validation, rate limiting, spam filtering, command parsing, and broadcasting messages to appropriate channels (world, region, party, guild, tell, trade, say, system, and Discord). It polls the database for new chat messages, processes them, and ensures that all chat interactions are secure, efficient, and synchronized across the server and clients.


API Access

Fields

  • public const int MAX_LENGTH

    Maximum allowed chat message length.

  • public bool AllowRepeatMessages

    If true, allows repeat messages from clients without spam filtering.

  • public float MessageRateLimit

    The server chat rate limit in milliseconds. Should match the client's UIChat.messageRateLimit.

  • public float MessagePumpRate

    The server chat message pump rate limit in seconds.

  • public int MessageFetchCount

    Number of chat messages to fetch per database poll.

  • private DateTime lastFetchTime

    Timestamp of the last successful fetch from the database.

  • private long lastFetchPosition

    Position of the last fetched chat message in the database.

  • private float nextPump

    Time remaining until the next database poll for chat messages.

  • private LocalConnectionState serverState

    Current connection state of the server.

Methods

  • public override void InitializeOnce()

    Initializes the chat system, registering broadcast handlers and chat helper commands.

  • public override void Destroying()

    Cleans up the chat system, unregistering broadcast handlers and chat helper commands.

  • void LateUpdate()

    Unity LateUpdate callback. Polls the database for chat messages at the specified rate and processes them.

  • private List FetchChatMessages()

    Fetches new chat messages from the database since the last fetch. Returns: List - List of new chat message entities.

  • private void ProcessChatMessages(List messages)

    Processes a list of chat messages, broadcasting them to appropriate channels and clients. messages (List): List of chat message entities to process.

  • public ChatCommand GetChannelCommand(ChatChannel channel)

    Gets the chat command handler for a specific chat channel. channel (ChatChannel): Chat channel to get the command for. Returns: ChatCommand - Chat command delegate for the channel.

  • private void OnServerChatBroadcastReceived(NetworkConnection conn, ChatBroadcast msg, Channel channel)

    Handles incoming chat broadcast from a character, validates and processes the message. conn (NetworkConnection): Network connection of the sender. msg (ChatBroadcast): Chat broadcast message. channel (Channel): Network channel used for the broadcast.

  • private void ProcessNewChatMessage(NetworkConnection conn, IPlayerCharacter sender, ChatBroadcast msg)

    Parses and processes a new chat message received from a connection, including validation, rate limiting, spam filtering, and command handling. conn (NetworkConnection): Network connection of the sender. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message.

  • public bool OnWorldChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles world chat messages, broadcasting to all characters in the specified world. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - True if message was broadcast, false otherwise.

  • public bool OnRegionChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles region chat messages, broadcasting to all connections in the sender's scene. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - False to prevent message from being written to the database.

  • public bool OnPartyChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles party chat messages, broadcasting to all party members. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - True if message was broadcast, false otherwise.

  • public bool OnGuildChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles guild chat messages, broadcasting to all guild members. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - True if message was broadcast, false otherwise.

  • public bool OnTellChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles tell (private) chat messages, broadcasting to the target character if online. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - True if message was broadcast, false otherwise.

  • public bool OnTradeChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles trade chat messages, broadcasting to all characters in the specified world. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - True if message was broadcast, false otherwise.

  • public bool OnSayChat(IPlayerCharacter sender, ChatBroadcast msg)

    Handles say (local) chat messages, broadcasting to all observers of the sender. sender (IPlayerCharacter): Player character sending the message. msg (ChatBroadcast): Chat broadcast message. Returns: bool - False to prevent message from being written to the database.

  • public void OnSendSystemMessage(NetworkConnection conn, string message)

    Allows the server to send system messages to the connection. conn (NetworkConnection): Network connection to send the system message to. message (string): System message text.

  • public void OnSendDiscordMessage(long worldServerID, long sceneServerID, string message)

    Allows the server to send Discord messages to a specific world server. worldServerID (long): World server ID to send the message to. sceneServerID (long): Scene server ID (for context).\n message (string): Discord message text.


Basic Usage

Setup

  1. Requires the server to be running and the chat system to be initialized as part of the server startup.
  2. All chat channels and commands are registered automatically by the system.
  3. The system automatically polls the database for new chat messages and processes them at the configured rate.
  4. No manual registration is required unless extending or customizing chat logic.

Example

// Example 1: Sending a system message to a player
chatSystem.OnSendSystemMessage(conn, "Welcome to FishMMO!");

// Example 2: Handling a chat broadcast (server-side)
// This is handled automatically by the system when a ChatBroadcast is received from a client.
// No manual invocation is required unless extending or customizing the logic.

Best Practices

  • Always validate and sanitize all incoming chat messages to prevent exploits and abuse.
  • Use rate limiting and spam filtering to maintain chat quality and server performance.
  • Extend the system by implementing new chat channels or commands as needed.
  • Avoid direct manipulation of chat data outside of the provided interfaces and system methods.
  • Ensure all chat-related database operations are efficient and non-blocking to prevent server lag.
⚠️ **GitHub.com Fallback** ⚠️