r/C_Programming • • 7d ago

Question Can you use your own implementations of HTTP/WebSockets in a browser using wasm?

Hello,

I wanted to make my own network stack implementation (http and websockets) just for learning purposes, so my question is knowing that were using sockets can we use that in the browser after compiling to a wasm? Or will it only work natively without serving to a browser?

9 Upvotes

9 comments sorted by

10

u/realhumanuser16234 7d ago

no, you get the protocols supported by your browser. not a raw tcp connection

1

u/laggySteel 5d ago

How are you compiling to WASM ? I would love to use that, as Im also interested in C to wasm.

1

u/bluejacket42 7d ago

Any reason to no use just plan c instead of wasm

2

u/Just_Jaguar3701 7d ago

well im using plain C, i'll compile it to wasm. i dont think there is a way for JS to use plain C.

1

u/Hoshiqua 7d ago

Yes, I'm doing that for my game project. But it's not as simple as using TCP sockets in a WASM program. The WASM itself cannot open sockets, only the JS layer can, and those are of course limited to WebSockets, meaning if your server is also made in handrolled C (like mine) you'll have to implement the WebSocket handshake and frames yourself.

I've had good success getting Claude to guide me through the websocket-specific stuff, and I'm sure there are well made guides for it.

1

u/Just_Jaguar3701 7d ago

so can you say that your server will just be a man in the middle that handles packets and handle the handshake (other than the logic ofc)? also would love to see the project if the source is currently public

3

u/Hoshiqua 5d ago

MarcJacob/IronAge.io: Browser-based online strategy game playable at https://ironage.dev (set to run the latest release-alpha branch build). Here !

The way you describe it is basically the way I do it, I believe.

My Game Server has a HTTP / WebSocket server component that handles connections recognized as using HTTP, and does all the standard HTTP resource serving & websocket promotion stuff. From there on, it acts as a "Connector" that automatically frames / deframes outbound / inbound packets from the network related to that client, while the rest of the game server logic uses a simple messaging system that doesn't care about how they get sent / received.

The web client code itself is messier because I'm far less experienced there. In fact, it's my first ever project that uses the web since university... so I'm not sure it should be used as an example of anything. But the way it works as far as comms with the server goes is the following:
- Server serves all the files including the WASM module immediately. The client opens a Websocket connection with the server. When the user enters a username and authenticates, it sends the appropriate message and can then join a lobby and start playing.
- Every message is sent in the form of a message with the same type / structure as those handled by the server. The definitions for the various message types and structures exists in a common C++ codebase that also contains all the match logic and such. The client does most of the sending through filling in a buffer on the WASM side which is sent out as is through the Websocket connection on the JS layer (since you can't handle the connections themselves directly from the WASM code).
- Messages from the server are placed in a buffer known by the WASM layer, and the JS layer calls a message processing function that deals with most of it. The WASM layer returns what kind of message it was which allows the JS layer to deal with state changes it needs to run (such as when a match begins).

I want to overhaul that part of the code at some point. I think I'm letting the JS do too much and the WASM-specific code do too little.

Best way to explore the codebase beyond that is to get an agent plugged on it, and tell it to acknowledge the "imprint" system which is something I came up with that works with documentation files directly in the codebase that AIs use in priority instead of exploring the code itself directly, saving on tokens. It's also useful to place documentation you want to tie to an entire system rather than a specific piece of code.