client module
- module
ClientModule
-
- type
Client
A client, which can be used to connect to a server and send and receive messages.
- Parameters:
client – TCPSocket to connect the client to the server.
name – The name of the client.
server – The IP address of the server.
port – The port number of the server.
header_length – The length of the message header in bytes.
eventHandlers – A set of EventHandler instances registered with this client.
rooms – A set of rooms that the client has joined.
kwargs – Additional keyword arguments that will be passed to event handler functions when they are called.
- function
Client
(name, server, port; header_length = 10, kwargs...) Function to create a client struct
- Parameters:
name – The name of the client.
server – The IP address of the server.
port – The port number of the server.
header_length – The length of the message header in bytes.
kwargs – Additional keyword arguments that will be passed to event handler functions when they are called.
- Returns:
The created client
- function
send
(client::Client, message) Send the given message to the server.
- Parameters:
client – Used client struct.
message – The message to send.
- function
register
(client::Client) Register this client with the server. This will send a REGISTER command message to the server and join the reserved rooms.
- Parameters:
client – Used client struct.
- function
receive
(client::Client) Receive a message from the server.
- Parameters:
client – Used client struct.
- Returns:
The received message, or False if an error occurred.
- function
close
(client::Client) Close the connection to the server. This will send a DISCONNECT command message to the server and close the socket.
- Parameters:
client – Used client struct.
- function
add_eventHandler
(client::Client, handleFunction, responseType; responseRooms = nothing, responseComponent = nothing, trigger = nothing) Add a new event handler to this client.
- Parameters:
client – Used client struct.
handleFunction – The function to call when a message is handled by this event handler. This function should take the same arguments as the handle method of the EventHandler class and return a response message, or None if no response is needed.
responseType – The type of the response message.
responseRooms – A list of room IDs where the response message should be sent. If not specified, the response will be sent to the same room as the original message.
responseComponent – The component ID where the response message should be sent.
trigger – A Trigger instance that specifies the criteria for triggering this event handler. If not specified, the event handler will never be triggered.
- function
join_room
(client::Client, room) Join the given room. This will send a JOINROOM command message to the server.
- Parameters:
client – Used client struct.
room – The ID of the room to join.
- function
listen
(client::Client) Listen for incoming messages from the server and handle them using the registered event handlers. This method will block until the connection to the server is closed.
- Parameters:
client – Used client struct.
- function
add_propagated_fields
(client::Client, message, response) Add the fields from the original message that should be propagated to the response message. This includes the root ID, the model status of the original message as well as the name of the sender.
- Parameters:
client – Used client struct.
message – The original message.
response – The response message.
- Returns:
The updated response message.
- type
EventHandler
This typ defines an event handler, which can be used to handle messages that match certain criteria.
- Parameters:
handleFunction – The function to call when a message is handled by this event handler. This function should take the same arguments as the handle method of this class and return a response message, or None if no response is needed.
responseType – The type of the response message.
responseRooms – A list of room IDs where the response message should be sent. If not specified, the response will be sent to the same room as the original message.
responseComponent – The component ID where the response message should be sent.
trigger – A Trigger instance that specifies the criteria for triggering this event handler. If not specified, the event handler will never be triggered.
- function
EventHandler
(handleFunction, responseType; responseRooms = nothing, responseComponent = nothing, trigger = nothing) Function to create a EventHandler.
- Parameters:
handleFunction – The function to call when a message is handled by this event handler. This function should take the same arguments as the handle method of this class and return a response message, or None if no response is needed.
responseType – The type of the response message.
responseRooms – A list of room IDs where the response message should be sent. If not specified, the response will be sent to the same room as the original message.
responseComponent – The component ID where the response message should be sent.
trigger – A Trigger instance that specifies the criteria for triggering this event handler. If not specified, the event handler will never be triggered..
- Returns:
Created EventHandler struct.
- function
is_triggered
(eventHandler::EventHandler, message) Check if this event handler is triggered by the given message.
- Parameters:
eventHandler – Used EventHandler.
message – The message to check.
- Returns:
True if the message matches the criteria specified for this event handler, False otherwise.
- function
handle
(eventHandler::EventHandler, args...; kwargs...) Handle the given message by calling the handleFunction specified in the constructor, and return a response message if needed.
- Parameters:
eventHandler – Used EventHandler.
args – Positional arguments to pass to handleFunction.
kwargs – Keyword arguments to pass to handleFunction.
- Returns:
A response message, or None if no response is needed.
- type
Trigger
This type defines a trigger, which can be used to check if a given message matches certain criteria.
- Parameters:
types – A list of message types that should trigger this trigger.
rooms – A list of room IDs that should trigger this trigger. If not specified, the trigger will be triggered by messages in any room.
directmessage – A boolean indicating whether direct messages (messages not sent to a specific room) should trigger this trigger.
- function
Trigger
(types, rooms; directmessages = false) Function to create a Trigger
- Parameters:
types – A list of message types that should trigger this trigger.
rooms – A list of room IDs that should trigger this trigger. If not specified, the trigger will be triggered by messages in any room.
directmessage – A boolean indicating whether direct messages (messages not sent to a specific room) should trigger this trigger.
- Returns:
Cretated Trigger.
- function
is_triggered
(trigger::Trigger, message) Check if this trigger is triggered by the given message.
- Parameters:
trigger – Used Trigger.
message – The message to check.
- Returns:
True if the message matches the criteria specified for this trigger, False otherwise.
- type