Newer
Older
safe_production_front / public / DispatchLib / DispatchSharedWorker.js
wangxitong on 16 Aug 1 KB first commit
//chrome://inspect/#workers

var PortList = new Map();

var LoginInfo = null;

onconnect = function (e) {
    let port = e.ports[0]

    port.onmessage = function (e) {
        var msg = e.data;
        console.log("onmessage:", msg);
        if (msg.cmd == 'WorkerInit') {
            var name = msg.from;
            port.name = name;

            PortList.set(name, port);
           
            console.log("WorkerInit", name);
        } else if (msg.cmd == 'WorkerLogin') {
            //±£´æµÇ¼ÐÅÏ¢
            LoginInfo = msg.data;
        } else {  
            var to = msg.to;
            var from = msg.from;

            if (msg.cmd == 'WorkerWsMsg' &&
            msg.data.Msg == 'LoginReq') 
            {
                port.postMessage({
                    cmd: 'WorkerWsMsg',
                    from: 'worker',
                    to: port.name,
                    data: LoginInfo,
                })
                return;
            }

            if (to == "all") {
                PortList.forEach(function(value, index, array) {
                    if (index != from) {
                        value.postMessage(msg);
                    }
                });
            } else {
                var to_port = PortList.get(to);
                if (typeof to_port != 'undefined') {
                    to_port.postMessage(msg);
                }
            }
            

        }
    }

    port.onmessageerror = function (e) {
        console.log("onmessageerror", e);
        //PortList.delete(e.currentTarget.name);
    }

    port.postMessage({
        cmd: 'WorkerConnected',
        from: 'worker',
        to: '',
        data: {},
    })

    console.log("onconnect");
}