websocket+sockjs+stompjs详解及实例

websocket+sockjs+stompjs详解及实例

https://segmentfault.com/a/1190000017204277

  1. /**
  2. * 依赖文件sockjs.js、stomp.js
  3. * */
  4. ;!(function (window) {
  5. 'use strict'
  6. let WS = function () {
  7. //保存所有的订阅事件 {Aevent:[pubfun(status,data),pubfun(status,data),...]}
  8. this.subEvents = {};
  9. this.isConnect = false;
  10. this.stompClient = null;
  11. this.selfClose = false;
  12. this.ws = null;
  13. this.url = null;
  14. };
  15. WS.prototype = {
  16. constructor: WS
  17. //设置连接状态
  18. , setConnect(status) {
  19. this.isConnect = status;
  20. }
  21. //建立连接
  22. , connect(url) {
  23. //若没有连接,才连接
  24. this.isConnect = false;
  25. this.url = url;
  26. this.ws = new SockJS(url);
  27. this.stompClient = Stomp.over(ws);
  28. this.stompClient.connect({}, (data) => {
  29. this.setConnect(true);
  30. this.connectSuc.apply(this, [stompClient, data]);
  31. }, (error) => {
  32. this.setConnect(false);
  33. this.connectErr.apply(this,[stompClient,error]);
  34. });
  35. this.ws.onclose = (e) => {
  36. this.isConnect = false;
  37. if(!this.selfClose){
  38. this.reConnect();
  39. }
  40. }
  41. return stompClient;
  42. }
  43. //手动断开连接
  44. , disconnect() {
  45. if(this.stompClient != null && this.isConnect) {
  46. this.stompClient.disconnect();
  47. this.isConnect = false;
  48. this.selfClose = true;
  49. this.ws = null;
  50. this.stompClient = null;
  51. }
  52. }
  53. //重连
  54. , reConnect(){
  55. if(this.isConnect){return;}
  56. this.connect(this.url);
  57. }
  58. //连接成功后的回调
  59. , connectSuc(stompClient, data) {
  60. if(this.isConnect){
  61. //发布连接成功事件
  62. this.trigger.apply(this, ['connectSuc', stompClient.subscribe.bind(stompClient), data]);
  63. //发布发送消息到服务端事件
  64. this.trigger.apply(this, ['sendMessage', stompClient.send.bind(stompClient), data]);
  65. }
  66. }
  67. //连接失败后的回调
  68. , connectErr(stompClient, data){
  69. //发布连接失败事件
  70. this.trigger.apply(this, ['connectErr', stompClient, data]);
  71. }
  72. //发布函数
  73. , trigger(eventType, ...data) {
  74. eventType = this.subEvents[eventType];
  75. for (var i = 0; i < eventType.length; i++) {
  76. eventType[i].apply(this, data);
  77. }
  78. }
  79. //订阅方法 --->用于订阅指定事件
  80. , on(eventType, handle) {
  81. if (!(eventType in this.subEvents)) {
  82. this.subEvents[eventType] = [];
  83. }
  84. this.subEvents[eventType].push(handle);
  85. }
  86. //删除订阅
  87. , off(eventType, handle) {
  88. eventType = this.subEvents[eventType];
  89. if (eventType) {
  90. let handleIndex = eventType.indexOf(handle);
  91. if (handleIndex >= 0) {
  92. eventType.splice(handleIndex, 1);
  93. }
  94. }
  95. }
  96. };
  97. window.WS = WS;
  98. })(window);
  99. /**
  100. *
  101. * var ws = new WS();
  102. ws.connect("/helloWebsocket");
  103. ws.on('connectSuc',function (subscribe,data) {
  104. subscribe('/topic/serverSend', function(response){
  105. info.innerHTML += "<div>"+response+"</div>";
  106. });
  107. subscribe('/topic/serverResponse',function (response) {
  108. info.innerHTML += "<div>"+response+"</div>";
  109. });
  110. });
  111. ws.on('connectErr',function (stompClient,data) {
  112. });
  113. //客户端发送消息给服务端
  114. ws.on('sendMessage',function (send,data) {
  115. send("/client/clientSendMessage",{},"hello server !!");
  116. });
  117. //强制关闭窗口后,断开连接
  118. window.onunload = function (ev) {
  119. ws.disconnect();
  120. }
  121. *
  122. * */