socket.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function Message(obj, conn) {
  2. this.isResponsable = function() {
  3. return obj.id >= 0
  4. }
  5. this.sendResponse = function(data) {
  6. conn.send({
  7. id: obj.id,
  8. is_response: true,
  9. data: data
  10. })
  11. }
  12. this.data = function() {
  13. return obj.data
  14. }
  15. return this
  16. }
  17. /*
  18. {
  19. connected: function
  20. сlosed: function
  21. incoming: function
  22. }
  23. */
  24. function Conn(addr, callback = {}) {
  25. self = this
  26. let socket = null
  27. let connected = false
  28. let rStore = {}
  29. let idCounter = 0, idMax = Math.pow(2, 62)
  30. let mQueue = []
  31. function nextID() {
  32. idCounter += 1
  33. if (idCounter == idMax)
  34. idCounter = 0
  35. return idCounter
  36. }
  37. function connect() {
  38. socket = new WebSocket("ws://" + addr + "/api")
  39. socket.onopen = function(e) {
  40. connected = true
  41. if (callback.connected != null)
  42. callback.connected()
  43. //console.log('connected', mQueue)
  44. mQueue.forEach(function(obj) {
  45. self.sendMessage(obj.obj, obj.rCallback, obj.deadline)
  46. })
  47. mQueue = []
  48. }
  49. socket.onclose = function(e) {
  50. //console.log('close...')
  51. connected = false
  52. if (callback.closed != null)
  53. callback.closed()
  54. setTimeout(function() {
  55. self.reconnect()
  56. }, 3000)
  57. }
  58. socket.onmessage = function(e) {
  59. let mObj = JSON.parse(e.data)
  60. //console.log(mObj)
  61. if (mObj.is_response) {
  62. //console.log('response')
  63. let rCall = rStore[mObj.id]
  64. //console.log('rc', rCall, rStore)
  65. if (rCall) {
  66. clearTimeout(rCall.cancel)
  67. delete rStore[mObj.id]
  68. rCall.callback(mObj.data)
  69. //console.log(rStore)
  70. }
  71. } else {
  72. let m = new Message(mObj, self)
  73. callback.incoming(m)
  74. }
  75. }
  76. }
  77. this.reconnect = function() {
  78. //console.log('reconnect', connected)
  79. connect()
  80. }
  81. function send (obj) {
  82. if (!connected) {
  83. return
  84. }
  85. socket.send(JSON.stringify(obj))
  86. }
  87. this.sendMessage = function(obj, rCallback, deadline = 30000) {
  88. if (!connected) {
  89. mQueue.push({
  90. obj: obj,
  91. rCallback: rCallback,
  92. deadline: deadline
  93. })
  94. return
  95. }
  96. if (deadline == 0) {
  97. send({
  98. id: id,
  99. data: obj,
  100. })
  101. } else {
  102. let id = nextID()
  103. send({
  104. id: id,
  105. data: obj,
  106. })
  107. let cancel = setTimeout(function() {
  108. delete rStore[id]
  109. }, deadline)
  110. rStore[id] = {
  111. cancel: cancel,
  112. callback: rCallback,
  113. }
  114. }
  115. }
  116. connect()
  117. return self
  118. }