Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
websocket-front
Advanced tools
前端对原生 WebSocket 的封装,默认支持自动重连,支持心跳检测
typescript + rollup
yarn add websocket-client
# 或
npm install websocket-client
import WebsocketClient from 'websocket-client';
const socket = new WebsocketClient('ws://127.0.0.1:3001')
import WebsocketClient from 'websocket-client';
const socket = new WebsocketClient('ws://127.0.0.1:3001', {
// 重连
isReconnect: true,
reconnectTimeout: 300,
reconnectRepeat: 5,
// 心跳检测
isHeartbeat: true
pingMsg: JSON.stringify({
messageType: "0",
content: "ping",
}),
pingTimeout: 300,
pongTimeout: 300,
// 回调
onMessage: receiveMessage,
onError: errorCallback,
close: closeCallback,
})
const receiveMessage = (message: Event) => {
console.log('接收到的消息:', message)
}
const errorCallback = (error: Event) => {
console.log('错误信息', event)
}
const closeCallback = (event: Event) => {
console.log('关闭了')
}
import WebsocketClient from 'websocket-client';
const socket = new WebsocketClient('ws://127.0.0.1:3001')
// 执行某些操作
socket.sendMessage('发送WebSocket消息');
// 执行某些操作
socket.close()
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
url | string | websocket 地址 |
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
isReconnect | boolean | 是否需要自动重连 | true |
reconnectTimeout | number | 自动重连间隔时间 | 300ms |
reconnectRepeat | number | 尝试自动重连的次数 | Infinity |
isHeartbeat | boolean | 是否支持心跳检测 | 是 |
pingMsg | string | 心跳检测发送给后端的消息 | 'ping' |
pingTimeout | number | 发送心跳检测消息的延迟时间 | 30s |
pongTimeout | number | 多长时间没有收到返回的心跳就重启 | 300ms |
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
onMessage | (event:Event) => void | 收到后端返回的消息后的回调 | |
onError | (error: Event) => void | 收到错误信息后的回调 | |
close | (event: Event) => void | WebSocket 关闭了的回调 |
事件名称 | 说明 | 回调函数 |
---|---|---|
sendMessage | 向后端发送消息 | |
close | 关闭 WebSocket |
FAQs
WebSocket client for browser with reconnect and heartbeat
The npm package websocket-front receives a total of 7 weekly downloads. As such, websocket-front popularity was classified as not popular.
We found that websocket-front demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.