MIT License

项目介绍

github/stable-socket 是一个托管在 GitHub 上的开源项目,主要使用 TypeScript 编写,采用 MIT License 许可证,目前获得 138 个 Star、16 次 Fork。

仓库全名
github/stable-socket
开源许可证
MIT License
Star 数
138
Fork 数
16
未关闭 Issue
1

README 摘要

StableSocket A web socket that reconnects. Installation Usage BufferedSocket Writing to a StableSocket while it is in the opening or closed states discards the message data. Use a BufferedSocket to buffer writes to be sent when it opens. Asynchronous connections StableSocket and BufferedSocket are abstractions over a WebSocket that maintain an internal state machine, managing reconnects and preventing writes to…

完整内容见 GitHub 原始 README 或本页 。

常见问题

stable-socket 用什么语言开发?

主要使用 TypeScript 编写,采用 MIT License 许可证,目前有 138 个 Star。

stable-socket 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 stable-socket 的源码?

可以直接克隆仓库:git clone https://github.com/github/stable-socket.git;安装与运行步骤见仓库 README。

stable-socket 最近还在维护吗?

仓库最近一次更新为 6月3日,当前有 1 个未关闭 Issue;Star 数为 138,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

A web socket that reconnects.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
308
价值分
101
活跃状态
活跃

语言占比

JavaScript
Shell
TypeScript

Release

README

StableSocket

A web socket that reconnects.

Installation

$ npm install @github/stable-socket

Usage

import {StableSocket} from '@github/stable-socket'

const delegate = {
  socketDidOpen(socket: Socket) {
    // Socket is ready to write.
    socket.send('Hello')
  },
  socketDidClose(socket: Socket, code?: number, reason?: string) {
    // Socket closed and will retry the connection.
  },
  socketDidFinish(socket: Socket) {
    // Socket closed for good and will not retry.
  },
  socketDidReceiveMessage(socket: Socket, message: string) {
    // Socket read data from the connection.
  },
  socketShouldRetry(socket: Socket, code: number): boolean {
    // Socket reconnects unless server returns the policy violation code.
    return code !== 1008
  }
}

const policy = {
  timeout: 4000,
  attempts: Infinity,
  maxDelay: 60000
}

const url = 'wss://live.example.com'
const socket = new StableSocket(url , delegate, policy)
socket.open()

BufferedSocket

Writing to a StableSocket while it is in the opening or closed states discards the message data. Use a BufferedSocket to buffer writes to be sent when it opens.

import {BufferedSocket, StableSocket} from '@github/stable-socket'
const socket = new BufferedSocket(new StableSocket(url, delegate, policy))
socket.open()
socket.send('hello') // Will be sent when the socket is open.

Asynchronous connections

StableSocket and BufferedSocket are abstractions over a WebSocket that maintain an internal state machine, managing reconnects and preventing writes to closed sockets. However, sometimes we need direct access to an open WebSocket in async functions.

connect

Asynchronously connects to a web socket port or fails after a timeout. The socket is open, and writable with send, when its promise is fulfilled. Returns a Promise fulfilled with an open WebSocket or rejected with a connection failure.

import {connect} from '@github/stable-socket'

try {
  const socket = await connect('wss://live.example.com', 100)
  socket.send('hi')
} catch (e) {
  console.log('Socket connection failed', e)
}
connectWithRetry

Asynchronously connects to a web socket port, retrying failed connections with exponential backoff. Returns a Promise fulfilled with an open WebSocket or rejected with a connection failure.

import {connectWithRetry} from '@github/stable-socket'

try {
  const policy = {timeout: 100, attempts: Infinity, maxDelay: 60000}
  const socket = await connectWithRetry('wss://live.example.com', policy)
  socket.send('hi')
} catch (e) {
  console.log('Socket connection failed', e)
}

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.

评论

暂无评论