cordova-rtc / cordova-plugin-iosrtc

Cordova iOS plugin exposing the WebRTC W3C API
MIT License
688 stars 340 forks source link

Keep getting readonly TypeError when trying to set up a RTCPeerConnection #123

Closed gitlaura closed 8 years ago

gitlaura commented 8 years ago

I keep getting this error logged to the console when trying to set up a RTCPeerConnection: "Error in Success callbackId: iosrtcPlugin1441625638 : TypeError: Attempted to assign to readonly property"

Do you have any ideas what could be causing this error? Would greatly appreciate help!

ibc commented 8 years ago

Which iOS version? iOS >= 9 is required (not sure if iOS 8 works)...

warrenjmcdonald commented 8 years ago

We are using the plugin successfully on 8.1.x.

gitlaura commented 8 years ago

Version 9.2

gitlaura commented 8 years ago

Are there any obvious readonly properties that I might be trying to write over?

yocontra commented 8 years ago

@gitlaura Can you provide the code causing the issue + a stacktrace w/ line numbers?

gitlaura commented 8 years ago

I'm building an ios app for uProxy with cordova. I don't have the code up yet and it's a but complicated to build but will try to get it up soon.

The main problem with using iosrtc plugin is that I can't registerGlobals() until the device is ready, but I have a script that loads that uses RTCPeerConnection, RTCIceCandidate, and RTCSessionDescription before 'deviceready' fires. I have been trying to set window.RTCPeerConnection (and the other two functions) = cordova.plugins.iosrtc.RTCPeerConnection in a separate script before 'deviceready' is fired. I'm pretty sure this is causing the TypeError.

Have you seen anyone successfully set these functions before 'deviceready' fires?

yocontra commented 8 years ago

@gitlaura I actually wrote something for this (for the temasys webrtc plugin) - it mocks the RTC objects while they are loading, then replays the actions on the real ones when they are ready.

https://github.com/contra/rtc-everywhere/blob/master/lib/temasys/MockRTC.js

You can modify that pretty easily to work with this plugin

ibc commented 8 years ago

@gitlaura may you please confirm if the error you experience happens here?:

https://github.com/eface2face/cordova-plugin-iosrtc/blob/master/js/videoElementsHandler.js#L288

ibc commented 8 years ago

The main problem with using iosrtc plugin is that I can't registerGlobals() until the device is ready, but I have a script that loads that uses RTCPeerConnection, RTCIceCandidate, and RTCSessionDescription before 'deviceready' fires. I have been trying to set window.RTCPeerConnection (and the other two functions) = cordova.plugins.iosrtc.RTCPeerConnection in a separate script before 'deviceready' is fired. I'm pretty sure this is causing the TypeError.

This is how Cordova works, nothing to do here. Your JS app needs to be adapted to the Cordova design.

A very hard hack is the following (assuming somewebrtcsignalinglib.js if your JS script requiring window.RTCPeerConnection to exist on load time):

<script type="text/javascript">
    window.addEventListener('load', function()
    {
        console.log('DOM loaded');

        document.addEventListener('deviceready', function()
        {
            var script = document.createElement('script');

            script.type = 'text/javascript';
            script.src = 'libs/somewebrtcsignalinglib.js';
            script.async = false;  // IMPORTANT
            document.getElementsByTagName('head')[0].appendChild(script);
        });
    });
</script>

This <script> stuff must be added into your main index.html.

gitlaura commented 8 years ago

Okay, thanks for your help. Working on waiting to load the scripts until after 'deviceready'.

ibc commented 8 years ago

@gitlaura that should work (I mean: it works for me)

gitlaura commented 8 years ago

After more debugging, I discovered that I'm getting 'TypeError: Attempted to assign to readonly property' due to line 3343 in cordova-plugin-iosrtc.js:

event.cancelable = true;

This appears to be a readonly property that can't be changed after the event is initialized. Has anyone else run into this problem? I can only get my application to work after commenting this line out.

ibc commented 8 years ago

@gitlaura it seems to be an issue in the yaeti module (which implementes the DOM EventTarget interface). Since the given event is a real DOM Event object we should not try to set its cancelable property (which is read only according to the spec).

Will fix it and release a new version of the plugin with an updated version of the yaeti dependency.

gitlaura commented 8 years ago

Thank you!