hbk6zzez.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. $(function () {
  2. openWebSocket();
  3. });
  4. window.onbeforeunload = function(){
  5. closeWebSocket();
  6. }
  7. let vm = new Vue({
  8. el: '#rrapp',
  9. data: {
  10. showList: true,
  11. title: null,
  12. pickUpCode: {},
  13. q: {},
  14. pickUpCodeList:[]
  15. },
  16. methods: {
  17. }
  18. });
  19. var webSocket;
  20. function openWebSocket() {
  21. var storeId = sessionStorage.getItem("storeId");
  22. if(window.location.search){
  23. storeId = window.location.search.split("=")[1];
  24. }
  25. //暂时性
  26. if(!storeId){
  27. storeId = 163
  28. }
  29. console.log(storeId);
  30. if ("WebSocket" in window) {
  31. console.log("当前浏览器支持WebSocket");
  32. //实现化WebSocket对象
  33. //指定要连接的服务器地址与端口建立连接
  34. //注意ws、wss使用不同的端口。我使用自签名的证书测试,
  35. //无法使用wss,浏览器打开WebSocket时报错
  36. //ws对应http、wss对应https。
  37. webSocket = new WebSocket("ws://8.135.102.238:8080/ws/server/"+storeId);
  38. // webSocket = new WebSocket("ws://127.0.0.1:8080/ws/server/163");
  39. // webSocket = new WebSocket("wss://cb.k1net.cn/ws/server/"+storeId);
  40. if (webSocket.readyState === webSocket.CONNECTING) {
  41. console.log('1.连接正在打开......');
  42. }
  43. // ---------- WebSocket 对象注册事件 ----------
  44. //连接打开事件
  45. webSocket.onopen = function () {
  46. if (webSocket.readyState === webSocket.OPEN) {
  47. console.log('2.连接已打开');
  48. }
  49. };
  50. //收到消息事件
  51. webSocket.onmessage = function (msg) {
  52. var data = Object.assign({}, JSON.parse(msg.data));
  53. console.log('3.接收到服务端信息......data:'+data);
  54. vm.sessionId = data.sessionId;
  55. vm.pickUpCodeList = [];
  56. if(data.pickUpCodeList.length > 0){
  57. for(var i = 0 ; i < data.pickUpCodeList.length ; i++){
  58. if(data.pickUpCodeList[i].storeId == storeId){
  59. vm.pickUpCodeList.push(data.pickUpCodeList[i]);
  60. }
  61. }
  62. }
  63. };
  64. //连接关闭事件
  65. webSocket.onclose = function () {
  66. if (!webSocket) {
  67. console.log('4.Socket连接已关闭');
  68. }
  69. };
  70. //发生了错误事件
  71. webSocket.onerror = function () {
  72. console.log("5.Socket发生了错误");
  73. }
  74. } else {
  75. console.log("当前浏览器不支持WebSocket");
  76. return false;
  77. }
  78. }
  79. function sendMessage() {
  80. if(webSocket && webSocket.readyState === webSocket.OPEN) {
  81. webSocket.send("client manual send message.")
  82. } else {
  83. console.log('6.未创建WebSocket连接');
  84. }
  85. }
  86. function closeWebSocket() {
  87. if (webSocket != null) {
  88. webSocket.close();
  89. webSocket = null;
  90. }
  91. console.log("关闭WebSocket");
  92. }
  93. //窗口关闭时,关闭连接
  94. window.unload = function () {
  95. webSocket.close();
  96. };