2017-09-10 3 views
1

서비스 작업자 인스턴스에 메시지를 게시하려고합니다. 나는 다음과 같은 오류가 발생합니다 다음과 같이'ServiceWorker'에서 'postMessage'를 실행하지 못했습니다. 함수를 복제 할 수 없습니다.

Failed to execute 'postMessage' on 'ServiceWorker': function(){ obj.removeListener(ev, fn); } could not be cloned.

내 코드는 다음과 같습니다

var socket = io(); 

function onYouTubeIframeAPIReady() { 
    //Tell the service worker who I am 
    navigator.serviceWorker.ready.then(serviceWorkerRegistration => { 
     navigator.serviceWorker.controller.postMessage({ 
      name: 'socketInit', 
      value: socket 
     }); 
    }); 
} 

이런 일이 발생하는 이유 어떤 생각?

답변

1

실제로 답변을 찾았습니다. 문서에 있습니다.

:

Parameters
message The message to send to the service worker. This can be any structured-clonable type.

structured-clonable-type는 다음과 같이 정의되는 : 당신은 기본적으로 Function 타입 속성이 복제 할 수 없기 때문에 (아마도 IO() 객체 내부 어딘가에 존재 )을 가질 수 없습니다

The structured clone algorithm is an algorithm defined by the HTML5 specification for copying complex JavaScript objects. It is used internally when transferring data to and from Workers via postMessage(). It builds up a clone by recursing through the input object while maintaining a map of previously visited references in order to avoid infinitely traversing cycles.

Things that don't work with structured clone

Error and Function objects cannot be duplicated by the structured clone algorithm; attempting to do so will throw a DATA_CLONE_ERR exception. Attempting to clone DOM nodes will likewise throw a DATA_CLONE_ERR exception. Certain parameters of objects are not preserved: The lastIndex field of RegExp objects is not preserved. Property descriptors, setters, and getters (as well as similar metadata-like features) are not duplicated. For example, if an object is marked read-only using a property descriptor, it will be read-write in the duplicate, since that's the default condition. The prototype chain does not get walked and duplicated.