lukeed/sockette được TopGit xếp vào nhóm dự án mã nguồn mở, với 2.5k sao trên GitHub, viết chủ yếu bằng JavaScript. The cutest little WebSocket wrapper! 🧦
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Sockette is a tiny (367 bytes) wrapper around WebSocket that will automatically reconnect if the connection is lost!
In addition to attaching additional API methods, Sockette allows you to reuse instances, avoiding the need to redeclare all event listeners.
You have direct access to the (current) underlying WebSocket within every EventListener callback (via event.target).
Install
$ npm install --save sockette
Usage
Unlike WebSocket, you should declare all event listeners on initialization:
const Sockette = require('sockette');
const ws = new Sockette('ws://localhost:3000', {
timeout: 5e3,
maxAttempts: 10,
onopen: e => console.log('Connected!', e),
onmessage: e => console.log('Received:', e),
onreconnect: e => console.log('Reconnecting...', e),
onmaximum: e => console.log('Stop Attempting!', e),
onclose: e => console.log('Closed!', e),
onerror: e => console.log('Error:', e)
});
ws.send('Hello, world!');
ws.json({type: 'ping'});
ws.close(); // graceful shutdown
// Reconnect 10s later
setTimeout(ws.reconnect, 10e3);
API
Sockette(url, options)
Returns: Sockette
Returns the Sockette instance.
url
Type: String
The URL you want to connect to — Should be prefixed with ws:// or wss://. This is passed directly to WebSocket.
options.protocols
Type: String|Array
Either a single protocol string or an array of strings used to indicate sub-protocols. See the WebSocket docs for more info.
options.timeout
Type: Number
Default: 1000
The amount of time (in ms) to wait in between reconnection attempts. Defaults to 1 second.
options.maxAttempts
Type: Number
Default: Infinity
The maximum number of attempts to reconnect.
Important: Pass -1 if you want to disable this feature. Although, this is main reason to use Sockette! :joy:
options.onopen
Type: Function
The EventListener to run in response to 'open' events. It receives the Event object as its only parameter.
This is called when the connection has been established and is ready to send and receive data.
Important: Sockette will forget the number of previous reconnection attempts, so that the next time connection is lost, you will consistently retry n number of times, as determined by options.maxAttempts.
options.onmessage
Type: Function
The EventListener to run in response to 'message' events. It receives the Event object as its only parameter.
This is called when a message has been received from the server. You'll probably want event.data!
options.onreconnect
Type: Function
The callback to run when attempting to reconnect to the server.
If Sockette is automatically reconnecting in response to an error or unexpected close event, then your onreconnect callback will receive the forwarded Event object.
options.onmaximum
Type: Function
The callback to run when the maxAttempts limit has been met.
This callback will receive the forwarded Event object from onclose.
options.onclose
Type: Function
The EventListener to run in response to 'close' events. It receives the Event object as its only parameter.
This is called when the connection has been closed for any reason.
Important: If the event.code is not1000, 1001, or 1005 an automatic reconnect attempt will be queued.
options.onerror
Type: Function
The EventListener to run in response to 'error' events. It receives the Event object as its only parameter.
This is called anytime an error occurs.
Important: If the event.code is ECONNREFUSED, an automatic reconnect attempt will be queued.
send(data)
Identical to WebSocket.send(), capable of sending multiple data types.
close(code, reason)
Identical to WebSocket.close().
Note: The code will default to 1000 unless specified.
json(obj)
Convenience method that passes your obj (Object) through JSON.stringify before passing it to WebSocket.send().
reconnect()
If options.maxAttempts has not been exceeded, enqueues a reconnection attempt. Otherwise, it runs your options.onmaximum callback.
open()
Initializes a new WebSocket — used on initialization and by reconnect().
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/lukeed/sockette là nguồn chính thức.
lukeed/sockette có bao nhiêu sao?
lukeed/sockette có 2.5k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/lukeed/sockette. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
lukeed/sockette có phải mã nguồn mở không?
Có — lukeed/sockette phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/lukeed/sockette.
lukeed/sockette còn đang phát triển không?
Commit gần nhất trên lukeed/sockette là 2.8 năm trước (theo timestamp GitHub). Repo có 79 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
lukeed/sockette dùng license gì?
lukeed/sockette phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
lukeed/sockette là gì?
lukeed/sockette (lukeed/sockette) là dự án JavaScript trên GitHub. Theo mô tả gốc: The cutest little WebSocket wrapper! 🧦
lukeed/sockette viết bằng ngôn ngữ gì?
lukeed/sockette chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về sockette?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về sockette.