Need lightweight/simple example of websockets.h usage

75 views
Skip to first unread message

qrss qrss

unread,
Sep 8, 2020, 4:49:38 PM9/8/20
to emscripten-discuss
I am searching for a way to enable client-side socket communications (open socket, send data to socket, read data from socket, close socket) in a language program compiled with emscripten emcc and run as a webassembly program in a browser. I'd like to see if Emscripten WebSockets API is a suitable vehicle, as I'd prefer to stick solely with C language and avoid using Javascript as an itermediary between my (wasm) client and the browser/server environment.


I have not been able to find any getting-started guide or information on Emscripten WebSockets API -- ideally I'd like to find a "hello world" level program (written in C) that shows usage of this API.

Thanks very much for any advice/pointers.

Dave

###


Emscripten WebSockets API


WebSockets API provides connection-oriented message-framed bidirectional asynchronous networking communication to the browser. It is the closest to TCP on the web that web sites can access, direct access to TCP sockets is not possible from web browsers.


Emscripten provides a passthrough API for accessing the WebSockets API from C/C++ code. This is useful for developers who would prefer not to write any JavaScript code, or deal with the C/C++ and JavaScript language interop. See the system include file <emscripten/websocket.h> for details. One benefit that the Emscripten WebSockets API provides over manual WebSockets access in JavaScript is the ability to share access to a WebSocket handle across multiple threads, something that can be time consuming to develop from scratch.


To target Emscripten WebSockets API, you must link it in with a “-lwebsocket.js” linker directive.


Floh

unread,
Sep 9, 2020, 8:35:50 AM9/9/20
to emscripten-discuss
I have some old cross-platform "net client" code here:


This sets up a socket via the native socket APIs, or emscripten's socket wrapper API around WebSockets and allows to send and receive messages (in this specific case the messages must be strings separated by '\r', this is just our own "message separator", for binary data this whole message splitting and concatenating part would need to be rewritten, the reason for this is that on native TCP connections, messages might be split by the transport layer into several packages, which must be concatenated at the receiving side, and on the sending side it's probably also wise to split the messages into packets that are slightly smaller than common MTU sizes, somewhere between 1200 and 1500 bytes or so).

The gist (no pun intended) is basically that emscripten sockets behave like native sockets in non-blocking mode (this is unlike most socket example code, which often uses blocking at least for setting up the connection, and sometimes threads or fork for new connections).

There are also some minor differences because emscripten sockets "frame" each call to send() into a message (because WebSockets are message oriented, not stream oriented like native sockets).

On the other side you need some sort of WebSocket proxy. In our case we had a server with two ports open, one regular TCP port for native clients, and a second WebSocket port for web clients. Our server side code was written in Go and used the Gorilla websocket library: https://github.com/gorilla/websocket
Reply all
Reply to author
Forward
0 new messages