cordova-rtc/cordova-plugin-iosrtc

Keep getting readonly TypeError when trying to set up a RTCPeerConnection

gitlaura opened this issue · 14 comments

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

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

We are using the plugin successfully on 8.1.x.

Version 9.2

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

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

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?

@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

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.

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

ibc commented

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

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

@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.

Thank you!