123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- $(function () {
- openWebSocket();
- });
- window.onbeforeunload = function(){
- closeWebSocket();
- }
- let vm = new Vue({
- el: '#rrapp',
- data: {
- showList: true,
- title: null,
- pickUpCode: {},
- q: {},
- pickUpCodeList:[]
- },
- methods: {
- }
- });
- var webSocket;
- function openWebSocket() {
- var storeId = sessionStorage.getItem("storeId");
- if(window.location.search){
- storeId = window.location.search.split("=")[1];
- }
- //暂时性
- if(!storeId){
- storeId = 163
- }
- console.log(storeId);
- if ("WebSocket" in window) {
- console.log("当前浏览器支持WebSocket");
- //实现化WebSocket对象
- //指定要连接的服务器地址与端口建立连接
- //注意ws、wss使用不同的端口。我使用自签名的证书测试,
- //无法使用wss,浏览器打开WebSocket时报错
- //ws对应http、wss对应https。
- webSocket = new WebSocket("ws://8.135.102.238:8080/ws/server/"+storeId);
- // webSocket = new WebSocket("ws://127.0.0.1:8080/ws/server/163");
- // webSocket = new WebSocket("wss://cb.k1net.cn/ws/server/"+storeId);
- if (webSocket.readyState === webSocket.CONNECTING) {
- console.log('1.连接正在打开......');
- }
- // ---------- WebSocket 对象注册事件 ----------
- //连接打开事件
- webSocket.onopen = function () {
- if (webSocket.readyState === webSocket.OPEN) {
- console.log('2.连接已打开');
- }
- };
- //收到消息事件
- webSocket.onmessage = function (msg) {
- var data = Object.assign({}, JSON.parse(msg.data));
- console.log('3.接收到服务端信息......data:'+data);
- vm.sessionId = data.sessionId;
- vm.pickUpCodeList = [];
- if(data.pickUpCodeList.length > 0){
- for(var i = 0 ; i < data.pickUpCodeList.length ; i++){
- if(data.pickUpCodeList[i].storeId == storeId){
- vm.pickUpCodeList.push(data.pickUpCodeList[i]);
- }
- }
- }
- };
- //连接关闭事件
- webSocket.onclose = function () {
- if (!webSocket) {
- console.log('4.Socket连接已关闭');
- }
- };
- //发生了错误事件
- webSocket.onerror = function () {
- console.log("5.Socket发生了错误");
- }
- } else {
- console.log("当前浏览器不支持WebSocket");
- return false;
- }
- }
- function sendMessage() {
- if(webSocket && webSocket.readyState === webSocket.OPEN) {
- webSocket.send("client manual send message.")
- } else {
- console.log('6.未创建WebSocket连接');
- }
- }
- function closeWebSocket() {
- if (webSocket != null) {
- webSocket.close();
- webSocket = null;
- }
- console.log("关闭WebSocket");
- }
- //窗口关闭时,关闭连接
- window.unload = function () {
- webSocket.close();
- };
|