Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A web socket that reconnects.

License

NotificationsYou must be signed in to change notification settings

github/stable-socket

A web socket that reconnects.

Installation

$ npm install @github/stable-socket

Usage

import{StableSocket}from'@github/stable-socket'constdelegate={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.returncode!==1008}}constpolicy={timeout:4000,attempts:Infinity,maxDelay:60000}consturl='wss://live.example.com'constsocket=newStableSocket(url,delegate,policy)socket.open()

BufferedSocket

Writing to a StableSocket while it is in the opening or closed statesdiscards the message data. Use a BufferedSocket to buffer writes to besent when it opens.

import{BufferedSocket,StableSocket}from'@github/stable-socket'constsocket=newBufferedSocket(newStableSocket(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 thatmaintain an internal state machine, managing reconnects and preventing writesto closed sockets. However, sometimes we need direct access to an open WebSocketin async functions.

connect

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

import{connect}from'@github/stable-socket'try{constsocket=awaitconnect('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 connectionswith exponential backoff. Returns a Promise fulfilled with an open WebSocketor rejected with a connection failure.

import{connectWithRetry}from'@github/stable-socket'try{constpolicy={timeout:100,attempts:Infinity,maxDelay:60000}constsocket=awaitconnectWithRetry('wss://live.example.com',policy)socket.send('hi')}catch(e){console.log('Socket connection failed',e)}

Development

npm installnpm test

License

Distributed under the MIT license. See LICENSE for details.

About

A web socket that reconnects.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

[8]ページ先頭

©2009-2025 Movatter.jp