Websockets HTML5

Hello! Im trying to create network topdown shooter and got the next problem. Im trying to using websockets (based on https://github.com/britzl/lua-websocket_example by @britzl) and already make it async and get worked on standalone and mobile versions.
I spent full day to understand, why it doesn’t work on HTML5 build. After research and great community in slack: websockets is unsupported on HTML5 build. So question: how I can make network html5 games?
Feature request! The most important part for me. Good day!

2 Likes

Maybe extension system will make it possible to implement easily.

For modern browsers, it’s just using native functions. Defold team to add interface too, or an easier way to interface with any Javascript.

1 Like

I’ve been digging deeper into my original websocket example and I am now very close to a fully functional cross platform websocket solution for Defold.

First of all and most importantly: Emscripten will create websockets automatically when you open sockets. This means that my original example will fail on HTML5 since the whole handshake and encode and decode of websocket frames will be handled by emscripten.

Second: Getting non-blocking sockets to work, regardless of platform is tricky. When setting the socket timeout to 0 you can get timeouts when connecting, sending and receiving data on a socket. You need to handle this in a good way, and I’ve found that it is best handled by using coroutines and yielding while waiting for connection or when reading/writing data.

Finally: I have updated my original example with a fully working cross platform websocket client.

5 Likes

Im trying now your examples and got next:

With simplewebsocket server on python, in your example, all works perfectly, even in HTML5. When i trying to connect to my websocket server on nodeJS got next:

Eos.js:44 Uncaught abort(“Assertion failed: undefined”) at Error
at jsStackTrace …
at stackTrace …
at abort …
at assert …
at handleMessage …
at WebSocket.peer_socket_onmessage …
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.

When I got the message from node, got this error and game crush. It’s strange and for now trying to fix it.
edit: If I dont sending messages from the server, all ok. The socket works on Client->Server, but fails if reverted.
But if i send data from server on “OnMessage” callback, its ok (in response). If send data from server without client response – game error on client

solution:

Was wrong protocol. Dont know why, but in OnMessage callback msg sends in right format. In other places ws.send() sends binary data (when i sended text) and clientfail.

1 Like

I have the same problem. Looks like Sec-WebSocket-Protocol header cause this problem only on browsers.
I don’t understand your solution. Did you change the opcode when sending? If so, how?

I am sorry, but I dont remember anything what I am doing 2+ years ago
Possibly, i tried send text and catch it as text, but it was in binary format which I need to decode

3 Likes

Thank you. I didn’t pay attention to the date :slight_smile: