Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

shared worker - Firefox Quantum SharedWorker not work

In Chrome and Firefox <57 it works, but in Firefox Quantum doesn't work.

The goal is to send a message to another tab in the browser.

When filling in the text input and clicking the send button, the other tab in the browser should write the message in the browser console.

html file:

<script type="text/javascript">
    const myWorker = new SharedWorker( "sharedWorkerChat.js" );
    const port = myWorker.port;

    port.addEventListener( "message", function( e ) {
        console.log( e.data );
    }, false );

    port.start();

    function postMessage() {
        const value = document.getElementById( "input" ).value;
        console.log( value );
        port.postMessage( value );
    }
</script>


<input type="text" id="input" />

<button onclick="postMessage()"> Send </button>

sharedWorkerChat.js

var m;

var instances = [];
self.addEventListener( "connect", function ( e ) {
    var port = e.ports[ 0 ];
    instancias.push( port );

    port.addEventListener( "message", function ( message ) {
        m = message.data;
        instances.forEach( function( p ) {
            p.postMessage( m );
        });
    }, false );

    port.start();
}, false);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just came across the same problem, apparently Firefox Quantum enables the multiprocessing functionality by default and it doesn't yet support SharedWorkers.

To check if you have multiprocessing enable type about:debugging#workers in your address bar, if it is active you should see a yellow warning about SharedWorkers at the top of the page.

You can disable this functionality by following the link on that warning.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...