createWS.js 525 B

1234567891011121314151617181920
  1. let createWS = (url, callbackObj) =>{
  2. var ws = new WebSocket(url)
  3. WebSocket.prototype.sendJson = function (obj) {
  4. this.send(JSON.stringify(obj))
  5. }
  6. ws.onopen = function () {
  7. callbackObj.open && callbackObj.open(ws)
  8. setInterval(function () {
  9. ws.send(JSON.stringify({
  10. type: "heartbeat"
  11. }))
  12. }, 10000);
  13. }
  14. ws.onclose = function () {
  15. callbackObj.onclose && callbackObj.onclose(ws)
  16. }
  17. return ws
  18. }
  19. export default createWS;