Inter Server Chat Example

Demonstrates what you can do with sockets
Unsplashed background img 1

To demonstrate the capabilities of sockets in MundoSK, I decided to make a simple script.

What this script does is combine the chats of two servers. When a player on one server sends a chat message, all players on both servers see it.

The script has two parts: One is the "hub" script, which goes on one server, whose host and port is known to all other servers. The other is the "other server" script, which goes on the other servers.

Hub Script:

on server start:
  set {GlobalChatEnabled} to false

function GlobalChatMessageSender(player: string, msg: string):
  set {_message} to "<%{_player}%>: %{_msg}%"
  broadcast {_message}
  loop {GlobalChatServers::*}:
    write "password", "globalchat", and {_message} to socket with host loop-index port loop-value

function GlobalChatMessageReceiver(msg: strings, info: objects):
  if {_msg::1} is "globalchat":
    GlobalChatMessageSender({_msg::2}, {_msg::3})
  else if {_msg::1} is "addserver":
    set {GlobalChatServers::%{_info::2}%} to {_msg::2} parsed as a number

on chat:
  if {GlobalChatEnabled} is true:
    cancel event
    GlobalChatMessageSender("%player%", message)

command /enableglobalchat:
  trigger:
    set {GlobalChatEnabled} to true
    open function socket at port 25000 with password "password" through function "GlobalChatMessageReceiver"
    broadcast "&2Global Chat Enabled!"

Other Server Script:

on server start:
  set {GlobalChatEnabled} to false

command /enableglobalchat:
  trigger:
    set {GlobalChatEnabled} to true
    open function socket at port 25001 with password "password" through function "GlobalChatBroadcaster"
    write "password", "addserver", and "25001" to socket with host "localhost" port 25000
    broadcast "Global Chat Enabled!"

function GlobalChatBroadcaster(msg: strings, info: objects):
  if {_msg::1} is "globalchat":
    broadcast "%{_msg::2}%"

on chat:
  if {GlobalChatEnabled} is true:
    cancel event
    write "password", "globalchat", "%player%", and "%message%" to socket with host "localhost" port 25000

Here's how the script works:

First, you execute the /enableglobalchat on the hub server. This opens the function socket and broadcasts a fun message. Then, you do the same command on the other servers. This opens a function socket on each of those individual servers, then writes to the hub server with the port of their function socket, which the hub server records in a list variable.

/enableglobalchat also sets to true a variable that allows players' chats to be globalchatted. Once global chat is enabled, whenever a player chats on the hub server, the event is cancelled and both the player and the message are sent to a function which converts the player and the message into a full message, broadcasts it on the hub server, and loops the aformentioned list variable, writing the message to each other server, which then broadcast the message.

The same thing happens with the other servers when a player chats, except they first have to write the player and the message to the hub server's function socket.

This script shows the bare minimum of what can be done with MundoSK sockets. You could have a private message system, a reply system, an auction system, or even more over multiple servers. The possibilities are endless.