{"size": 1385, "ext": "js", "max_stars_repo_path": "socket-serve/web-socket-04/webSocetForwordClient.js", "max_stars_repo_name": "a982338665/angular", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "socket-serve/web-socket-04/webSocetForwordClient.js", "max_issues_repo_name": "a982338665/angular", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "socket-serve/web-socket-04/webSocetForwordClient.js", "max_forks_repo_name": "a982338665/angular", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0833333333, "max_line_length": 78, "alphanum_fraction": 0.5516245487}
const WebSocket = require('ws');
// const wsconfig = require('../config/wsconfig');

let is_open = false;
let ws ;
let mqList = [];
function getws() {
    if(ws){
        return ws;
    }else {
        ws = new WebSocket('ws://localhost:5888');
        ws.on('open', function open() {
            is_open = true;
            mqList.forEach(function (item,index) {
                forwordNew(item.receiveuser,item.msgbody,item.type)
            });
            mqList = [];
        });
        ws.on("message", function(data) {
            console.log(data);
        });
        return ws;
    }
}

function forword(receiveuser,msgbody){
    let ws = getws();
    let data = {
        "receiveuser":receiveuser,
        "msgbody":msgbody
    };
    if(is_open){
        let dataStr = JSON.stringify(data);
        console.error('发送数据：----'+dataStr);
        ws.send(dataStr);
    }else{
        mqList.push({"receiveuser":receiveuser,"msgbody":msgbody})
    }

}


function forwordNew(receiveuser,msgbody,type){
    let ws = getws();
    let data = {
        "type":type,
        "receiveuser":receiveuser,
        "msgbody":msgbody
    };
    if(is_open){
        let dataStr = JSON.stringify(data);
        ws.send(dataStr);
    }else{
        mqList.push({"receiveuser":receiveuser,"msgbody":msgbody,"type":type})
    }

}

exports.forword = forword;
exports.forwordNew = forwordNew;