DefoldWebSocket not connecting

#1

Defolder,
This may be the wrong place to post this, but my current test program using the DefoldWebSocket extension (And the 2 dependencies, LuaSec and LuaSocket).
My current program should attempt a connection to a test server hosted on a repl but the server receives nothing.
My main.script is as follows:

local client_async = require "websocket.client_async"
function init(self)
	print("initialized network module")
	self.ws = client_async({
		connect_timeout = 10,
	})
	self.servertoken = "OXPqhF+MJO%Gn|vstuIRK8csRdg=59RTnW6%K=1o$wZrF!o3GAMzutJlcPlSoTcvNbH9AKnmU8MY^nk4fz&+3Vgt%?^ZfJfAAVI0_qTYjO_|mSlIKi##OahEFK_z+81K|vjuu!aoJBKwxRKr&|USxY_*-=Uukrk966dqFAJuWQs&u0QgnxZdUzPaSYOLmu$SAcBh#chfM3!IEJs9@0@_An-9BrTigtbRkGgCwxS-2EJ$yovp%TINbb$CNLKG2X%d"
	self.serveraddress = "ws://parry-host.grify.repl.co:25434"
	local sslparams = {
		mode = "client",
		protocol = "tlsv1_2",
		verify = "none",
		options = "all",
	}
	self.ws:on_connected(function(ok, err)
		if ok then
			print("Connected ws")
		else
			print("Unable to connect", err)
		end
	end)
	self.ws:on_disconnected(function()
		print("Disconnected")
	end)
	self.ws:on_message(function(message)
		print("Received message".. message)
	end)
	print("Touch!")
	print("Trying to reach Parry servers")
	self.ws:connect("ws://super-basic-ws-server.grify.repl.co", self.servertoken)
end

Thank you,
Grify

P.S. I suppose I’ll have to change the server token now, won’t I XD

0 Likes

#2

Have you tested the server with another type of test WS client / tested the Defold side with a test WS server?

2 Likes

#3

Yes, I tested the server with a vanilla node WS client and it worked perfectly. I even did a full rewrite of the server and client to be the most bare bones version that should still work, and it didn’t.

0 Likes

#4

The WS server is a unencrypted nodeJS server on port 25434

0 Likes

#5

I did a bit of debugging and the code manages to create a TCP socket to 35.201.120.147 on port 25434. It then goes into a loop waiting for the socket to be writable, but it never becomes ready. I do not know why.

I tried a Chrome extension for WebSocket testing (https://chrome.google.com/webstore/detail/websocket-test-client/fgponpodhbmadfljofbimhhlengambbn/related?hl=en) and it failed to connect as well. The same extension (and defold-websocket) successfully connected to ws://echo.websocket.org

2 Likes

#6

Does defoldwebsocket not support custom ports, or ports in a certain range? My tests on ports localhost:80, localhost:443, localhost:8080, 20222 and of course 25434. None of them worked, but neither did a connection to ws://echo.websocket.org which seems to not connect as well.
Here’s my (condensed) client code that is failing to connect to them all:

local client_async = require "websocket.client_async"
function init(self)
	self.ws = client_async({
		connect_timeout = 10,
	})
	self.ws:on_connected(function(ok, err)
		if ok then
			print("Connected ws")
		else
			print("Unable to connect", err)
		end
	end)
	self.ws:on_disconnected(function()
		print("Disconnected")
	end)
	self.ws:on_message(function(message)
		print("Received message".. message)
	end)
	self.ws:connect("ws://echo.websocket.org")
end

and here is my WebSocketServer code (nodeJS, can be run locally with node cli or on repl.it):

const http = require("http")

const WebSocket = require("ws")

const WebSocketServer = require("websocket").server

const server = http.createServer((req, res) => {

res.writeHead(200)

res.write("http server ok")

res.end()

}).listen(20222)

const wsServer = new WebSocketServer({

httpServer: server

})

wsServer.on("request", req => {

console.log("New request")

console.log(req.requestedProtocols[0])

const connection = req.accept(req.requestedProtocols[0])

console.log("connection")

connection.on("message", console.log)

})

Frustrating, yes?

0 Likes

#7

Can you please try the example included in the project itself? https://github.com/britzl/defold-websocket

1 Like

#8

the example project posted by @britzl is working fine for me!

I’ve been sending some absolute filth to that echo server. I feel sorry for the guy sending it back to me.

4 Likes

#9

I found the problem and I’m terribly ashamed of myself. Carry on, and have a nice day!
Thanks,
grify

0 Likes

#10

Great! What was the problem and the solution?

1 Like

#11

Firewall? :thinking:

0 Likes

#12

Haha no, even more ridiculous than that.

Apparently, the

self.ws:step()

isn’t optional.
And apparently my vision isn’t quite what I thought it was either.
Apologies for causing such a commotion over such a simple error, but thank you very much for your support, it was much more than inspiring.
See you next time with a missing semicolon,
Honestly thanks for your help,
grify

1 Like

#13

It happens. Just today I accidentally put steamworks.final() in our game’s update function and spent longer than I should have trying to figure out why it wasn’t working. :crazy_face:

1 Like

#14

BTW Did you see our announcement of a new native websocket implementation?

2 Likes

#15

This is awesome!!!

Where can I switch to beta build server and is there a different place I should go to for help with this?

If this works, it will save me hundreds of hours of development time :slight_smile:

Thank you so much for making this :heart:
grify

0 Likes

#16

No need to switch. We released the new version yesterday. A good starting point is this page: https://defold.com/extension-websocket/

2 Likes

#17

Thank you so much! You’re always very helpful!
I can’t wait to reformat my server to actually work properly now :]
Have a pleasant week!
Thanks again,
grify

2 Likes

#18

Printed and pinned to my wall :slight_smile:

3 Likes