BotServer (synchronization)

BotServer - How to use the BotServer

 

All available BotServer functions

BotServer.init(ClientID, ServerID) -- Initialization of the BotServer
BotServer.terminate() -- Terminate the connection to the server
BotServer.listen(topic, function(name, message)) -- Listen to a server topic containing the message
BotServer.send(topic, message) -- Send a server topic to all clients with a message
BotServer.init(ClientID, ServerID)

The init function is how we initialize the server and create a connection to it. To create the server you must call this function with
the desired ID of the server so other clients can connect to it and also the ID of the person who connects to the server.
Usually it's easiest to use the character name of the person who connects to the server.

Example:

BotServer.init(name(), 12345)

In the example above we create a server with the ID 12345 and the client connects to the server with the name() function which returns the character name.

BotServer.listen(topic, function(name, message))

The listen function is how we create a listener to listen for messages that the server sends us, to do this we need to create a "topic" for the listener to listen to and then we need the callback that will be called when we receive the message from the server.

Example:

BotServer.listen("doSay", function(name, message)
  say(message)
end)

In the example above we create a listener to listen for the topic "doSay", if we receive a "doSay" message from the server we will say the message in chat.

BotServer.send(topic, message)

The send function is how we send messages to the server with a topic, this is the messages that our listeners on other clients will listen for.

Example:

BotServer.send("doSay", "Hello")

In the example above we send a message to the server with the topic "doSay" containing the message "Hello", with the listener we created above this will make our character say "Hello" in chat when we send this to the server. 

BotServer.terminate()

Just like the name suggests this is how we terminate the connection if you want to disconnect from the server.

Full code example:

-- Create the connection
BotServer.init(name(), 12345)

-- Create a listener
BotServer.listen("doSay", function(name, message)
  if name == "Leader of the server" then -- We should make sure to only listen for the leaders messages
  	say(message)
  end
end)

-- Send message to the server
BotServer.send("doSay", "Hello")

-- When we are finished terminate connection
BotServer.terminate()