Compare commits

..

No commits in common. "256ab53998503b49036b965fe99a664863777181" and "8fb06c39b1b5a34c4618e88abe2abd4cdc69421f" have entirely different histories.

2 changed files with 34 additions and 112 deletions

View file

@ -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()
```

View file

@ -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()
local resp = server:RequestAsync({ server:PostAsync(BaseUrl .. "api/send/" .. JobId .."/"..id, msg)
["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 end
local function close()
local loop = task.spawn(function() server:RequestAsync({
while wait(.5) do ["Url"] = BaseUrl .. "api/close/" .. JobId .. "/" .. id,
local response = server:RequestAsync({ ["Method"] = "DELETE"
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
["Method"] = "GET"
}) })
end
if response.Success then local loop = coroutine.create(function()
local msg = response.Body while wait(50 / 1000) do
local msg = server:GetAsync(BaseUrl .. "poll/"..JobId.."/"..id)
if notExists(messages, msg) then if notExists(messages, msg) then
table.insert(messages, msg) table.insert(messages, msg)
if notEmpty(msg) then if notEmpty(msg) then
onMessage(socket, msg) onMessage(msg)
end end
end end
else
onError({message = response.StatusMessage, code = response.StatusCode})
break
end
end end
end) end)
coroutine.resume(loop)
local function close()
if closed then return end
closed = true
server:RequestAsync({
["Url"] = BaseUrl .. "/api/close/" .. JobId .. "/" .. id,
["Method"] = "DELETE"
})
task.cancel(loop)
end
game:BindToClose(close) game:BindToClose(close)
socket = { return {
sendMessage = sendMessage, sendMessage = sendMessage,
onMessage = onMessage, onMessage = onMessage,
close = close close = close
} }
return socket
else
onError({message = response.StatusMessage, code = response.StatusCode})
return false
end
end end
return ws return ws