bfsocket.io
Advanced tools
Comparing version 0.1.9 to 0.2.0
@@ -7,2 +7,6 @@ /** | ||
export declare const isJSON: (j: string) => boolean; | ||
export * as Store from './session'; | ||
export declare const Store: { | ||
getItem: <T extends unknown>(key: string) => string | T | T[]; | ||
setItem: (key: string, value: string | number | Array<any> | Record<string | number, any>) => boolean; | ||
removeItem: (key: string) => boolean; | ||
}; |
@@ -0,0 +0,0 @@ /** |
156
lib/index.js
@@ -1,155 +0,1 @@ | ||
/* | ||
* @Author: Yoney Y (YuTianyuan) | ||
* @Date: 2021-12-18 13:18:21 | ||
* @Last Modified by: Yoneyy (y.tianyuan) | ||
* @Last Modified time: 2022-03-14 11:45:39 | ||
*/ | ||
import strdm from "strdm"; | ||
import { isJSON, Store } from './helpers'; | ||
const SESSION_KEY = 'bws:session'; | ||
class SocketIO { | ||
ws; | ||
options; | ||
uniqueid; // socket uniqueid | ||
session; // socket session | ||
reconnectcount; // number of reconnections | ||
remillisecond; // reconnection interval | ||
reconnectlimit; // max limit for reconnection | ||
lockreconnect = false; // lock the reconnect , avoid repeating heavy chains | ||
disablereconnect = false; // whether to enable reconnection | ||
timer = null; | ||
heart = null; | ||
events = {}; | ||
constructor(options) { | ||
this.options = options; | ||
this.heart = options.heart; | ||
this.reconnectcount = 0; | ||
this.remillisecond = options?.remillisecond ?? 1000; | ||
this.reconnectlimit = options?.reconnectlimit ?? 10; | ||
this.session = options.session ?? Store.getItem(SESSION_KEY); | ||
this.ws = new WebSocket(options.url, options.protocols); | ||
this.uniqueid = options.uniqueid ?? strdm(16, { numbers: Date.now() + '' }); | ||
this.session && Store.setItem(SESSION_KEY, this.session); | ||
this.overrides(); | ||
} | ||
overrides() { | ||
this.ws.onclose = (ev) => { | ||
this.reconnect(); | ||
this.onDistory(ev); | ||
}; | ||
this.ws.onerror = () => this.reconnect(); | ||
this.ws.onopen = (ev) => this.onConnected(ev); | ||
} | ||
/** | ||
* Send heart data. | ||
*/ | ||
ping() { | ||
clearInterval(this.timer); | ||
const { data, delay } = this.heart; | ||
this.emit('heart', data); | ||
this.timer = setInterval(() => this.emit('heart', this.heart?.data ?? {}), delay ?? 1000 * 10); | ||
} | ||
/** | ||
* Socket reconnect | ||
*/ | ||
reconnect() { | ||
if (this.reconnectcount >= this.reconnectlimit) | ||
return; | ||
if (this.lockreconnect || this.disablereconnect) | ||
return; | ||
this.reconnectcount++; | ||
this.lockreconnect = true; | ||
setTimeout(() => { | ||
this.ws = new WebSocket(this.options.url, this.options.protocols); | ||
this.overrides(); | ||
this.lockreconnect = false; | ||
}, this.remillisecond); | ||
} | ||
/** | ||
* Closed Socket connect and disable reconnect | ||
*/ | ||
close() { | ||
this.disablereconnect = true; | ||
this.ws.close(); | ||
} | ||
/** | ||
* Socket close event | ||
*/ | ||
onDistory(fn) { | ||
clearInterval(this.timer); | ||
typeof fn === 'function' | ||
&& fn | ||
&& fn(); | ||
} | ||
/** | ||
* Socket connecnted event | ||
* @param fn | ||
*/ | ||
onConnected(fn) { | ||
this.reconnectcount = 0; | ||
this.heart && this.ping(); | ||
(fn && typeof fn === 'function') && fn(); | ||
} | ||
/** | ||
* set heart data | ||
* @param data | ||
*/ | ||
setHeartData(data) { | ||
this.heart.data = data; | ||
} | ||
/** | ||
* Send Message | ||
* @param data | ||
* @returns | ||
*/ | ||
dispatch(data) { | ||
const type = Object.prototype.toString.call(data); | ||
if (type === '[object Object]' | ||
|| type === '[object String]' | ||
|| type === '[object Array]' | ||
|| type === '[object Number]' | ||
|| type === '[object Boolean]') | ||
return this.ws.send(JSON.stringify(data)); | ||
this.ws.send(data); | ||
} | ||
/** | ||
* Listen Socket message event | ||
* @param event | ||
* @param cb | ||
*/ | ||
on(event, cb) { | ||
if (this.events[event] == null) | ||
this.events[event] = cb; | ||
this.ws.onmessage = (e) => { | ||
const { cmd, session, ...rest } = JSON.parse(e.data); | ||
this.session = session; | ||
Store.setItem(SESSION_KEY, session); | ||
this.events?.[cmd]?.(rest); | ||
}; | ||
} | ||
/** | ||
* Emit event on socket server | ||
* @param event | ||
* @param data | ||
*/ | ||
emit(event, data) { | ||
const session = this.session | ||
&& typeof this.session === 'string' | ||
&& isJSON(this.session) | ||
? JSON.parse(this.session) | ||
: this.session; | ||
const patch = { | ||
cmd: event, | ||
session, | ||
uniqueid: this.uniqueid, | ||
...data | ||
}; | ||
if (this.ws.readyState === this.ws.CONNECTING) | ||
return this.ws.onopen = () => { | ||
this.ping(); | ||
this.dispatch(patch); | ||
}; | ||
this.dispatch(patch); | ||
} | ||
} | ||
export default SocketIO; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){return t=Object.assign||function(t){for(var e,n=1,s=arguments.length;n<s;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},t.apply(this,arguments)};const e="~!@#$%^*()_+-=[]{}|;:,./<>?",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";var s=function(t){try{return JSON.parse(t),!0}catch(t){return!1}},o=window.sessionStorage,i=function(t){if(null==t)throw new Error("param `key` must be required");var e=o.getItem(t+"");return e&&s(e)?JSON.parse(e):e},r=function(t,e){try{var n="string"!=typeof e?JSON.stringify(e):e;return o.setItem(t+"",n),!0}catch(t){return!1}},c=function(){function o(t){var s,o,c,l;this.lockreconnect=!1,this.disablereconnect=!1,this.timer=null,this.heart=null,this.events={},this.options=t,this.heart=t.heart,this.reconnectcount=0,this.remillisecond=null!==(s=null==t?void 0:t.remillisecond)&&void 0!==s?s:1e3,this.reconnectlimit=null!==(o=null==t?void 0:t.reconnectlimit)&&void 0!==o?o:10,this.session=null!==(c=t.session)&&void 0!==c?c:i("bws:session"),this.ws=new WebSocket(t.url,t.protocols),this.uniqueid=null!==(l=t.uniqueid)&&void 0!==l?l:function(t=6,s={}){let o=t,i="",r="";if("boolean"==typeof s&&!0===s)i+="0123456789"+n+e;else if("string"==typeof s)i=s;else{let t=s;!1!==t.numbers&&(i+="string"==typeof t.numbers?t.numbers:"0123456789"),!1!==t.strings&&(i+="string"==typeof t.strings?t.strings:n),t.symbols&&(i+="string"==typeof t.symbols?t.symbols:e)}for(;o>0;)o--,r+=i[Math.floor(Math.random()*i.length)];return r}(16,{numbers:Date.now()+""}),this.session&&r("bws:session",this.session),this.overrides()}return o.prototype.overrides=function(){var t=this;this.ws.onclose=function(e){t.reconnect(),t.onDistory(e)},this.ws.onerror=function(){return t.reconnect()},this.ws.onopen=function(e){return t.onConnected(e)}},o.prototype.ping=function(){var t=this;clearInterval(this.timer);var e=this.heart,n=e.data,s=e.delay;this.emit("heart",n),this.timer=setInterval((function(){var e,n;return t.emit("heart",null!==(n=null===(e=t.heart)||void 0===e?void 0:e.data)&&void 0!==n?n:{})}),null!=s?s:1e4)},o.prototype.reconnect=function(){var t=this;this.reconnectcount>=this.reconnectlimit||this.lockreconnect||this.disablereconnect||(this.reconnectcount++,this.lockreconnect=!0,setTimeout((function(){t.ws=new WebSocket(t.options.url,t.options.protocols),t.overrides(),t.lockreconnect=!1}),this.remillisecond))},o.prototype.close=function(){this.disablereconnect=!0,this.ws.close()},o.prototype.onDistory=function(t){clearInterval(this.timer),"function"==typeof t&&t&&t()},o.prototype.onConnected=function(t){this.reconnectcount=0,this.heart&&this.ping(),t&&"function"==typeof t&&t()},o.prototype.setHeartData=function(t){this.heart.data=t},o.prototype.dispatch=function(t){var e=Object.prototype.toString.call(t);if("[object Object]"===e||"[object String]"===e||"[object Array]"===e||"[object Number]"===e||"[object Boolean]"===e)return this.ws.send(JSON.stringify(t));this.ws.send(t)},o.prototype.on=function(t,e){var n=this;null==this.events[t]&&(this.events[t]=e),this.ws.onmessage=function(t){var e,s,o=JSON.parse(t.data),i=o.cmd,c=o.session,l=function(t,e){var n={};for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&e.indexOf(s)<0&&(n[s]=t[s]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(s=Object.getOwnPropertySymbols(t);o<s.length;o++)e.indexOf(s[o])<0&&Object.prototype.propertyIsEnumerable.call(t,s[o])&&(n[s[o]]=t[s[o]])}return n}(o,["cmd","session"]);n.session=c,r("bws:session",c),null===(s=null===(e=n.events)||void 0===e?void 0:e[i])||void 0===s||s.call(e,l)}},o.prototype.emit=function(e,n){var o=this,i=this.session&&"string"==typeof this.session&&s(this.session)?JSON.parse(this.session):this.session,r=t({cmd:e,session:i,uniqueid:this.uniqueid},n);if(this.ws.readyState===this.ws.CONNECTING)return this.ws.onopen=function(){o.ping(),o.dispatch(r)};this.dispatch(r)},o}();exports.default=c; |
{ | ||
"name": "bfsocket.io", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"description": "bfsocket.io is WebSocket client", | ||
"main": "./lib/index.js", | ||
"module": "lib/index.js", | ||
"module": "lib/index.esm.js", | ||
"types": "lib/index.d.ts", | ||
@@ -29,4 +29,5 @@ "author": "YoneyY <yoneyy@163.com>", | ||
"scripts": { | ||
"dev": "tsc -p tsconfig.json -w", | ||
"build": "tsc -p tsconfig.json", | ||
"dev": "cross-env NODE_ENV=development yarn rollup -c -w --bundleConfigAsCjs", | ||
"build:dev": "cross-env NODE_ENV=development yarn rollup -c --bundleConfigAsCjs", | ||
"build": "cross-env NODE_ENV=production yarn rollup -c --bundleConfigAsCjs", | ||
"prepublishOnly": "yarn build" | ||
@@ -38,5 +39,12 @@ }, | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^23.0.2", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"@types/node": "^17.0.21", | ||
"cross-env": "^7.0.3", | ||
"rollup": "^3.2.5", | ||
"rollup-plugin-delete": "^2.0.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup-plugin-typescript2": "^0.34.1", | ||
"typescript": "^4.6.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15227
9
6
171
1