Websocket how to send binary text messages , also does it support wss?

  1. 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?

  2. Does the extension support secure WebSocket sending with the wss:// prefix?

  3. 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

1 Like
  1. it uses the Lua string you pass to it. A Lua string can contain binary data.

  2. yes, it supports wss

  3. ” How does Defold load the WebSocket extension without compiling it”

It does compile it into the executable,just like any ithe native extension.

Thanks ,
is there any flags i need to add to make it support secure ? and binary ?
regarding 3 , i didn’t saw any compilation going on when i added the zip to the dependencies …

You need to connect to wss://mydomain.com and not ws://mydomain.com

Everything you send is stream of bytes, represented by a Lua string. You don’t need to do anything in particular. Generate bytes in a string and send it.

The first time you build a project with native code, the native code is sent to build.defold.com, and compiled and linked into a custom engine which gets downloaded and stored in the .internal/cache folder in your project. In the editor you see a “Building engine” progress text while building the first time.

1 Like