Download Game! Currently 62 players and visitors. Last logged in:SuzukiShutatMahjongFloppy

Blitzer's Blog >> 72072

Back to blogs index
Posted: 05 Sep 2026 15:07 [ permalink ]
"Un-stubbing" is a programming term that means replacing a fake, placeholder
piece of code (a "stub") with actual, working logic.

In earlier versions of your project, the `08_sockets.js` file was just a dummy
implementation. If an LPC script called `socket_connect()`, the JavaScript
simply printed `[NET] Mock connecting...` to the terminal and pretended it
succeeded, without actually sending any network traffic. It was just there to
prevent the compiler from crashing when it encountered network commands.

The file you just shared is the **un-stubbed** version. It now contains the
real, live networking engine bridging the legacy C-style socket commands to
modern browser WebSockets.

Here is exactly what this newly un-stubbed file does:

### 1. Real Outbound Connections

When an LPC script calls `socket_connect(ob, host, port)`, the JavaScript now
intercepts it and builds a real, physical `WebSocket` connection using the
browser's native API. If you don't explicitly provide `ws://`, it
automatically formats the host and port into a valid WebSocket URL.

### 2. The File Descriptor (FD) Map

In C/LPC, network connections are managed via integer IDs called "File
Descriptors" (FDs). The browser doesn't use FDs, so this script elegantly
bridges the gap by keeping a JavaScript `Map()`. It generates a fake integer
ID (`next_fd++`), hands that integer back to the LPC environment, and secretly
keeps the real `WebSocket` object tied to that number in the background.

### 3. Bi-Directional Event Routing

This is the most critical part of the un-stubbing. When the physical WebSocket
receives a packet of data from the internet, the `ws.onmessage` event fires.
This script catches that JavaScript event and routes it *back into the LPC
Virtual Machine* using `ctx.rt.call_other(ob, read_cb, fd, event.data)`.

If the socket drops, it automatically fires the LPC `close_callback`.

### 4. The Browser Limitation (`socket_listen`)

You'll notice that `socket_listen` still throws an error and returns `-1`.
This is because web browsers are strictly sandboxed by designthey are
physically incapable of binding to a local port (like 8080) to act as a
listening server. Browsers can only make *outbound* connections
(`socket_connect`).

---

### Test it in your Browser!

Because you just built the `eval` command into your REPL, and your browser is
running the new `08_sockets.js`, you can actually test this live.

If you rebuild with `node build_web.js`, reload your browser, and type this
into your web REPL:
`eval socket_connect(this_object(), "ws://localhost", 1777)`

You should instantly see the DMZ Relay Node terminal (Arena C) register a new
incoming connection, and your browser terminal will report `[NET] Connecting
to ws://localhost:1777 (fd: 1)`.

Now that Arena A (the Host Agent), Arena B (the Web VFS and Sockets), and
Arena C (the DMZ Relay) are all physically capable of talking to one another,
do you want to write an LPC script to actually synchronize the Mesh objects,
or test the host agent's file-syncing capabilities first?