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
|
# 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()
|
||||||
|
```
|
107
WebSocket.lua
107
WebSocket.lua
|
@ -1,6 +1,5 @@
|
||||||
local Base64 = require(script.Base64)
|
local Base64 = require(script.Parent:WaitForChild("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
|
||||||
|
@ -19,47 +18,83 @@ 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 id = server:RequestAsync({
|
local response, status = server:RequestAsync({
|
||||||
["Url"] = BaseUrl .. "/connect/" .. JobId,
|
["Url"] = BaseUrl .. "/api/connect/" .. JobId,
|
||||||
["Method"] = "PUT",
|
["Method"] = "PUT",
|
||||||
["Body"] = dict.url
|
["Body"] = server:JSONEncode({["url"] = dict.url}),
|
||||||
}).Body
|
["Headers"] = {["Content-Type"] = "application/json"}
|
||||||
print(id)
|
})
|
||||||
print(typeof(id))
|
if response.Success then
|
||||||
local function sendMessage(msg)
|
local id = response.Body
|
||||||
wait()
|
local function sendMessage(msg)
|
||||||
server:PostAsync(BaseUrl .. "api/send/" .. JobId .."/"..id, msg)
|
wait()
|
||||||
end
|
local resp = server:RequestAsync({
|
||||||
|
["Url"] = BaseUrl .. "/api/send/" .. JobId .."/"..id,
|
||||||
local function close()
|
["Method"] = "POST",
|
||||||
server:RequestAsync({
|
["Body"] = server:JSONEncode({["data"] = msg}),
|
||||||
["Url"] = BaseUrl .. "api/close/" .. JobId .. "/" .. id,
|
["Headers"] = {["Content-Type"] = "application/json"}
|
||||||
["Method"] = "DELETE"
|
})
|
||||||
})
|
if not resp.Success then
|
||||||
end
|
onError({message = resp.StatusMessage, code = resp.StatusCode})
|
||||||
|
return false
|
||||||
local loop = coroutine.create(function()
|
else
|
||||||
while wait(50 / 1000) do
|
return true
|
||||||
local msg = server:GetAsync(BaseUrl .. "poll/"..JobId.."/"..id)
|
|
||||||
if notExists(messages, msg) then
|
|
||||||
table.insert(messages, msg)
|
|
||||||
if notEmpty(msg) then
|
|
||||||
onMessage(msg)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
coroutine.resume(loop)
|
|
||||||
game:BindToClose(close)
|
local loop = task.spawn(function()
|
||||||
return {
|
while wait(.5) do
|
||||||
sendMessage = sendMessage,
|
local response = server:RequestAsync({
|
||||||
onMessage = onMessage,
|
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
|
||||||
close = close
|
["Method"] = "GET"
|
||||||
}
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
socket = {
|
||||||
|
sendMessage = sendMessage,
|
||||||
|
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