All Projects → zimv → Websocket Heartbeat Js

zimv / Websocket Heartbeat Js

Licence: mit
♥️ simple and useful

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Websocket Heartbeat Js

Wamp Proto
The Web Application Messaging Protocol
Stars: ✭ 405 (-15.8%)
Mutual labels:  websocket
Flask Socketio
Socket.IO integration for Flask applications.
Stars: ✭ 4,523 (+840.33%)
Mutual labels:  websocket
Aria2.js
JavaScript library for aria2, "The next generation download utility."
Stars: ✭ 471 (-2.08%)
Mutual labels:  websocket
Microwebsrv
A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
Stars: ✭ 420 (-12.68%)
Mutual labels:  websocket
Centrifuge
Real-time messaging library for Go with scalability in mind
Stars: ✭ 446 (-7.28%)
Mutual labels:  websocket
Primus
⚡ Primus, the creator god of the transformers & an abstraction layer for real-time to prevent module lock-in.
Stars: ✭ 4,302 (+794.39%)
Mutual labels:  websocket
Xchange Stream
XChange-stream is a Java library providing a simple and consistent streaming API for interacting with Bitcoin and other crypto currency exchanges via WebSocket protocol. It is build on top of of XChange library providing new interfaces for streaming API. User can subscribe for live updates via reactive streams of RxJava library.
Stars: ✭ 402 (-16.42%)
Mutual labels:  websocket
Telegraph
Secure Web Server for iOS, tvOS and macOS
Stars: ✭ 474 (-1.46%)
Mutual labels:  websocket
Webim
基于环信webim+layim的web网页即时通讯 layim H5移动版
Stars: ✭ 448 (-6.86%)
Mutual labels:  websocket
Goflyway
An encrypted HTTP server
Stars: ✭ 4,292 (+792.31%)
Mutual labels:  websocket
Websocket
The Hoa\Websocket library.
Stars: ✭ 421 (-12.47%)
Mutual labels:  websocket
Happychat
基于Netty实现的WebSocket聊天室,支持几万人同时在线聊天
Stars: ✭ 445 (-7.48%)
Mutual labels:  websocket
Socketio Client Ue4
socket.io client plugin for Unreal Engine 4
Stars: ✭ 451 (-6.24%)
Mutual labels:  websocket
Hydra
后端一站式微服务框架,提供API、web、websocket,RPC、任务调度、消息消费服务器
Stars: ✭ 407 (-15.38%)
Mutual labels:  websocket
Wslay
The WebSocket library in C
Stars: ✭ 472 (-1.87%)
Mutual labels:  websocket
Agileconfig
基于.NET Core开发的轻量级分布式配置中心 / .NET Core light configuration server
Stars: ✭ 403 (-16.22%)
Mutual labels:  websocket
Engine.io
socket.io
Stars: ✭ 4,329 (+800%)
Mutual labels:  websocket
Html
HTML Standard
Stars: ✭ 5,217 (+984.62%)
Mutual labels:  websocket
Skyeye
智能办公OA系统[SpringBoot2-快速开发平台],适用于医院,学校,中小型企业等机构的管理。Activiti5.22+动态表单实现零java代码即可做到复杂业务的流程实施,同时包含文件在线操作、日志、考勤、CRM、ERP进销存、项目、拖拽式生成问卷、日程、笔记、计划、行政等多种复杂业务功能。同时,可进行授权二开。
Stars: ✭ 472 (-1.87%)
Mutual labels:  websocket
Iogrid
Multiplayer game engine/framework built using SocketCluster and Phaser
Stars: ✭ 455 (-5.41%)
Mutual labels:  websocket

Build Status DeepScan Grade

websocket-heartbeat-js (中文版README)


Introduction

The websocket-heartbeat-js is base on WebSocket of browser javascript, whose main purpose is to ensure web client and server connection, and it has a mechanism of heartbeat detection and automatic reconnection. When client device has network outage or server error which causes websocket to disconnect, the program will automatically reconnect until reconnecting is successful again.

Why

