Skip to content

Commit

Permalink
Fix match and party data message callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Jun 12, 2024
1 parent 905c9dd commit 375cd18
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 28 deletions.
39 changes: 34 additions & 5 deletions codegen/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
if message.match_data then
message.match_data.data = b64.decode(message.match_data.data)
end
if message.cid then
local callback = socket.requests[message.cid]
if callback then
callback(message)
end
socket.requests[message.cid] = nil
end
for event_id,_ in pairs(message) do
if socket.events[event_id] then
socket.events[event_id](message)
Expand All @@ -30,10 +37,22 @@
end
if callback then
socket.engine.socket_send(socket, message, callback)
if message.cid then
socket.requests[message.cid] = callback
socket.engine.socket_send(socket, message)
else
socket.engine.socket_send(socket, message)
callback(true)
end
else
return async(function(done)
socket.engine.socket_send(socket, message, done)
if message.cid then
socket.requests[message.cid] = done
socket.engine.socket_send(socket, message)
else
socket.engine.socket_send(socket, message)
done(true)
end
end)
end
end
Expand All @@ -45,6 +64,10 @@
assert(type(socket) == "table", "The created instance must be a table")
socket.client = client
socket.engine = client.engine
-- callbacks
socket.cid = 0
socket.requests = {}
-- event handlers are registered here
socket.events = {}
Expand Down Expand Up @@ -168,7 +191,6 @@ def type_to_lua(t):
elif t == "map":
return "table"
else:
print("WARNING: Unknown type '%s' - Will use type 'table'" % t)
return "table"


Expand All @@ -194,7 +216,7 @@ def parse_proto_message(message):
return properties


def message_to_lua(message_id, api):
def message_to_lua(message_id, api, wait_for_callback):
message = get_proto_message(message_id, api)
if not message:
print("Unable to find message %s" % message_id)
Expand All @@ -219,7 +241,11 @@ def message_to_lua(message_id, api):
lua = lua + " assert(socket)\n"
for prop in props:
lua = lua + " assert(%s == nil or _G.type(%s) == '%s')\n" % (prop["name"], prop["name"], prop["type"])
if wait_for_callback:
lua = lua + " socket.cid = socket.cid + 1\n"
lua = lua + " local message = {\n"
if wait_for_callback:
lua = lua + " cid = tostring(socket.cid),\n"
lua = lua + " %s = {\n" % message_id
for prop in props:
lua = lua + " %s = %s,\n" % (prop["name"], prop["name"])
Expand Down Expand Up @@ -260,10 +286,13 @@ def messages_to_lua(rtapi):
STATUS_MESSAGES = [ "StatusFollow", "StatusUnfollow", "StatusUpdate" ]
ALL_MESSAGES = CHANNEL_MESSAGES + MATCH_MESSAGES + MATCHMAKER_MESSAGES + PARTY_MESSAGES + STATUS_MESSAGES

NO_CALLBACK_MESSAGES = [ "MatchDataSend", "PartyDataSend" ]

ids = []
lua = ""
for message_id in ALL_MESSAGES:
lua = lua + message_to_lua(message_id, rtapi)
wait_for_callback = (message_id not in NO_CALLBACK_MESSAGES)
lua = lua + message_to_lua(message_id, rtapi, wait_for_callback)
ids.append(message_id)

return { "ids": ids, "lua": lua }
Expand Down
22 changes: 2 additions & 20 deletions nakama/engine/defold.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ function M.socket_create(config, on_message)
local socket = {}
socket.config = config
socket.scheme = config.use_ssl and "wss" or "ws"

socket.cid = 0
socket.requests = {}
socket.on_message = on_message

return socket
Expand All @@ -159,18 +156,7 @@ end
-- internal on_message, calls user defined socket.on_message function
local function on_message(socket, message)
message = json.decode(message)
if not message.cid then
socket.on_message(socket, message)
return
end

local callback = socket.requests[message.cid]
if not callback then
log("Unable to find callback for cid", message.cid)
return
end
socket.requests[message.cid] = nil
callback(message)
socket.on_message(socket, message)
end

--- Connect a created socket using web sockets.
Expand Down Expand Up @@ -210,13 +196,9 @@ end
--- Send a socket message.
-- @param socket The socket table, see socket_create.
-- @param message The message string to send.
-- @param callback The callback function.
function M.socket_send(socket, message, callback)
function M.socket_send(socket, message)
assert(socket and socket.connection, "You must provide a socket")
assert(message, "You must provide a message to send")
socket.cid = socket.cid + 1
message.cid = tostring(socket.cid)
socket.requests[message.cid] = callback

local data = json.encode(message)
-- Fix encoding of match_create and status_update messages to send {} instead of []
Expand Down
Loading

0 comments on commit 375cd18

Please sign in to comment.