I have some old cross-platform "net client" code here:

https://gist.github.com/floooh/b4d25d0e11ef2be91dd19cb974bcc7a6

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


On Tuesday, 8 September 2020 22:49:38 UTC+2, qrss qrss wrote:
>
> 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.
>
> https://emscripten.org/docs/porting/networking.html
>
> 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
>
> ###
>
> https://emscripten.org/docs/porting/networking.html
>
> 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.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/d0edf0fa-d1f4-44ef-a4e7-68fe93c1fbceo%40googlegroups.com.

Reply via email to