Skip to content

A simple, easy-to-use, high-performance, and plug-in Socket solution.

License

Notifications You must be signed in to change notification settings

dp-os/socket.ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

012aa54 · Jan 13, 2025

History

68 Commits
Aug 8, 2024
Apr 30, 2023
Jan 13, 2025
Apr 22, 2023
Apr 23, 2023
Apr 22, 2023
Apr 23, 2023
Apr 22, 2023
Apr 26, 2023
Apr 27, 2023
Apr 26, 2023
Jan 13, 2025
Jan 13, 2025
Apr 23, 2023
Apr 23, 2023
Apr 26, 2023
Apr 26, 2023

Repository files navigation

socket.ts

A simple, easy-to-use, high-performance, and plug-in Socket solution.

Install

npm install socket.ts

Quick Start

import { Socket, SocketState } from 'socket.ts';

const socket = new Socket({
    url: 'ws://localhost:3000'
});

// Subscribe to Socket state changes
socket.subscribeState((state) => {
    console.log(state);
});

// Listen for server data push
socket.subscribeMessage((messageEvent) => {
    console.log(messageEvent);
});

// The event triggered after calling the transformMessage method to parse the data.
socket.subscribeData((data) => {
    console.log(data);
});

// Send data to the server
socket.send({ msg: 'I am ready.' });

socket.connect();

// Disconnect
// socket.disconnect();

// Dispose memory and remove all event listeners
// socket.dispose()

Options

export interface SocketOptions {
    /**
     * The URL of the Socket request.
     */
    url?: string;
    /**
     * The protocol of the Socket request.
     */
    protocols?: string | string[];
    /**
     * The interval between reconnection attempts after the connection is lost, in milliseconds. Default is: 1000 * 30.
     */
    retryInterval?: number;
    /**
     * The interval between sending heartbeats, in milliseconds. Default is: 1000 * 60.
     */
    pingInterval?: number;
    /**
     * The data to be sent with the heartbeat.
     */
    pingData?: any;
    /**
     * Create a bridge. The default bridge is WorkerSocketBridge.
     * @param socket The current Socket instance.
     * @returns 
     */
    createBridge: (socket: Socket) => SocketBridge;
    /**
     * After receiving data pushed from the server, you want to convert it into another format.
     * @param event
     * @returns Returns the converted data.
    */
    transformResponse?: (event: MessageEvent) => any
    /**
     * The plugins used.
     */
    plugins?: SocketPlugin[];
}

About

A simple, easy-to-use, high-performance, and plug-in Socket solution.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published