Compare commits
6 commits
8fb06c39b1
...
256ab53998
Author | SHA1 | Date | |
---|---|---|---|
|
256ab53998 | ||
|
af26ff5dd5 | ||
|
923b4654d1 | ||
|
dfaecbfec7 | ||
|
2f947401da | ||
|
77d779dd4d |
45
README.md
45
README.md
|
@ -1,2 +1,45 @@
|
|||
# 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()
|
||||
```
|
|
@ -1,6 +1,5 @@
|
|||
local Base64 = require(script.Base64)
|
||||
local Base64 = require(script.Parent:WaitForChild("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
|
||||
|
@ -19,47 +18,83 @@ 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 id = server:RequestAsync({
|
||||
["Url"] = BaseUrl .. "/connect/" .. JobId,
|
||||
local response, status = server:RequestAsync({
|
||||
["Url"] = BaseUrl .. "/api/connect/" .. JobId,
|
||||
["Method"] = "PUT",
|
||||
["Body"] = dict.url
|
||||
}).Body
|
||||
print(id)
|
||||
print(typeof(id))
|
||||
["Body"] = server:JSONEncode({["url"] = dict.url}),
|
||||
["Headers"] = {["Content-Type"] = "application/json"}
|
||||
})
|
||||
if response.Success then
|
||||
local id = response.Body
|
||||
local function sendMessage(msg)
|
||||
wait()
|
||||
server:PostAsync(BaseUrl .. "api/send/" .. JobId .."/"..id, msg)
|
||||
end
|
||||
|
||||
local function close()
|
||||
server:RequestAsync({
|
||||
["Url"] = BaseUrl .. "api/close/" .. JobId .. "/" .. id,
|
||||
["Method"] = "DELETE"
|
||||
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
|
||||
end
|
||||
|
||||
local loop = coroutine.create(function()
|
||||
while wait(50 / 1000) do
|
||||
local msg = server:GetAsync(BaseUrl .. "poll/"..JobId.."/"..id)
|
||||
|
||||
local loop = task.spawn(function()
|
||||
while wait(.5) do
|
||||
local response = server:RequestAsync({
|
||||
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
|
||||
["Method"] = "GET"
|
||||
})
|
||||
|
||||
if response.Success then
|
||||
local msg = response.Body
|
||||
if notExists(messages, msg) then
|
||||
table.insert(messages, msg)
|
||||
if notEmpty(msg) then
|
||||
onMessage(msg)
|
||||
onMessage(socket, msg)
|
||||
end
|
||||
end
|
||||
else
|
||||
onError({message = response.StatusMessage, code = response.StatusCode})
|
||||
break
|
||||
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)
|
||||
return {
|
||||
socket = {
|
||||
sendMessage = sendMessage,
|
||||
onMessage = onMessage,
|
||||
close = close
|
||||
}
|
||||
return socket
|
||||
else
|
||||
onError({message = response.StatusMessage, code = response.StatusCode})
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return ws
|
||||
|
|
Loading…
Reference in a new issue