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
## 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()
```
TODO: Detailed instructions on how to use this module will be added soon.

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)
print("jobid is " .. JobId)
local BaseUrl = "https://sock.kcadev.org"
local atob = Base64.atob
local btoa = Base64.btoa
@ -18,83 +19,47 @@ local function notEmpty(s)
end
local ws = function (dict)
local socket = {}
local closed = false
local onMessage = dict.onMessage or function(msg)
print(msg)
end
local onError = dict.onError or function(err)
print(err)
end
local server = game:GetService('HttpService')
local response, status = server:RequestAsync({
["Url"] = BaseUrl .. "/api/connect/" .. JobId,
local id = server:RequestAsync({
["Url"] = BaseUrl .. "/connect/" .. JobId,
["Method"] = "PUT",
["Body"] = server:JSONEncode({["url"] = dict.url}),
["Headers"] = {["Content-Type"] = "application/json"}
})
if response.Success then
local id = response.Body
["Body"] = dict.url
}).Body
print(id)
print(typeof(id))
local function sendMessage(msg)
wait()
local resp = server:RequestAsync({
["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
server:PostAsync(BaseUrl .. "api/send/" .. JobId .."/"..id, msg)
end
local loop = task.spawn(function()
while wait(.5) do
local response = server:RequestAsync({
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
["Method"] = "GET"
local function close()
server:RequestAsync({
["Url"] = BaseUrl .. "api/close/" .. JobId .. "/" .. id,
["Method"] = "DELETE"
})
end
if response.Success then
local msg = response.Body
local loop = coroutine.create(function()
while wait(50 / 1000) do
local msg = server:GetAsync(BaseUrl .. "poll/"..JobId.."/"..id)
if notExists(messages, msg) then
table.insert(messages, msg)
if notEmpty(msg) then
onMessage(socket, msg)
onMessage(msg)
end
end
else
onError({message = response.StatusMessage, code = response.StatusCode})
break
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
coroutine.resume(loop)
game:BindToClose(close)
socket = {
return {
sendMessage = sendMessage,
onMessage = onMessage,
close = close
}
return socket
else
onError({message = response.StatusMessage, code = response.StatusCode})
return false
end
end
return ws