-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.luau
More file actions
262 lines (220 loc) · 6.55 KB
/
Connection.luau
File metadata and controls
262 lines (220 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
local HttpService = game:GetService("HttpService")
local StudioService = game:GetService("StudioService")
local Root = script:FindFirstAncestor("Metain")
local Config = require(Root.Config)
local UI = require(Root.UI)
local handleCommand = require(Root.Commands.init)
local Connection = {}
local function startHeartbeat(currentWs: WebStreamClient)
local state = Config.state
if state.heartbeatThread then
pcall(task.cancel, state.heartbeatThread)
state.heartbeatThread = nil
end
state.heartbeatThread = task.spawn(function()
while state.isConnected and state.isAuthorized do
task.wait(30)
if not state.isConnected or not state.isAuthorized then break end
pcall(function()
currentWs:Send("ping")
end)
end
end)
end
local function stopHeartbeat()
local state = Config.state
if state.heartbeatThread then
pcall(task.cancel, state.heartbeatThread)
state.heartbeatThread = nil
end
end
local attemptReconnect: () -> ()
local function closeOldWebSocket()
local state = Config.state
if state.ws then
state.connectionId = state.connectionId + 1
pcall(function() (state.ws :: WebStreamClient):Close() end)
state.ws = nil
end
end
local function setupWebSocket(currentWs: WebStreamClient, authMessage: {[string]: any})
local state = Config.state
local myConnId = state.connectionId
currentWs.Opened:Connect(function(_statusCode: number, _headers: string)
if myConnId ~= state.connectionId then return end
state.isConnected = true
state.isReconnecting = false
UI.updateUI()
currentWs:Send(HttpService:JSONEncode(authMessage))
end)
currentWs.MessageReceived:Connect(function(message: string)
if myConnId ~= state.connectionId then return end
if message == "pong" then return end
local parseOk, data = pcall(HttpService.JSONDecode, HttpService, message)
if not parseOk or not data then return end
if data.type == "auth_status" then
if data.status == "authorized" then
state.isAuthorized = true
if data.reconnectToken then
state.reconnectToken = data.reconnectToken
end
startHeartbeat(currentWs)
if state.reconnectResetThread then
pcall(task.cancel, state.reconnectResetThread)
end
state.reconnectResetThread = task.delay(10, function()
state.reconnectResetThread = nil
if state.isConnected and state.isAuthorized then
state.reconnectAttempts = 0
end
end)
elseif data.status == "rejected" then
state.isAuthorized = false
state.reconnectToken = nil
warn("[Metain] " .. (data.reason or "Connection rejected"))
elseif data.status == "disconnected" then
state.isAuthorized = false
end
UI.updateUI()
return
end
if not state.isAuthorized and data.type ~= "ping" then
return
end
local response = handleCommand(data)
if data.requestId then
response.requestId = data.requestId
end
currentWs:Send(HttpService:JSONEncode(response))
end)
local connectionLostHandled = false
local function handleConnectionLost()
if myConnId ~= state.connectionId then return end
if connectionLostHandled then return end
connectionLostHandled = true
state.isConnected = false
state.isAuthorized = false
state.isReconnecting = false
state.ws = nil
stopHeartbeat()
UI.toggleButton:SetActive(false)
UI.updateUI()
attemptReconnect()
end
currentWs.Closed:Connect(handleConnectionLost)
currentWs.Error:Connect(function(statusCode: number, errorMessage: string)
warn("[Metain] WebSocket error:", statusCode, errorMessage)
handleConnectionLost()
end)
end
local function createWebSocket(userId: string): (boolean, WebStreamClient | string)
closeOldWebSocket()
local ok, result = pcall(function()
return HttpService:CreateWebStreamClient(
Enum.WebStreamClientType.WebSocket,
{Url = Config.SERVER_URL .. "/?userId=" .. userId}
)
end)
return ok, result
end
attemptReconnect = function()
local state = Config.state
if state.isReconnecting or state.isConnected then return end
if not state.reconnectToken or not state.lastUserId then return end
if state.reconnectAttempts >= Config.MAX_RECONNECT_ATTEMPTS then
state.reconnectToken = nil
state.reconnectAttempts = 0
state.isReconnecting = false
UI.updateUI()
return
end
state.isReconnecting = true
state.reconnectAttempts = state.reconnectAttempts + 1
local delay = math.min(2 ^ state.reconnectAttempts, 30)
UI.updateUI()
if state.reconnectThread then
pcall(task.cancel, state.reconnectThread)
state.reconnectThread = nil
end
state.reconnectThread = task.delay(delay, function()
state.reconnectThread = nil
if state.isConnected then
state.isReconnecting = false
UI.updateUI()
return
end
local token = state.reconnectToken
local userId = state.lastUserId
if not token or not userId then
state.isReconnecting = false
UI.updateUI()
return
end
local ok, result = createWebSocket(userId)
if not ok then
state.isReconnecting = false
UI.updateUI()
attemptReconnect()
return
end
state.ws = result
setupWebSocket(state.ws :: WebStreamClient, {
type = "plugin_reconnect",
userId = userId,
reconnectToken = token,
pluginVersion = Config.PLUGIN_VERSION,
universeId = tostring(game.GameId),
})
end)
end
function Connection.connect(connectionCode: string)
local state = Config.state
if state.isConnected then return end
if connectionCode == "" then
warn("[Metain] Enter a connection code first")
return
end
UI.toggleButton:SetActive(true)
state.reconnectAttempts = 0
if state.reconnectResetThread then
pcall(task.cancel, state.reconnectResetThread)
state.reconnectResetThread = nil
end
local studioUserId = tostring(StudioService:GetUserId())
state.lastUserId = studioUserId
local ok, result = createWebSocket(studioUserId)
if not ok then
UI.toggleButton:SetActive(false)
warn("[Metain] Failed to create WebSocket:", result)
return
end
state.ws = result
setupWebSocket(state.ws :: WebStreamClient, {
type = "plugin_ready",
userId = studioUserId,
connectionCode = connectionCode:upper(),
pluginVersion = Config.PLUGIN_VERSION,
universeId = tostring(game.GameId),
})
end
function Connection.disconnect()
local state = Config.state
state.reconnectToken = nil
state.reconnectAttempts = 0
state.isReconnecting = false
stopHeartbeat()
if state.reconnectResetThread then
pcall(task.cancel, state.reconnectResetThread)
state.reconnectResetThread = nil
end
if state.reconnectThread then
pcall(task.cancel, state.reconnectThread)
state.reconnectThread = nil
end
closeOldWebSocket()
state.isConnected = false
state.isAuthorized = false
UI.toggleButton:SetActive(false)
UI.updateUI()
end
return Connection