JSPM

@carbide/c4-websocket-client

1.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 9
    • Score
      100M100P100Q64851F
    • License MIT

    WebSocket 客户端封装组件,支持自动重连、心跳检测 - Day 1 核心组件 C4

    Package Exports

    • @carbide/c4-websocket-client
    • @carbide/c4-websocket-client/dist/index.cjs.js
    • @carbide/c4-websocket-client/dist/index.esm.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@carbide/c4-websocket-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    C4 WebSocketClient - WebSocket 客户端

    Day 1 核心组件 C4,支持自动重连、心跳检测、消息订阅。

    安装

    npm install @carbide/c4-websocket-client

    使用

    import { WebSocketClient } from '@carbide/c4-websocket-client';
    
    const ws = new WebSocketClient({
      url: 'wss://api.example.com/chat',
      autoReconnect: true,
      reconnectInterval: 3000,
      maxReconnectAttempts: 5,
      heartbeatInterval: 30000
    });
    
    // 连接
    ws.connect();
    
    // 接收消息
    const unsubscribe = ws.onMessage((data) => {
      console.log('Received:', data);
    });
    
    // 监听状态
    ws.onStatusChange((status) => {
      console.log('Status:', status); // connected | disconnected | reconnecting
    });
    
    // 发送消息
    ws.send({ type: 'chat', message: 'Hello' });
    
    // 关闭连接
    ws.close();

    API

    方法 说明
    connect() 连接 WebSocket
    send(data) 发送消息
    onMessage(handler) 订阅消息,返回取消订阅函数
    onStatusChange(handler) 订阅状态变化
    close() 关闭连接

    在项目中使用

    详见 day-01/02-chat-bot 等项目。