-
In what format does the Defold client send messages? Is it pure text? How can I send and receive text in binary format, similar to how it can be done in JavaScript using, for example,
connection.binaryType = "arraybuffer"
? Also, how can I receive and parse binary text from the server? -
Does the extension support secure WebSocket sending with the
wss://
prefix? -
The final question: How does Defold load the WebSocket extension without compiling it, only using the zip file? Where can I read about how it accomplishes this?
my defold code :
local M = {}
M.WSURL = "ws://localhost:8001/test"
function M.websocket_callback(self, conn, data)
if data.event == websocket.EVENT_DISCONNECTED then
print("Disconnected: " .. tostring(conn))
self.connection = nil
--update_gui(self)
elseif data.event == websocket.EVENT_CONNECTED then
--update_gui(self)
print("Connected: " .. tostring(conn))
elseif data.event == websocket.EVENT_ERROR then
print("Error: '" .. tostring(data.message) .. "'")
if data.handshake_response then
print("Handshake response status: '" .. tostring(data.handshake_response.status) .. "'")
for key, value in pairs(data.handshake_response.headers) do
print("Handshake response header: '" .. key .. ": " .. value .. "'")
end
print("Handshake response body: '" .. tostring(data.handshake_response.response) .. "'")
end
elseif data.event == websocket.EVENT_MESSAGE then
print("Receiving: '" .. tostring(data.message) .. "'")
end
end
return M
then calling it :
local networkmaneger = require ("main/scripts.network_maneger")
function init(self)
msg.post(".","acquire_input_focus")
msg.post("#","show_level_select")
self.active = false
self.url = networkmaneger.WSURL
print(self.url)
local params = {
timeout = 300000,
headers = "Sec-WebSocket-Protocol: chat\r\nOrigin: mydomain.com\r\n"
}
self.connection = websocket.connect(self.url, params, networkmaneger.websocket_callback)
end
function on_message(self, message_id, message, sender)
if message_id == hash("show_level_select") then
msg.post("#","enable")
self.active = true
elseif message_id == hash("hide_level_select") then
msg.post("#","disable")
self.active = false
end
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed and self.active then
print("button pressed1111")
local message_to_send = 'sending to server'
local ok, was_clean, code, reason = websocket.send(self.connection, message_to_send)
print("Sending '" .. message_to_send .. "'", ok, was_clean, code, reason)
end
end
function final(self)
if self.connection ~= nil then
websocket.disconnect(self.connection)
end
end
Thanks