When we use the native websocket, if network disconnects, any event function not be executed. So front-end program doesn't know that websocket was disconnected. But if program is now executing ***WebSocket.send()***, browser must discover that message signal is failed, so the onclose function will execute.

Back-end websocket service is likely to happen error, when websocket disconnected that front-end not notice message received. So need to send ping message by timeout. Server return pong message to client when server received ping message. Because received pong message, client know connection normal. If client not received pong message, it is connection abnormal, client will reconnect.

In summary, for solve above two problems. Client should initiative send ping message for check connect status.

How

1.close websocket connection

If websocket need to disconnect, client must execute ***WebsocketHeartbeatJs.close()***. If server wants to disconnect, it should send a close message to client. When client received close message that it to execute ***WebsocketHeartbeatJs.close()***.

Example:

websocketHeartbeatJs.onmessage = (e) => {
    if(e.data == 'close') websocketHeartbeatJs.close();
}

2.ping & pong

Server should to return pong message when the client sends a ping message. Pong message can be of any value. websocket-heartbeat-js will not handle pong message, instead it will only reset heartbeat after receiving any message, as receiving any message means that the connection is normal.

Usage

install

npm install websocket-heartbeat-js

import

import WebsocketHeartbeatJs from 'websocket-heartbeat-js';
let websocketHeartbeatJs = new WebsocketHeartbeatJs({
    url: 'ws://xxxxxxx'
});
websocketHeartbeatJs.onopen = function () {
    console.log('connect success');
    websocketHeartbeatJs.send('hello server');
}
websocketHeartbeatJs.onmessage = function (e) {
    console.log(`onmessage: ${e.data}`);
}
websocketHeartbeatJs.onreconnect = function () {
    console.log('reconnecting...');
}

use script

<script src="./node_modules/websocket-heartbeat-js/dist/index.js"></script>
let websocketHeartbeatJs = new window.WebsocketHeartbeatJs({
    url: 'ws://xxxxxxx'
});

API

websocketHeartbeatJs.ws (WebSocket)

This websocketHeartbeatJs.ws is native Websocket instance. If you need more native Websocket features, operate the websocketHeartbeatJs.ws.

websocketHeartbeatJs.ws == WebSocket(websocketHeartbeatJs.opts.url);

websocketHeartbeatJs.opts (Object)

Attribute required type default description
url true string none websocket service address
pingTimeout false number 15000 A heartbeat is sent every 15 seconds. If any backend message is received, the timer will reset
pongTimeout false number 10000 After the Ping message is sent, the connection will be disconnected without receiving the backend message within 10 seconds
reconnectTimeout false number 2000 The interval of reconnection
pingMsg false string "heartbeat" Ping message value
repeatLimit false number null The trial times of reconnection。default: unlimited
const options = {
    url: 'ws://xxxx',
    pingTimeout: 15000, 
    pongTimeout: 10000, 
    reconnectTimeout: 2000,
    pingMsg: "heartbeat"
}
let websocketHeartbeatJs = new WebsocketHeartbeatJs(options);

websocketHeartbeatJs.send(msg) (function)

Send the message to the back-end service

websocketHeartbeatJs.send('hello server');

websocketHeartbeatJs.close() (function)

The front end manually disconnects the websocket connection. This method does not trigger reconnection.

hook function and event function

websocketHeartbeatJs.onclose (function)

websocketHeartbeatJs.onclose = () => {
    console.log('connect close');
}

websocketHeartbeatJs.onerror (function)

websocketHeartbeatJs.onerror = () => {
    console.log('connect onerror');
}

websocketHeartbeatJs.onopen (function)

websocketHeartbeatJs.onopen = () => {
    console.log('open success');
}

websocketHeartbeatJs.onmessage (function)

websocketHeartbeatJs.onmessage = (e) => {
    console.log('msg:', e.data);
}

websocketHeartbeatJs.onreconnect (function)

websocketHeartbeatJs.onreconnect = (e) => {
    console.log('reconnecting...');
}

demo

demo show

blog

初探和实现websocket心跳重连

similar package

websocket-heartbeat-miniprogram

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].