From a806b4ae1ebd9fbb82f373ea3475c7ddb6c5d5e3 Mon Sep 17 00:00:00 2001 From: Oliver Marks Date: Sun, 16 Sep 2018 14:56:16 +0100 Subject: [PATCH] Expose message metadata to user --- client.lua | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/client.lua b/client.lua index c2b5715..ba23f09 100644 --- a/client.lua +++ b/client.lua @@ -54,7 +54,7 @@ function skynet.listen() if URL == skynet.server then local result = json.decode(contents) if result and result.type and result.type == "message" then - os.queueEvent("skynet_message", result.channel, result.message, result.ID, result) + os.queueEvent("skynet_message", result.channel, result.message, result) end end end @@ -62,26 +62,28 @@ end -- Receives one message on given channel -- Will open channel if it is not already open +-- Returns the channel, message, and full message object function skynet.receive(channel) if channel then skynet.open(channel) end local ch, result parallel.waitForAny(skynet.listen, function() repeat - _, ch, result = os.pullEvent "skynet_message" + _, ch, result, obj = os.pullEvent "skynet_message" -- channel being nil means no channel filter. until channel == nil or ch == channel end) - return ch, result + return ch, result, obj end -- Send given data on given channel -function skynet.send(channel, data) - send_raw { - type = "message", - message = data, - channel = channel - } +-- Can accept a third argument - an object of extra metadata to send +function skynet.send(channel, data, full) + local obj = full or {} + obj.type = "message" + obj.message = data + obj.channel = channel + send_raw(obj) end return skynet