Vue 的长连接,仅使用 sockjs-client

739 阅读1分钟

一、安装

npm install sockjs-client --save

二、使用

import SockJS from 'sockjs-client';


mounted(){
	this.connection();
},
methods: {
	// 建立连接
	connection(){
    	let orderId = '200665765656567757656754';
        // 建立连接对象
        // 连接服务端提供的通信接口
        this.socket = new SockJS(`http://22.0.157.40:10020/business-h5/orders/ws/status?oid=${orderId}&token=${this.tradeToken}`);
        
        let self = this;
        this.socket.onopen = function (){
        	console.info('open');
            self.socket.send('from client');
        };
        
        this.socket.onmessage = function (e) {
        	console.info('message', e.data);
        };
        
        this.socket.onclose = function (){
        	console.info('close');
        };
        
        this.socket.onerror = function (e){
        	console.info(e);
        };
        
    }
}