This page demonstrates practical network programming in OwnLang through concrete examples that showcase HTTP client communication and real-time socket programming. The examples illustrate integration with external APIs, asynchronous request handling, and bidirectional socket communication patterns.
For information about the underlying network module implementations, see Network and HTTP Modules. For server-side web programming, see Server Module.
The GitHub timeline example demonstrates HTTP client capabilities using the http
module to consume REST APIs. This example fetches public GitHub events and processes the JSON response data.
The GitHub timeline fetcher shows both synchronous and asynchronous HTTP request patterns:
The example demonstrates the thread()
function for non-blocking HTTP requests examples/network/github_timeline.own14-16 The callback function receives the response data and processes it using jsondecode()
to parse JSON.
Sources: examples/network/github_timeline.own
The GitHub example showcases comprehensive JSON data processing with pattern matching for different event types:
Event Type | Handler Pattern | Response Data |
---|---|---|
CommitCommentEvent | Extract comment body | payload.comment.body |
CreateEvent | Repository creation | payload.ref_type |
PushEvent | Commit count | length(payload.commits) |
IssuesEvent | Issue actions | payload.action , payload.issue.title |
The github_event_type()
function examples/network/github_timeline.own25-40 uses pattern matching to handle different GitHub webhook event structures, demonstrating robust API response processing.
Sources: examples/network/github_timeline.own
The pipes online game demonstrates Socket.io client integration for real-time multiplayer communication. The socket connection enables bidirectional event-based messaging between clients.
The socket client initialization examples/game/pipes_online.own89-110 establishes connection and registers event handlers for game state synchronization.
Sources: examples/game/pipes_online.own
The networking layer synchronizes game state between multiple clients through structured event messaging:
Direction | Event Name | Data Structure | Purpose |
---|---|---|---|
Server → Client | gameStart | data[0] (board array) | Initialize game board |
Server → Client | updateGhostCell | {x: int, y: int} | Sync opponent moves |
Server → Client | updateGhostCursor | {x: int, y: int} | Track opponent cursor |
Client → Server | updateCursor | {x: curX, y: curY} | Broadcast cursor position |
Client → Server | switchCell | {x: curX, y: curY} | Send game action |
The game loop processes input and emits corresponding socket events examples/game/pipes_online.own117-132 demonstrating real-time input synchronization patterns.
Sources: examples/game/pipes_online.own
OwnLang provides multiple approaches to handle network operations without blocking the main thread:
The thread()
function enables asynchronous HTTP requests while maintaining callback-based response handling examples/network/github_timeline.own14
Sources: examples/network/github_timeline.own
Network programming requires proper connection lifecycle management:
newSocket(url)
followed by socket.connect()
.on(event, handler)
calls before connectionsocket.disconnect()
in cleanup codeThe pipes game properly disconnects the socket when exiting the game loop examples/game/pipes_online.own173
Sources: examples/game/pipes_online.own
Network examples demonstrate integration between multiple OwnLang modules:
Module | Usage Pattern | Example |
---|---|---|
http | API consumption | http(url, callback) |
json | Response parsing | jsondecode(response) |
socket | Real-time communication | newSocket(url).on(event, handler) |
std | Threading and utilities | thread(function, args...) |
functional | Data processing | foreach(array, processor) |
These examples showcase how OwnLang's modular architecture enables comprehensive network programming capabilities through module composition.
Sources: examples/network/github_timeline.own
, examples/game/pipes_online.own
Refresh this wiki