Addon messages are an easy way to do it:
E.g, taken from one of my scripts, I listen for messages this way:
function eventHandlerMainFrame(self, event, message, _, Type, Sender)
if (event == "CHAT_MSG_ADDON" and Sender == UnitName("player")) then
local packet, link, linkCount, MSG = message:match("(%d%d%d)(%d%d)(%d%d)(.+)");
if not packet or not link or not linkCount or not MSG then
return
end
packet, link, linkCount = tonumber(packet), tonumber(link), tonumber(linkCount);
links[packet] = links[packet] or {count = 0};
links[packet][link] = MSG;
links[packet].count = links[packet].count + 1;
if (links[packet].count ~= linkCount) then
return
end
local fullMessage = table.concat(links[packet]);
links[packet] = {count = 0};
-- Handle addon messages
-- Handle game list
if string.starts(fullMessage, "GAMES-") then
local tokens = scen_split(fullMessage)
local pos = 1
for i=2, #tokens, 2 do
ONLINE_PLAYERS[pos] = {tokens[i + 1], tokens[i]}
pos = pos + 1
end
-- update view
SB_Main_ScrollBar_Update()
elseif string.starts(fullMessage, "PLAYERS-") then
local tokens = scen_split(fullMessage)
for i=2, #tokens do
ONLINE_PLAYERS[i - 1] = {tokens[i], "1"}
end
-- update view
SB_Main_ScrollBar_Update()
elseif string.starts(fullMessage, "SelectedPerks-") then
local tokens = scen_split(fullMessage)
local perks = tokens[2]
local offsets = {1, 3, 5, 7}
for i=1,4 do
SELECTED_PERKS[i] = tonumber(perks:sub(offsets[i], offsets[i] + 1))
end
end
end
end
I send addon messages this way:
SendAddonMessage("CREATEGAME", messageToSend, "WHISPER", UnitName("player"))
I send messages from the server to client this way:
void WorldSession::SendAddonMessage(Player* player, std::string message, uint32 packet)
{
uint32 splitLength = 240;
uint32 splits = ceil((float)message.length() / (float)splitLength);
uint32 counter = 1;
for (uint32 i = 0; i < message.length(); i += splitLength)
{
std::stringstream send;
send << std::setfill('0') << std::setw(3) << packet;
send << std::setw(2) << counter;
send << std::setw(2) << splits;
send << message.substr(i, std::min(splitLength, (uint32)(message.length() - i)));
counter = counter + 1;
WorldPacket* data = new WorldPacket();
uint32 messageLength = send.str().length() + 1;
data->Initialize(SMSG_MESSAGECHAT, 100);
*data << (uint8)CHAT_MSG_SYSTEM;
*data << LANG_ADDON;
*data << player->GetGUID();
*data << uint32(0);
*data << player->GetGUID();
*data << messageLength;
*data << send.str().c_str();
*data << uint8(0);
player->GetSession()->SendPacket(data);
TC_LOG_INFO("server.debug", "[DEBUG] Sent: %s", send.str().c_str());
}
}