

function connect(user, url, func) {
    uni.connectSocket({
        url: url,
        header: { 'content-type': 'application/json' },
        success: function () {
            console.log('websocket连接成功~')
        },

        fail: function () {
            console.log('websocket连接失败~')
        }
    })

    uni.onSocketOpen(function (res) {
        uni.showToast({
            title: '已连接聊天~',
            icon: "success",
            duration: 2000
        })

        //接受服务器消息
        uni.onSocketMessage(func);//func回调可以拿到服务器返回的数据
    });

    uni.onSocketError(function (res) {
        uni.showToast({
            title: 'websocket连接失败，请检查！',
            icon: "none",
            duration: 2000
        })
    })
}

//发送消息
function send(msg) {
    uni.sendSocketMessage({
        data: msg
    });
}
let websocket = {
    connect,
    send
}

// module.exports = {
//     connect: connect,
//     send: send
// }
export default websocket