服务器端事件在SharedWorker中未收到消息

时间:2019-06-18 19:45:03

标签: shared-worker

我正在我的REACT应用中实现服务器端事件。我面临的问题是在“共享工作程序”方案中,未收到来自服务器的消息。据我所知,启用sharedworker时未建立连接。我想知道如何使用SharedWorker获取服务器端事件。

        this.sharedWorker = new SharedWorker(workerURL);
        //myWorker.port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.

        this.sharedWorker.port.onmessage = (e)=> {
          this.listeners[e.data.event] && this.listeners[e.data.event][0](e.data)
        }

        this.sharedWorker.onerror=(e)=>{
            console.log("Shared worker is closed due to error.")
            this.port && this.port.close();
            this.closeSSEConnection("Shared worker is closed due to error.");
        }

        //On tab close, remove the port in the port array
        window.addEventListener('unload',() => {           
            this.sharedWorker.port.postMessage({ 
                eventType:"tabClose"
            });
            this.port && this.port.close();
        });
    }

预期输出将是服务器端事件,应在控制台中打印,但不打印任何消息

0 个答案:

没有答案