Websockets and Emscripten

1,962 views
Skip to first unread message

Thomas Arnbjerg

unread,
Jan 18, 2017, 9:48:22 AM1/18/17
to emscripten-discuss
Has anyone succeded in connecting an Emscripten socket client against a libWebsocket based server?

Floh

unread,
Jan 18, 2017, 11:20:40 AM1/18/17
to emscripten-discuss
Yes here :) We have recently built a little proof-of-concept client/server prototype where natively compiled clients connect through a TCP socket, and emscripten clients connect through WebSocket, both compiled from the same C++ source.

The game server was written in golang, and we used https://github.com/gorilla/websocket as websocket proxy layer in the game server process, the game server opened one normal TCP port with Golang's native TCP support, and a separate WebSocket port next to it, managed by Gorilla.

On the client side I wrote a minimal client socket wrapper which uses non-blocking sockets across all platforms (emscripten's socket wrapper is generally non-blocking).

Unfortunately the whole project is closed source, but here's a gist with the (very quick'n'dirty) NetClient implementation which has all the interesting client-side stuff in it:


We created 3 client types, one written in Unity exported to asm.js, one with Oryol (https://github.com/floooh/oryol), and one in Typescript with babylon.js (http://babylonjs.com/).

The Unity client was about 4.9 MByte (theoretically we could have get this down to around 3.5 MB because the Unity physics code slipped in because of a stabbing test against the ground), the Oryol client around 410 KBytes, and the Typescript client around 310 KBytes (mostly the minimized babylon.js blob), all sizes are compressed sizes.

Fun project all in all :)

Cheers,
-Floh.

Floh

unread,
Jan 18, 2017, 11:23:40 AM1/18/17
to emscripten-discuss
PS: I should mention that we tried different WebSocket wrapper solutions (e.g. websockify's node and python versions running in a separate process), and we ran into various compatibility problems between the different client-side WebSocket implementations and the server side, mostly some websocket "subprotocol" details that hadn't been implemented, so that the WebSocket handshake between browser and server was denied by the browser because the server didn't properly react to the subprotocol request.

The Gorilla package worked with all client types and that's what we used in the end.

Thomas Arnbjerg

unread,
Jan 20, 2017, 3:20:21 AM1/20/17
to emscripten-discuss
Hi Floh,
Thank's for sharing your experience - great input. I'll share my experience with libwebsockets when I have something to share..
Br
Thomas

Jean-Marc Le Roux

unread,
Jan 25, 2017, 9:37:14 AM1/25/17
to emscripten-discuss
Fully working and cross-platform (Windows, OSX, Linux, Android, iOS, Emscripten) WebSocket implementation :


We're using the "websocket" NodeJS package server side.
But it should work with any websocket server impl.
Reply all
Reply to author
Forward
0 new messages