Websocket message handling

Hi,
I used defold-websocket by britzel

I am having problem with handling on_message:

global_ws:on_message( function(message)
		print("Received message", message)
		msg = split(message,"/*^*/")
		switch(msg[1])
		if tonumber(msg[1]) == 9001 then
			print ("Room Joined")
		end
	end)

I am trying to split the incoming message using this split function

function split(s, sep)
	local fields = {}
	local sep = sep or " "
	local pattern = string.format("([^%s]+)", sep)
	string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
	return fields
end

It works, and I am able to see the output:

    DEBUG:SCRIPT: Cursor out	hash: [/instance4]	hash: [green]	68.5	190.5
    DEBUG:SCRIPT: Cursor over	hash: [/instance3]	hash: [green]	79.5	171.5
    DEBUG:SCRIPT: Pressed	hash: [/instance3]	hash: [green]	113.5	125.5
    DEBUG:SCRIPT: Clicked	hash: [/instance3]	hash: [green]	115.5	124.5
    DEBUG:SCRIPT: calc
    DEBUG:SCRIPT: Released	hash: [/instance3]	hash: [green]	115.5	124.5
    DEBUG:SCRIPT: 9001/*^*/Unknown-2824 joined the room. > 10
    DEBUG:SCRIPT: 
    { --[[0000020C99703D50]]
      1 = "9001",
      2 = "Unknown-2824 joined the room. > 10"
    }
    DEBUG:SCRIPT: 1	9001
    DEBUG:SCRIPT: 2	Unknown-2824 joined the room. > 10

However,
(as I am modifying https://github.com/britzl/defold-input/tree/master/examples/cursor project to fit my need )

It throws error asap I move my mouse away from Any button.

DEBUG:SCRIPT: 1	9001
DEBUG:SCRIPT: 2	Unknown-2824 joined the room. > 10
ERROR:SCRIPT: /in/cursor.lua:16: attempt to call field 'url' (a nil value)
stack traceback:
	/in/cursor.lua:16: in function 'url_to_key'
	/in/cursor.lua:22: in function 'trigger'
	/in/cursor.script:97: in function 'trigger_event'
	/in/cursor.script:169: in function </in/cursor.script:114>

Why is cursor unable to find url_to_key?

This is the UI:

  1. I copy room name into clipboard and paste it into the Game using the clipboard icon.
  2. I click on Host button to Join room ( App auto connects to WSS at launch )
  3. I can see it joined the room as server sends back confirmation (where split received message happens)
  4. Then I move my mouse away from HOST button, and UI freeze and Debug screen spams with cursor url error thing.

This works though:

global_ws:on_message( function(message)
		print("Received message", message)
		
		local fields = {}
		local sep = "/*^*/"
		local pattern = string.format("([^%s]+)", sep)
		string.gsub(message, pattern, function(c) fields[#fields + 1] = c end)
		--switch(fields[1])
		if tonumber(fields[1]) == 9001 then
			print ("Room Joined")
		end
		
	end)

Oh, I would really recommend that you switch to the official WebSocket extension which we released just a few weeks ago:

It’s better in every way. The defold-websocket library can still be used but should be considered as deprecated.

For some reason Native Websocket Extension library does not play well with python websocket server.

In yours, I am able to join room and server accepts the cmd and sends reply back to all connected clients.
But, in Native version server receives the cmd but does nothing.
I see nobody join the room in HTML client as well.

Perhaps you need to use text mode?

2 Likes

These are issues which we definitely want to solve. Please create tickets for these in the extension repo!