C2 WebSocket Handler
Core WebSocket server for the C2 (Command & Control) system. Provides a JSON message bus on localhost for external tools to communicate with the game engine.
Core WebSocket server for the C2 (Command & Control) system. Provides a JSON message bus on localhost for external tools to communicate with the game engine.
Overview
Creates a WebSocket server on port 8088 that receives JSON messages, supports ping/pong keepalive, dynamic extension loading, and routes all other messages to registered plugins via the onC2WebSocketHandlerMessage hook.
Public API
| Function | Args | Returns | Description |
|---|---|---|---|
M.onExtensionLoaded | - | - | Creates WebSocket server on 127.0.0.1:8088 |
M.onExtensionUnloaded | - | - | Destroys WebSocket server and cleans up |
M.onUpdate | dt | - | Polls server events, decodes JSON, routes messages |
Built-in Message Types
| Message Type | Fields | Description |
|---|---|---|
loadExtension | extensionName | Dynamically loads a GE extension by name |
ping (raw) | - | Responds with "pong" (keepalive) |
All other JSON messages are broadcast via:
extensions.hook("onC2WebSocketHandlerMessage", {
event = evt,
message = jsonData,
server = server
})Hook Interface
Plugins implement onC2WebSocketHandlerMessage(args) to receive messages:
-- args structure:
-- args.event - WebSocket event (has peerId, type, msg)
-- args.message - Decoded JSON table
-- args.server - Server object for sending responses
-- Example plugin handler (not part of this extension):
local function onC2WebSocketHandlerMessage(args)
local msg = args.message
if msg.type == "myCommand" then
args.server:sendData(args.event.peerId, jsonEncode({type = "response", data = "ok"}))
end
endServer Details
| Property | Value |
|---|---|
| Address | 127.0.0.1 |
| Port | 8088 |
| Protocol | WebSocket (JSON messages) |
| Library | BNGWebWSServer via utils/wsUtils |
Event Processing
-- Event types from server:getPeerEvents()
-- evt.type == "D" - Data message
-- evt.msg - Raw message string
-- evt.peerId - Unique peer identifier
-- Response pattern:
server:sendData(evt.peerId, jsonEncode({type = "info", msg = "..."}))Notes
- Server is localhost-only (
127.0.0.1) - not accessible from network - Invalid JSON messages are logged as errors
- Extension loading errors are caught with
pcalland reported to the client - The
wsUtils.createOrGetWSfunction reuses existing servers on the same port - Server updates (flush/receive) happen via
server:update()each frame
See Also
- C2 Tile Manager - Scene tile data plugin
- C2 Vehicle Manager - Vehicle streaming plugin
`suspensionFrequencyTester` - Suspension Natural Frequency Analysis
Debug extension that measures suspension natural frequencies on the player vehicle's front and rear axles. Displays an ImGui window with frequency spectrum analysis and category matching (Luxury, Spor
C2 Tile Manager
Spatial hash tile index for scene objects, forest items, decals, road markings, and AI graph nodes. Provides tile-based scene queries with debug visualization and WebSocket data export.