1
0

hbk6zzez.js 2.3 KB

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