Blame view

public/js/service/socket/chat.js 2.53 KB
李奇 authored
1 2 3 4 5 6 7
/**
 * socket 聊天
 *
 * @author: liqi <qi.li@yoho.cn>
 * @date: 2016/11/8
 */
8 9
var times,
    param,
李奇 authored
10 11
    socket,
    server,
李奇 authored
12 13
    servers,
    options,
14
    connectId,
李奇 authored
15 16 17 18 19 20
    conversationMessage;

function _randomServer() {
    return servers[new Date().getTime() % servers.length];
}
李奇 authored
21 22
/**
 * socket初始化
李奇 authored
23
 * @param opts
李奇 authored
24
 */
李奇 authored
25 26
function socketInit(opts) {
    options = opts;
李奇 authored
27 28 29
    servers = options.servers;
    server = _randomServer();
    conversationMessage = options.conversationMessage;
30
    param = JSON.stringify(conversationMessage);
李奇 authored
31 32

33 34 35 36 37
    /**
     * 连接
     * @returns {WebSocket}
     */
    function socketConnect() {
李奇 authored
38
        var socketIns = new WebSocket(server + '/im?param=' + param);
39
40
        socketIns.onmessage = options.onMessage || function() {};
41
42
        socketIns.onopen = options.onOpen || function() {};
43
李奇 authored
44
        socketIns.onerror = options.onError || function() {};
郝肖肖 authored
45
李奇 authored
46
        return socketIns || {};
李奇 authored
47 48 49 50 51
    }

    if (window.WebSocket) {
        setTimeout(function() {
            times = 1;
52 53

            // socket对象存在或者正在链接
htoooth authored
54
            if (socket || socket && socket.readyState === WebSocket.CONNECTING) {
55 56 57
                return;
            }
李奇 authored
58 59 60 61
            socket = socketConnect();
            connectId = setInterval(function() {
                if (socket.readyState !== WebSocket.OPEN) {
                    if (times < 3) {
李奇 authored
62
                        socket.close && socket.close();
李奇 authored
63 64 65 66
                        socket = socketConnect();
                        times++;
                    } else {
                        clearInterval(connectId);
67
                        socket = null;
李奇 authored
68
郝肖肖 authored
69
                        // 连接失败回调
李奇 authored
70 71 72
                        options.connectFailCb();
                    }
                } else {
73
                    socket.onclose = options.onClose || function() {};
74
李奇 authored
75 76 77 78 79
                    clearInterval(connectId);
                }
            }, 5000);

        }, 1000);
李奇 authored
80 81 82
    }
}
李奇 authored
83 84
/**
 * 发送消息
李奇 authored
85
 * @param msg 消息体
李奇 authored
86
 */
李奇 authored
87 88 89 90
function sendMsg(msg) {
    if (!window.WebSocket) {
        return;
    }
91
李奇 authored
92
    if (socket && socket.readyState === WebSocket.OPEN) {
李奇 authored
93
        socket.send(JSON.stringify(msg));
李奇 authored
94
    } else {
李奇 authored
95
        options.socketClosedCb();
李奇 authored
96 97 98
    }
}
李奇 authored
99
/**
李奇 authored
100 101 102 103 104 105 106 107
 * 链接可用
 * @returns {*|boolean}
 */
function isOpen() {
    return socket && socket.readyState === WebSocket.OPEN;
}

/**
李奇 authored
108 109
 * 关闭socket
 */
110
function clearSocket() {
李奇 authored
111
    if (socket) {
112
        socket = null;
李奇 authored
113 114 115
    }
}
李奇 authored
116
module.exports = {
姜枫 authored
117
    socket: socket,
李奇 authored
118
    init: socketInit,
119
    clear: clearSocket,
李奇 authored
120 121
    send: sendMsg,
    isOpen: isOpen
李奇 authored
122 123
};