mirror of
https://github.com/defold/extension-websocket.git
synced 2025-09-27 07:52:18 +02:00
Added support for SSL
This commit is contained in:
@@ -1,24 +1,29 @@
|
||||
--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)
|
||||
return gui.is_enabled(node) and action.pressed and gui.pick_node(node, action.x, action.y)
|
||||
end
|
||||
|
||||
local function update_buttons(self)
|
||||
local function http_handle_response(self, id, response)
|
||||
print(response.status, response.response)
|
||||
end
|
||||
|
||||
local function update_gui(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)
|
||||
gui.set_enabled(self.connection_text, true)
|
||||
gui.set_text(self.connection_text, "Connected to " .. self.url)
|
||||
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)
|
||||
gui.set_enabled(self.connection_text, false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -41,7 +46,10 @@ function init(self)
|
||||
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_text = gui.get_node("connection_text")
|
||||
update_gui(self)
|
||||
|
||||
--http.request("https://www.google.com", "GET", http_handle_response)
|
||||
|
||||
self.connection = nil
|
||||
end
|
||||
@@ -59,7 +67,7 @@ end
|
||||
local function websocket_callback(self, conn, data)
|
||||
if data.event == websocket.EVENT_DISCONNECTED then
|
||||
self.connection = nil
|
||||
update_buttons(self)
|
||||
update_gui(self)
|
||||
if data.error then
|
||||
log("Diconnect error:", data.error)
|
||||
self.connection = nil
|
||||
@@ -69,7 +77,7 @@ local function websocket_callback(self, conn, data)
|
||||
log("Connection error:", data.error)
|
||||
self.connection = nil
|
||||
end
|
||||
update_buttons(self)
|
||||
update_gui(self)
|
||||
elseif data.event == websocket.EVENT_MESSAGE then
|
||||
log("Receiving: '" .. tostring(data.message) .. "'")
|
||||
end
|
||||
@@ -84,12 +92,11 @@ local function connect(self, scheme)
|
||||
--options = "all",
|
||||
}
|
||||
|
||||
local url = scheme .. URL
|
||||
|
||||
log("Connecting to " .. url)
|
||||
self.connection = websocket.connect(url, params, websocket_callback)
|
||||
self.url = scheme .. URL
|
||||
log("Connecting to " .. self.url)
|
||||
self.connection = websocket.connect(self.url, params, websocket_callback)
|
||||
if self.connection == nil then
|
||||
print("Failed to connect to " .. url .. ": " .. err)
|
||||
print("Failed to connect to " .. self.url .. ": " .. err)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -104,10 +111,8 @@ 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)
|
||||
|
Reference in New Issue
Block a user