44-- the necessary modules again
55return [[
66local CONFIG_URL, CONFIG_PORT = ...
7+ -- UPSTREAM CHANGE: Removed json requirement
8+ local json = require("json")
79
810require("love.filesystem")
911local socket = require("socket")
@@ -36,6 +38,11 @@ local uiToNetworkChannel = love.thread.getChannel("uiToNetwork")
3638function Networking.connect()
3739 -- TODO: Check first if Networking.Client is not null
3840 -- and if it is, skip this function
41+ -- UPSTREAM CHANGE: Removed socket connection management logic
42+ if Networking.Client and not isSocketClosed then
43+ Networking.Client:close()
44+ isSocketClosed = true
45+ end
3946
4047 SEND_THREAD_DEBUG_MESSAGE(
4148 string.format("Attempting to connect to multiplayer server... URL: %s, PORT: %d", CONFIG_URL, CONFIG_PORT)
@@ -50,7 +57,15 @@ function Networking.connect()
5057
5158 if connectionResult ~= 1 then
5259 SEND_THREAD_DEBUG_MESSAGE(string.format("%s", errorMessage))
53- networkToUiChannel:push("action:error,message:Failed to connect to multiplayer server")
60+
61+ -- UPSTREAM CHANGE: Changed to string format instead of JSON
62+ local errorMsg = {
63+ action = "error",
64+ message = "Failed to connect to multiplayer server"
65+ }
66+
67+ networkToUiChannel:push(json.encode(errorMsg))
68+ -- UPSTREAM FORMAT: networkToUiChannel:push("action:error,message:Failed to connect to multiplayer server")
5469 else
5570 isSocketClosed = false
5671 end
@@ -67,11 +82,19 @@ local mainThreadMessageQueue = function()
6782 for _ = 1, requestsPerCycle do
6883 local msg = uiToNetworkChannel:pop()
6984 if msg then
70- if msg:find("^action") ~= nil then
71- Networking.Client:send(msg .. "\n")
72- elseif msg == "connect" then
85+ -- UPSTREAM CHANGE: Changed message protocol logic
86+ if msg == "connect" then
7387 Networking.connect()
88+ else
89+ -- Send any non-empty message (JSON or otherwise) to the server
90+ Networking.Client:send(msg .. "\n")
7491 end
92+ -- UPSTREAM FORMAT:
93+ -- if msg:find("^action") ~= nil then
94+ -- Networking.Client:send(msg .. "\n")
95+ -- elseif msg == "connect" then
96+ -- Networking.connect()
97+ -- end
7598 else
7699 -- If there are no more messages, yield
77100 coroutine.yield()
@@ -126,7 +149,12 @@ local networkPacketQueue = function()
126149 isRetry = false
127150
128151 timerCoroutine = coroutine.create(timer)
129- networkToUiChannel:push("action:disconnected")
152+
153+ local disconnectedAction = {
154+ action = "disconnected",
155+ message = "Connection closed by server",
156+ }
157+ networkToUiChannel:push(json.encode(disconnectedAction))
130158 else
131159 -- If there are no more packets, yield
132160 coroutine.yield()
@@ -166,13 +194,17 @@ while true do
166194
167195 timerCoroutine = coroutine.create(timer)
168196
169- networkToUiChannel:push("action:disconnected")
197+ local disconnectedAction = {
198+ action = "disconnected",
199+ message = "Connection closed due to inactivity",
200+ }
201+ networkToUiChannel:push(json.encode(disconnectedAction))
170202 end
171203
172204 if isRetry then
173205 retryCount = retryCount + 1
174206 -- Send keepAlive without cutting the line
175- uiToNetworkChannel:push(" action: keepAlive")
207+ uiToNetworkChannel:push(json.encode({ action = " keepAlive" }) )
176208
177209 -- Restart the timer
178210 timerCoroutine = coroutine.create(timer)
0 commit comments