tiows.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. if (typeof (tio) == "undefined") {
  2. tio = {};
  3. }
  4. tio.ws = {};
  5. /**
  6. * @param {*} ws_protocol wss or ws
  7. * @param {*} ip
  8. * @param {*} port
  9. * @param {*} paramStr 加在ws url后面的请求参数,形如:name=张三&id=12
  10. * @param {*} param 作为tio.ws对象的参数,由业务自己使用,框架不使用
  11. * @param {*} handler
  12. * @param {*} heartbeatTimeout 心跳时间 单位:毫秒
  13. * @param {*} reconnInterval 重连间隔时间 单位:毫秒
  14. * @param {*} binaryType 'blob' or 'arraybuffer';//arraybuffer是字节
  15. */
  16. tio.ws = function (ws_protocol, ip, port, paramStr, param, handler, heartbeatTimeout, reconnInterval, binaryType) {
  17. this.ip = ip;
  18. this.port = port;
  19. this.url = ws_protocol + '://' + ip + ':' + port;
  20. this.binaryType = binaryType || 'arraybuffer';
  21. console.log(binaryType);
  22. if (paramStr) {
  23. this.url += '?' + paramStr
  24. this.reconnUrl = this.url + "&"
  25. } else {
  26. this.reconnUrl = this.url + "?"
  27. }
  28. this.reconnUrl += "tiows_reconnect=true";
  29. this.param = param
  30. this.handler = handler
  31. this.heartbeatTimeout = heartbeatTimeout
  32. this.reconnInterval = reconnInterval
  33. this.lastInteractionTime = function () {
  34. if (arguments.length == 1) {
  35. this.lastInteractionTimeValue = arguments[0]
  36. }
  37. return this.lastInteractionTimeValue
  38. }
  39. this.heartbeatSendInterval = heartbeatTimeout / 2
  40. this.connect = function (isReconnect) {
  41. var _url = this.url;
  42. if (isReconnect) {
  43. _url = this.reconnUrl;
  44. }
  45. var ws = new WebSocket(_url)
  46. this.ws = ws
  47. ws.binaryType = this.binaryType; // 'arraybuffer'; // 'blob' or 'arraybuffer';//arraybuffer是字节
  48. var self = this
  49. ws.onopen = function (event) {
  50. self.handler.onopen.call(self.handler, event, ws)
  51. self.lastInteractionTime(new Date().getTime())
  52. self.pingIntervalId = setInterval(function () {
  53. self.ping(self)
  54. }, self.heartbeatSendInterval)
  55. }
  56. ws.onmessage = function (event) {
  57. self.handler.onmessage.call(self.handler, event, ws)
  58. self.lastInteractionTime(new Date().getTime())
  59. }
  60. ws.onclose = function (event) {
  61. clearInterval(self.pingIntervalId) // clear send heartbeat task
  62. try {
  63. self.handler.onclose.call(self.handler, event, ws)
  64. } catch (error) {
  65. }
  66. self.reconn(event)
  67. }
  68. ws.onerror = function (event) {
  69. self.handler.onerror.call(self.handler, event, ws)
  70. }
  71. console.log(ws);
  72. return ws
  73. }
  74. this.reconn = function (event) {
  75. var self = this
  76. setTimeout(function () {
  77. var ws = self.connect(true)
  78. self.ws = ws
  79. }, self.reconnInterval)
  80. }
  81. this.ping = function () {
  82. var iv = new Date().getTime() - this.lastInteractionTime(); // 已经多久没发消息了
  83. // 单位:秒
  84. if ((this.heartbeatSendInterval + iv) >= this.heartbeatTimeout) {
  85. this.handler.ping(this.ws)
  86. }
  87. };
  88. this.send = function (data) {
  89. this.ws.send(data);
  90. };
  91. }