Websocket problem

Has anyone been able to use sockets in HTML5?

When I try to connect, Chrome gives me the error:
“WebSocket is closed before the connection is established.”

This is my code:

function init(self)
s = socket.connect(“localhost”, 10000)
s:send(“test”)
end

I’ve tested it against my own node server and echo.websocket.org on port 8080.

Thanks!

1 Like

Issue others have had too

I did some more research and as was pointed out in the linked post you can’t connect using a normal socket. A WebSocket requires a handshake and more things to get it set up properly. Luckily there’s a lua-websocket project on GitHub that with some modifications can be made to work with Defold.

I’ve put together a minimal example of a WebSocket client connecting to an echo server and sending and receiving data. Note that this implementation uses the synchronous version of lua-websocket. I couldn’t get the copas based version to work. It should however be fairly easy to roll your own async version using coroutines. I put the code here:

4 Likes

Sweet!

Took a look and this also adds some other nice things like sha1.

Thanks @britzl!

Thanks britzl!

For example worked for me in the standalone build, but still fails in browser, and without any error feedback from Chrome.

My original code attempts a web socket handshake. I can see it with a simple netcat dump on that port. I believe emscripten provides a web socket implementation, but Luasockets seems to break it. I saw people on a gideros forum posting the same error.

I’m going to ping this one last time before giving up. The solution posted did not solve the problem while running in html5, and as this engine is closed source I can’t probe further. Defold is a promising engine that I’d like to use for this project, but the lack of usable html5 websockets is a dealbreaker for me unfortunately.

1 Like

@catluck: I actually brought this up today at work. The thing is that we’re currently using plain Lua 5.1 on web, while we use LuaJIT for all other platforms. LuaJIT comes with the bit-op library while plain Lua 5.1 is missing it. The library is needed for the websocket handshake and this explains why it works on desktop but not in a browser. I will try to push for us to include the bit-op lib in web builds, but it will still take at least a few weeks before we can expect to see this in a release.

3 Likes

How confident are you in the eventuality of it working? Could I start now and test using the native build knowing that it will eventually work in HTML5?

1 Like

As far as I know it is the lack of the Lua BitOp library that is preventing WebSockets in HTML5 builds from working. But, there could be other hidden issues once BitOp is in place, but I doubt it. Still, I cannot guarantee when we would be able to release this. I think this is an important issue to solve, but we have many stake holders and many different and all very important things in our backlog.

3 Likes