var socketOpen = false;
var socketMsgQueue = [];

const connectSocket = function(callback) {
	console.warn('connectSocket打开！');
	uni.connectSocket({
		url: 'wss://www.yuyekeji.cn/node/'
	});
	uni.onSocketOpen(function(res) {
		socketOpen = true;
		console.log('WebSocket连接已打开！');
		for (var i = 0; i < socketMsgQueue.length; i++) {
			sendSocketMessage(socketMsgQueue[i]);
		}
		socketMsgQueue = [];

		if (callback)
			callback();
	});
	uni.onSocketError(function(res) {
		console.log('WebSocket连接打开失败，请检查！');
	});
}
const sendSocketMessage = function sendSocketMessage(msg) {
	if (socketOpen) {
		uni.sendSocketMessage({
			data: msg
		});
	} else {
		socketMsgQueue.push(msg);
	}
}

export default {
	connectSocket,
	sendSocketMessage
}
