game-networking
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesegame-networking
游戏网络模块
Purpose
用途
This skill allows the AI to implement multiplayer game networking, managing protocols (e.g., UDP, TCP), minimizing latency, and ensuring game state synchronization for seamless player interactions.
该技能可让AI实现多人游戏的网络功能,管理UDP、TCP等协议,降低延迟,并确保游戏状态同步,以实现流畅的玩家交互。
When to Use
适用场景
Use this skill when building multiplayer games that require real-time communication, such as online shooters, MMOs, or cooperative adventures. Apply it for scenarios involving player synchronization, server-client architectures, or handling network interruptions in game loops.
当开发需要实时通信的多人游戏(如在线射击游戏、大型多人在线游戏MMO或合作冒险游戏)时,可使用该技能。适用于玩家同步、服务器-客户端架构或在游戏循环中处理网络中断等场景。
Key Capabilities
核心功能
- Handles UDP for low-latency, unreliable data transmission and TCP for reliable, ordered delivery.
- Implements latency compensation via interpolation and prediction algorithms to smooth gameplay.
- Synchronizes game states using techniques like delta encoding and authoritative servers.
- Supports NAT traversal and peer-to-peer connections for reduced server dependency.
- Manages bandwidth optimization through packet compression and prioritization.
- 支持UDP(低延迟、不可靠数据传输)和TCP(可靠、有序传输)协议
- 通过插值与预测算法实现延迟补偿,提升游戏流畅度
- 采用增量编码、权威服务器等技术实现游戏状态同步
- 支持NAT穿透与点对点(P2P)连接,降低对服务器的依赖
- 通过数据包压缩与优先级处理实现带宽优化
Usage Patterns
使用模式
To use this skill, first initialize a network context, then establish connections, send/receive data, and handle disconnections. Always wrap network operations in try-catch blocks for robustness. For example, in a game loop, check for incoming packets every frame and update game state accordingly. Use asynchronous patterns to avoid blocking the main thread. If authentication is needed, set the environment variable before invoking commands.
$GAME_NETWORKING_API_KEY使用该技能时,需先初始化网络上下文,然后建立连接、收发数据并处理断开连接的情况。为保证稳定性,务必将网络操作包裹在try-catch块中。例如,在游戏循环中,每帧检查 incoming 数据包并相应更新游戏状态。采用异步模式以避免阻塞主线程。若需要身份验证,需在调用命令前设置环境变量。
$GAME_NETWORKING_API_KEYCommon Commands/API
常用命令与API
Interact via OpenClaw CLI or REST API. CLI commands require the skill to be activated first with .
openclaw activate game-networking-
CLI Commands:
- Initialize a network session:
openclaw game-networking init --protocol udp --port 5000 --key $GAME_NETWORKING_API_KEY- Example: Sets up a UDP server on port 5000, requiring an API key.
- Send a packet:
openclaw game-networking send --host 192.168.1.1 --data '{"player_pos": [10, 20]}'- Flags: for target IP,
--hostfor JSON payload.--data
- Flags:
- Receive packets:
openclaw game-networking listen --timeout 5- Listens for incoming data with a 5-second timeout.
- Initialize a network session:
-
API Endpoints:
- POST /api/network/connect: Establishes a connection; body:
{"protocol": "udp", "host": "localhost", "port": 5000}- Requires header:
Authorization: Bearer $GAME_NETWORKING_API_KEY
- Requires header:
- GET /api/network/status: Retrieves current latency and connection stats; query param:
?sessionID=123 - PUT /api/network/sync: Sends game state updates; body:
{"state": {"players": [{"id": 1, "pos": [5, 10]}]}}
- POST /api/network/connect: Establishes a connection; body:
-
Code Snippets:
- Initialize and connect in Python:
python
import openclaw api_key = os.environ.get('GAME_NETWORKING_API_KEY') session = openclaw.game_networking.init(protocol='udp', port=5000, key=api_key) session.connect(host='192.168.1.1') - Send data:
python
data = {'player_pos': [10, 20]} openclaw.game_networking.send(session, data)
- Initialize and connect in Python:
-
Config Formats:
- Use JSON for configurations, e.g.:
json
{ "protocol": "tcp", "maxLatency": 100, "syncInterval": 50 }- Save as and pass via CLI:
network-config.jsonopenclaw game-networking init --config network-config.json
- Save as
- Use JSON for configurations, e.g.:
可通过OpenClaw CLI或REST API进行交互。使用CLI命令前需先通过激活该技能。
openclaw activate game-networking-
CLI命令:
- 初始化网络会话:
openclaw game-networking init --protocol udp --port 5000 --key $GAME_NETWORKING_API_KEY- 示例:在5000端口搭建UDP服务器,需提供API密钥。
- 发送数据包:
openclaw game-networking send --host 192.168.1.1 --data '{"player_pos": [10, 20]}'- 参数:指定目标IP,
--host传入JSON格式的负载数据。--data
- 参数:
- 接收数据包:
openclaw game-networking listen --timeout 5- 监听 incoming 数据,超时时间为5秒。
- 初始化网络会话:
-
API端点:
- POST /api/network/connect:建立连接;请求体:
{"protocol": "udp", "host": "localhost", "port": 5000}- 请求头需包含:
Authorization: Bearer $GAME_NETWORKING_API_KEY
- 请求头需包含:
- GET /api/network/status:获取当前延迟与连接统计数据;查询参数:
?sessionID=123 - PUT /api/network/sync:发送游戏状态更新;请求体:
{"state": {"players": [{"id": 1, "pos": [5, 10]}]}}
- POST /api/network/connect:建立连接;请求体:
-
代码片段:
- Python初始化与连接:
python
import openclaw api_key = os.environ.get('GAME_NETWORKING_API_KEY') session = openclaw.game_networking.init(protocol='udp', port=5000, key=api_key) session.connect(host='192.168.1.1') - 发送数据:
python
data = {'player_pos': [10, 20]} openclaw.game_networking.send(session, data)
- Python初始化与连接:
-
配置格式:
- 采用JSON格式配置,示例:
json
{ "protocol": "tcp", "maxLatency": 100, "syncInterval": 50 }- 保存为并通过CLI传入:
network-config.jsonopenclaw game-networking init --config network-config.json
- 保存为
- 采用JSON格式配置,示例:
Integration Notes
集成说明
Integrate this skill into game engines like Unity or Godot by importing the OpenClaw SDK and calling functions from your scripts. For Unity, add the OpenClaw package via NuGet and reference it in C# scripts. Always verify dependencies: ensure the skill is compatible with your game's architecture (e.g., client-server vs. P2P). If using with other OpenClaw skills, chain them; for example, use first to handle user logins, then pass the token to network commands via . Test integrations in a simulated environment to handle firewall issues.
game-auth$GAME_NETWORKING_API_KEY可通过导入OpenClaw SDK并在脚本中调用函数,将该技能集成到Unity、Godot等游戏引擎中。对于Unity,可通过NuGet添加OpenClaw包并在C#脚本中引用。务必验证依赖关系:确保该技能与游戏架构兼容(如客户端-服务器模式或P2P模式)。若与其他OpenClaw技能配合使用,可按顺序调用;例如,先使用处理用户登录,再通过将令牌传入网络命令。建议在模拟环境中测试集成,以处理防火墙相关问题。
game-auth$GAME_NETWORKING_API_KEYError Handling
错误处理
Anticipate common errors like connection timeouts, packet loss, or invalid API keys. Use try-except blocks for CLI/API calls; for example, catch for failed connections. Specific patterns:
NetworkError- If a command fails with "Timeout", retry up to 3 times with exponential backoff: .
openclaw game-networking send --retry 3 - For API errors, check HTTP status codes (e.g., 401 for unauthorized; respond by prompting for ).
$GAME_NETWORKING_API_KEY - In code, handle exceptions like this:
python
try: response = openclaw.game_networking.listen(timeout=5) except openclaw.NetworkTimeoutError as e: print(f"Timeout occurred: {e}. Retrying...") # Implement retry logic here
Log errors with timestamps and include debug flags in commands, e.g., to output detailed traces.
openclaw game-networking init --debug需提前处理连接超时、数据包丢失或API密钥无效等常见错误。对CLI/API调用使用try-except块;例如,捕获以处理连接失败。具体处理方式:
NetworkError- 若命令因“Timeout”失败,最多重试3次并采用指数退避策略:。
openclaw game-networking send --retry 3 - 对于API错误,检查HTTP状态码(如401表示未授权;需提示用户设置)。
$GAME_NETWORKING_API_KEY - 代码中可按如下方式处理异常:
python
try: response = openclaw.game_networking.listen(timeout=5) except openclaw.NetworkTimeoutError as e: print(f"Timeout occurred: {e}. Retrying...") # Implement retry logic here
记录错误时需包含时间戳,在命令中加入调试参数(如)以输出详细追踪信息。
openclaw game-networking init --debugUsage Examples
使用示例
- Set up a multiplayer lobby: Use this to create a simple server for player connections. First, run . Then, in your game code, connect clients with: ```python import openclaw session = openclaw.game_networking.connect(host='localhost', port=8080) session.send({'action': 'join_lobby', 'player_name': 'User1'})
openclaw game-networking init --protocol tcp --port 8080 --key $GAME_NETWORKING_API_KEY
This
2. **Synchronize player positions in a real-time game:** Initialize with `openclaw game-networking init --protocol udp`. In the game loop, send updates: ```python
while game_running:
pos = get_player_position()
openclaw.game_networking.send(session, {'player_pos': pos})
incoming = openclaw.game_networking.receive()
if incoming: update_game_state(incoming)
``` This ensures low-latency synchronization, compensating for delays by interpolating positions.- 搭建多人游戏大厅: 可使用该技能创建一个供玩家连接的简单服务器。首先运行。然后在游戏代码中按如下方式连接客户端:```python import openclaw session = openclaw.game_networking.connect(host='localhost', port=8080) session.send({'action': 'join_lobby', 'player_name': 'User1'})
openclaw game-networking init --protocol tcp --port 8080 --key $GAME_NETWORKING_API_KEY
2. **实时游戏中同步玩家位置:** 先通过`openclaw game-networking init --protocol udp`初始化。在游戏循环中发送位置更新:```python
while game_running:
pos = get_player_position()
openclaw.game_networking.send(session, {'player_pos': pos})
incoming = openclaw.game_networking.receive()
if incoming: update_game_state(incoming)
``` 该实现可确保低延迟同步,通过位置插值补偿延迟。Graph Relationships
关联关系
- Depends on: authentication (for secure connections), game-physics (for synchronized simulations)
- Related to: game-dev cluster skills like game-ai (for intelligent network behaviors), data-storage (for persisting game states)
- Conflicts with: none directly, but avoid overlapping with low-level socket implementations
- 依赖:身份验证(用于安全连接)、游戏物理(用于同步模拟)
- 关联:游戏开发集群技能,如game-ai(用于智能网络行为)、data-storage(用于持久化游戏状态)
- 冲突:无直接冲突,但应避免与底层套接字实现重叠