socket.io-client-pro2

1.0.21 • Public • Published

SocketClient

SocketClient is a powerful and flexible Node.js library for interacting with Socket.IO servers. It handles connections, disconnections, and events gracefully, providing advanced configuration options like proxy support, error handling, and SSL/TLS certificate management.

Table of Contents

Features

  • Support for Socket.IO versions v2, v3, and v4.
  • Automatic reconnections with configurable retries.
  • Proxy server support for both custom and environment-based proxies.
  • Easy integration of SSL/TLS certificates for secure connections.
  • Custom event listeners for handling socket events.
  • Built-in utilities for managing headers, query parameters, and cookies.

Installation

To install the SocketClient, use npm:

npm install socket.io-client-pro

Usage

Here’s a quick example of how to use the SocketClient:

const { SocketClient } = require('socket.io-client-pro');

const client = new SocketClient({
    target: {
        "project_id": "guest",
        "target_id": "1aaa0cc37a7009",
        "parent_id": "0",
        "target_type": "websocket",
        "name": "ws://localhost:8080/?query1=aaa",
        "sort": 2001,
        "version": 3,
        "mark_id": "1",
        "status": 1,
        "method": "Raw",
        "request": {
            "event": {
                "parameter": [
                    {
                        "description": "自定义 my-event",
                        "is_checked": 1,
                        "key": "custom-event",
                        "value": ""
                    },
                    {
                        "description": "自定义 my-even2",
                        "is_checked": 1,
                        "key": "my-even2",
                        "value": ""
                    }
                ]
            },
            "header": {
                "parameter": [
                    {
                        "description": "",
                        "field_type": "String",
                        "is_checked": 1,
                        "key": "connection",
                        "value": "{{aa}}",
                        "not_null": 1,
                        "param_id": "1aaaac74ba700a"
                    },
                    {
                        "description": "",
                        "field_type": "String",
                        "is_checked": 1,
                        "key": "bbb",
                        "value": "456",
                        "not_null": 1,
                        "param_id": "1aaaadcc3a700b"
                    }
                ]
            },
            "query": {
                "parameter": [
                    {
                        "description": "",
                        "field_type": "String",
                        "is_checked": 1,
                        "key": "query1",
                        "value": "1",
                        "not_null": 1,
                        "param_id": "1aaeb8687a7000"
                    },
                    {
                        "description": "",
                        "field_type": "String",
                        "is_checked": 1,
                        "key": "query2",
                        "value": "哈哈😂",
                        "not_null": 1,
                        "param_id": "1aaebb177a7001"
                    }
                ]
            },
        },
        "url": "http://cc.apipost.cc:6023",
        "config": {
            "information_size": 5,
            "reconnect_num": 0,
            "reconnect_time": 5000,
            "shake_hands_path": "/socket.io",
            "shake_hands_time_out": 0,
            "socket_event_name": "",
            "socketIo_version": "v3"
        },
        "created_at": "2024-12-06T11:38:52+08:00",
        "updated_at": "2024-12-06T11:56:16+08:00"
    },
    option: {
        "globals": {
            "aa": 11
        },
        "env": {
            "env_id": "1",
            "env_name": "默认环境",
            "env_pre_url": "",
            "env_pre_urls": {
                "1": {
                    "server_id": "1",
                    "name": "默认服务",
                    "sort": 1000,
                    "uri": ""
                },
                "default": {
                    "server_id": "1",
                    "name": "默认服务",
                    "sort": 1000,
                    "uri": ""
                }
            },
            "environment": {
                "aa": 22,
                "bb": 33
            }
        },
        "cookies": {
            "switch": 1,
            "data": []
        },
        "system_configs": {
            "send_timeout": 0,
            "auto_redirect": -1,
            "max_redirect_time": 5,
            "auto_gen_mock_url": 1,
            "request_param_auto_json": -1,
            "proxy": {
                "type": 2,
                "envfirst": 1,
                "bypass": [],
                "protocols": [
                    "http"
                ],
                "auth": {
                    "authenticate": -1,
                    "host": "",
                    "username": "",
                    "password": ""
                }
            },
            "ca_cert": {
                "open": -1,
                "path": "",
                "base64": "data:application/json;base64,ewogICAgImNsaWVudE5hbWUiOiAiVGh1bmRlciBDbGllbnQiLAogICAgImNvbGxlY3Rpb25OYW1lIjogIm1tbSIsCiAgICAiY29sbGVjdGlvbklkIjogImY1Zjc0NDBlLTYzMTAtNDcxYS04ZDlmLWYxODkxODUzZGY1NSIsCiAgICAiZGF0ZUV4cG9ydGVkIjogIjIwMjQtMTItMDVUMTc6NTQ6MTEuNjE0WiIsCiAgICAidmVyc2lvbiI6ICIxLjIiLAogICAgImZvbGRlcnMiOiBbXSwKICAgICJyZXF1ZXN0cyI6IFsKICAgICAgICB7CiAgICAgICAgICAgICJfaWQiOiAiNmIxMjgxNGQtNTRkNy00OThkLThlM2MtNTcwMzBkN2Y1ODE3IiwKICAgICAgICAgICAgImNvbElkIjogImY1Zjc0NDBlLTYzMTAtNDcxYS04ZDlmLWYxODkxODUzZGY1NSIsCiAgICAgICAgICAgICJjb250YWluZXJJZCI6ICIiLAogICAgICAgICAgICAibmFtZSI6ICJlY2hvIGFwaSIsCiAgICAgICAgICAgICJ1cmwiOiAiaHR0cHM6Ly93d3cudGh1bmRlcmNsaWVudC5jb20vd2VsY29tZSIsCiAgICAgICAgICAgICJtZXRob2QiOiAiR0VUIiwKICAgICAgICAgICAgInNvcnROdW0iOiAyMDAwMCwKICAgICAgICAgICAgImNyZWF0ZWQiOiAiMjAyNC0wOC0yMFQwNDowMToxMS4zNzNaIiwKICAgICAgICAgICAgIm1vZGlmaWVkIjogIjIwMjQtMDgtMjBUMDQ6MDE6MjkuMTA4WiIsCiAgICAgICAgICAgICJoZWFkZXJzIjogW10KICAgICAgICB9CiAgICBdLAogICAgInJlZiI6ICJNeUhjb1BocWYtVXFFMlpxQ2RSdHNpUFAtUzMzNDVoaDFaRE5tZ0Nscm82SG4zT05uZl8yMWtod2FBUHZHTTNoUERmb3VZLV9BbVhUN0VjOV9Vblh0USIKfQ=="
            },
            "client_cert": {}
        },
        "custom_functions": {}
    },
    callback: (response) => {
        console.log(response);
    }
});

(async () => {
    await client.connect();
    client.emitEvent('message', { text: 'Hello, world!' });
})();

Configuration Options

  • callback: A function that will be called with the response of socket operations.

Event Listeners

You can set up custom event listeners in the request.event:

event: {
    socketIoEventListeners: [
        { key: 'someEvent', is_checked: true },
        { key: 'anotherEvent', is_checked: true },
    ]
}

These events will trigger the callback function you provided during initialization.

Methods

  • connect(): Establish a connection to the Socket.IO server.
  • emitEvent(event, data, ackCallback): Emit an event to the server with optional acknowledgment callback.
  • disconnect(): Disconnect from the Socket.IO server.
  • awaitDisconnect(): Wait for the Socket.IO server to disconnect.

License

This project is licensed under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i socket.io-client-pro2

Weekly Downloads

1

Version

1.0.21

License

MIT

Unpacked Size

66.6 kB

Total Files

6

Last publish

Collaborators

  • proud_lion