tank-websocket.js is a stable websocket client plugin.

feature

  • Supports multiple instances/single case, global singleton pattern in Single Page Application
  • Support commonjs, es6 import two import modes
  • Support automatic reconnection mechanism
  • Support for event listeners
  • 0 dependencies
  • Unit test high coverage
  • Support browser

install


npm install tank-websocket.js
// or
yarn add tank-websocket.js

import

commonjs


const TankWebSocket = require("tank-websocket.js");
//或
const {SocketClient, useSocketClient} = require("tank-websocket.js");

ES6+ import


import TankWebSocket from "tank-websocket.js";
//或
import {SocketClient, useSocketClient} from "tank-websocket.js";

html tag import


<script src="https://unpkg.com/tank-websocket.js/lib/">script>

initialization

Multiple instance creation


const twsc = new TankWebSocket.SocketClient('ws://127.0.0.1:19198');
twsc.onOpen((event) => {
    console.log("on open", event)
})

Singleton creation

Global singleton pattern in single page application


​//main.js 
import TankWebSocket from "tank-websocket.js";​​​​​​​

TankWebSocket.useSocketClient('ws://127.0.0.1:19198');

//其他文件 
import TankWebSocket from "tank-websocket.js";

TankWebSocket.useSocketClient().onOpen((event) => { 
      console.log("on open", event) 
})

#tankwebsocket.js #Homepage #Documentation #Downloads #WebSocket #Client #Plugin #News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *