With an empty clean project, and this code based on the example page:
local client_async = require "websocket.client_async"
function init(self)
self.ws = client_async()
self.ws:on_connected(function(ok, err)
if ok then
print("Connected")
msg.post("#", "acquire_input_focus")
else
print("Unable to connect", err)
end
end)
self.ws:on_disconnected(function()
print("Disconnected")
end)
self.ws:connect("ws://localhost:8080")
end
function update(self, dt)
self.ws:step()
end
It prints the message properly if the server is running. But if the server is not running, the “Unable to connect” does not get printed.
I also tried Colyseus, and it’s the same thing. The “on error” event there also doesn’t get triggered.
I tried it on the latest editor 1.2.142, but I’ve never been successful in older versions either. I also tried html5 build, I do get a websocket error there.
However, this error is not captured in code (i.e. can’t connect error doesn’t even get printed in the console).
How do I capture this connection refused properly in code, so I can display an error message properly? Potential workaround is to have a timeout e.g. if after 2 seconds I am still not connected, then I can assume something is wrong with the connection. But I am hoping this is something the extension is capturing properly, and I just don’t know how to make it work.