mirror of
https://github.com/defold/extension-websocket.git
synced 2025-09-27 07:52:18 +02:00
first embryo
This commit is contained in:
120
examples/websocket.gui_script
Normal file
120
examples/websocket.gui_script
Normal file
@@ -0,0 +1,120 @@
|
||||
--local websocket_async = require "websocket.client_async"
|
||||
|
||||
local URL="://echo.websocket.org"
|
||||
|
||||
local function click_button(node, action)
|
||||
--print("mouse", action.x, action.y)
|
||||
|
||||
return gui.is_enabled(node) and action.released and gui.pick_node(node, action.x, action.y)
|
||||
end
|
||||
|
||||
local function update_buttons(self)
|
||||
if self.connection then
|
||||
gui.set_enabled(self.connect_ws_node, false)
|
||||
gui.set_enabled(self.connect_wss_node, false)
|
||||
gui.set_enabled(self.send_node, true)
|
||||
gui.set_enabled(self.close_node, true)
|
||||
else
|
||||
gui.set_enabled(self.connect_ws_node, true)
|
||||
gui.set_enabled(self.connect_wss_node, true)
|
||||
gui.set_enabled(self.send_node, false)
|
||||
gui.set_enabled(self.close_node, false)
|
||||
end
|
||||
end
|
||||
|
||||
local function log(...)
|
||||
local text = ""
|
||||
local len = select("#", ...)
|
||||
for i=1,len do
|
||||
text = text .. tostring(select(i, ...)) .. (i == len and "" or ", ")
|
||||
end
|
||||
|
||||
print(text)
|
||||
local node = gui.get_node("log")
|
||||
gui.set_text(node, gui.get_text(node) .. "\n" .. text)
|
||||
end
|
||||
|
||||
function init(self)
|
||||
msg.post(".", "acquire_input_focus")
|
||||
msg.post("@render:", "clear_color", { color = vmath.vector4(0.2, 0.4, 0.8, 1.0) })
|
||||
self.connect_ws_node = gui.get_node("connect_ws/button")
|
||||
self.connect_wss_node = gui.get_node("connect_wss/button")
|
||||
self.send_node = gui.get_node("send/button")
|
||||
self.close_node = gui.get_node("close/button")
|
||||
update_buttons(self)
|
||||
|
||||
self.connection = nil
|
||||
end
|
||||
|
||||
function final(self)
|
||||
msg.post(".", "release_input_focus")
|
||||
end
|
||||
|
||||
function update(self, dt)
|
||||
if self.ws then
|
||||
self.ws.step()
|
||||
end
|
||||
end
|
||||
|
||||
local function websocket_callback(self, conn, data)
|
||||
if data.event == websocket.EVENT_DISCONNECTED then
|
||||
self.connection = nil
|
||||
update_buttons(self)
|
||||
elseif data.event == websocket.EVENT_CONNECTED then
|
||||
if data.error then
|
||||
log("on_connected error", data.error)
|
||||
self.connection = nil
|
||||
end
|
||||
update_buttons(self)
|
||||
elseif data.event == websocket.EVENT_MESSAGE then
|
||||
log("Receiving: '" .. tostring(data.message) .. "'")
|
||||
end
|
||||
end
|
||||
|
||||
local function connect(self, scheme)
|
||||
local params = {
|
||||
mode = "client" --only supported mode currently
|
||||
--mode = "client",
|
||||
--protocol = "tlsv1_2",
|
||||
--verify = "none",
|
||||
--options = "all",
|
||||
}
|
||||
|
||||
local url = scheme .. URL
|
||||
|
||||
log("Connecting to " .. url)
|
||||
local conn, err = websocket.connect(url, params, websocket_callback)
|
||||
if conn == nil then
|
||||
print("Failed to connect to " .. url .. ": " .. err)
|
||||
else
|
||||
self.connection = conn
|
||||
end
|
||||
end
|
||||
|
||||
local function disconnect(self)
|
||||
if self.connection ~= nil then
|
||||
websocket.disconnect(self.connection)
|
||||
end
|
||||
self.connection = nil
|
||||
end
|
||||
|
||||
|
||||
function on_input(self, action_id, action)
|
||||
--print("MAWE", action_id)
|
||||
if click_button(self.connect_ws_node, action) then
|
||||
print("MAWE click connect_ws_node")
|
||||
connect(self, "ws")
|
||||
elseif click_button(self.connect_wss_node, action) then
|
||||
print("MAWE click connect_wss_node")
|
||||
connect(self, "wss")
|
||||
elseif click_button(self.close_node, action) then
|
||||
disconnect(self)
|
||||
elseif click_button(gui.get_node("send/button"), action) then
|
||||
local message_to_send = 'sending to server'
|
||||
local ok, was_clean, code, reason = websocket.send(self.connection, message_to_send)
|
||||
log("Sending '" .. message_to_send .. "'", ok, was_clean, code, reason)
|
||||
elseif click_button(gui.get_node("close/button"), action) then
|
||||
log("Closing")
|
||||
self.ws:close()
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user