Compare commits
No commits in common. "256ab53998503b49036b965fe99a664863777181" and "8fb06c39b1b5a34c4618e88abe2abd4cdc69421f" have entirely different histories.
256ab53998
...
8fb06c39b1
45
README.md
45
README.md
|
@ -1,45 +1,2 @@
|
||||||
# Websocket Client for Roblox
|
# Websocket Client for Roblox
|
||||||
|
TODO: Detailed instructions on how to use this module will be added soon.
|
||||||
## Setup
|
|
||||||
1. Place the WebSocket and Base64 files in ServerScriptService.
|
|
||||||
2. Create a new script in Workspace, or wherever you want.
|
|
||||||
3. If you are hosting your own WebSocket server, replace the URL `https://sock.kcadev.org` with your own.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
```lua
|
|
||||||
local ws = require(game.ServerScriptService:WaitForChild("WebSocket"))
|
|
||||||
local server = ws({
|
|
||||||
url = "wss://echo.websocket.org/",
|
|
||||||
onMessage = function(message)
|
|
||||||
print("Message received: " .. message)
|
|
||||||
end,
|
|
||||||
onError = function(error)
|
|
||||||
print("Error: " .. error)
|
|
||||||
end
|
|
||||||
})
|
|
||||||
server.sendMessage("Hello World")
|
|
||||||
```
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
### ws
|
|
||||||
```lua
|
|
||||||
local server = ws({
|
|
||||||
url = "wss://echo.websocket.org/",
|
|
||||||
onMessage = function(message)
|
|
||||||
print("Message received: " .. message)
|
|
||||||
end,
|
|
||||||
onError = function(error)
|
|
||||||
print("Error: " .. error)
|
|
||||||
end
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### server.sendMessage
|
|
||||||
```lua
|
|
||||||
server.sendMessage("Hello World")
|
|
||||||
```
|
|
||||||
|
|
||||||
### server.close
|
|
||||||
```lua
|
|
||||||
server.close()
|
|
||||||
```
|
|
101
WebSocket.lua
101
WebSocket.lua
|
@ -1,5 +1,6 @@
|
||||||
local Base64 = require(script.Parent:WaitForChild("Base64"))
|
local Base64 = require(script.Base64)
|
||||||
local JobId = game:GetService('HttpService'):GenerateGUID(false)
|
local JobId = game:GetService('HttpService'):GenerateGUID(false)
|
||||||
|
print("jobid is " .. JobId)
|
||||||
local BaseUrl = "https://sock.kcadev.org"
|
local BaseUrl = "https://sock.kcadev.org"
|
||||||
local atob = Base64.atob
|
local atob = Base64.atob
|
||||||
local btoa = Base64.btoa
|
local btoa = Base64.btoa
|
||||||
|
@ -18,83 +19,47 @@ local function notEmpty(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
local ws = function (dict)
|
local ws = function (dict)
|
||||||
local socket = {}
|
|
||||||
local closed = false
|
|
||||||
local onMessage = dict.onMessage or function(msg)
|
local onMessage = dict.onMessage or function(msg)
|
||||||
print(msg)
|
print(msg)
|
||||||
end
|
end
|
||||||
local onError = dict.onError or function(err)
|
|
||||||
print(err)
|
|
||||||
end
|
|
||||||
local server = game:GetService('HttpService')
|
local server = game:GetService('HttpService')
|
||||||
local response, status = server:RequestAsync({
|
local id = server:RequestAsync({
|
||||||
["Url"] = BaseUrl .. "/api/connect/" .. JobId,
|
["Url"] = BaseUrl .. "/connect/" .. JobId,
|
||||||
["Method"] = "PUT",
|
["Method"] = "PUT",
|
||||||
["Body"] = server:JSONEncode({["url"] = dict.url}),
|
["Body"] = dict.url
|
||||||
["Headers"] = {["Content-Type"] = "application/json"}
|
}).Body
|
||||||
})
|
print(id)
|
||||||
if response.Success then
|
print(typeof(id))
|
||||||
local id = response.Body
|
local function sendMessage(msg)
|
||||||
local function sendMessage(msg)
|
wait()
|
||||||
wait()
|
server:PostAsync(BaseUrl .. "api/send/" .. JobId .."/"..id, msg)
|
||||||
local resp = server:RequestAsync({
|
end
|
||||||
["Url"] = BaseUrl .. "/api/send/" .. JobId .."/"..id,
|
|
||||||
["Method"] = "POST",
|
|
||||||
["Body"] = server:JSONEncode({["data"] = msg}),
|
|
||||||
["Headers"] = {["Content-Type"] = "application/json"}
|
|
||||||
})
|
|
||||||
if not resp.Success then
|
|
||||||
onError({message = resp.StatusMessage, code = resp.StatusCode})
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
local function close()
|
||||||
|
server:RequestAsync({
|
||||||
|
["Url"] = BaseUrl .. "api/close/" .. JobId .. "/" .. id,
|
||||||
|
["Method"] = "DELETE"
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
local loop = task.spawn(function()
|
local loop = coroutine.create(function()
|
||||||
while wait(.5) do
|
while wait(50 / 1000) do
|
||||||
local response = server:RequestAsync({
|
local msg = server:GetAsync(BaseUrl .. "poll/"..JobId.."/"..id)
|
||||||
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
|
if notExists(messages, msg) then
|
||||||
["Method"] = "GET"
|
table.insert(messages, msg)
|
||||||
})
|
if notEmpty(msg) then
|
||||||
|
onMessage(msg)
|
||||||
if response.Success then
|
|
||||||
local msg = response.Body
|
|
||||||
if notExists(messages, msg) then
|
|
||||||
table.insert(messages, msg)
|
|
||||||
if notEmpty(msg) then
|
|
||||||
onMessage(socket, msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
onError({message = response.StatusMessage, code = response.StatusCode})
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
|
|
||||||
local function close()
|
|
||||||
if closed then return end
|
|
||||||
closed = true
|
|
||||||
server:RequestAsync({
|
|
||||||
["Url"] = BaseUrl .. "/api/close/" .. JobId .. "/" .. id,
|
|
||||||
["Method"] = "DELETE"
|
|
||||||
})
|
|
||||||
task.cancel(loop)
|
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
game:BindToClose(close)
|
coroutine.resume(loop)
|
||||||
socket = {
|
game:BindToClose(close)
|
||||||
sendMessage = sendMessage,
|
return {
|
||||||
onMessage = onMessage,
|
sendMessage = sendMessage,
|
||||||
close = close
|
onMessage = onMessage,
|
||||||
}
|
close = close
|
||||||
return socket
|
}
|
||||||
else
|
|
||||||
onError({message = response.StatusMessage, code = response.StatusCode})
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return ws
|
return ws
|
||||||
|
|
Loading…
Reference in a new issue