SecurityError with Chromecast program

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

SecurityError with Chromecast program

15 Jun 2021, 12:19

Hello,

I have been working on a small project for the past week to automatically open Google Chrome, load 7 tabs with different websites in each, and Chromecast to each one of them (there is a different Chromecast for each website). With the help from @gregster and using Chrome.ahk, I have gotten it to work! However, I run into a security error after the 3rd website gets casted. To my knowledge, it doesn't look like an AHK error, but more of a Google Chrome issue, but I do not know how to fix it. Here is my project and the error message I get. Please let me know if anyone can help. Thank you!

Code: Select all

#NoEnv
#Include ../Chrome.ahk

SetBatchLines, -1
; SetTitleMatchMode, 2
; SendMode Input

; Declaring and initializing URL and Chromecast variables
URL1 := "NameOfURL1"
Chromecast1 := "NameOfCast1"

URL2 := "NameOfURL2"
Chromecast2 := "NameOfCast2"

URL3 := "NameOfURL3"
Chromecast3 := "NameOfCast3"

URL4 := "NameOfURL4"
Chromecast4 := "NameOfCast4"

URL5 := "NameOfURL5"
Chromecast5 := "NameOfCast5"

URL6 := "NameOfURL6"
Chromecast6 := "NameOfCast6" 

URL7 := "NameOfURL7"
Chromecast7 := "NameOfCast7"

/*
	ProfilePath - Path to the user profile directory to use. Will use the standard if left blank.
	URLs        - The page or array of pages for Chrome to load when it opens.
	Flags       - Additional flags for chrome when launching.
	ChromePath  - Path to chrome.exe, will detect from start menu when left blank.
	DebugPort   - What port should Chrome's remote debugging server run on.
*/
global ChromeInst := new Chrome(,[URL1, URL2, URL3, URL4, URL5, URL6, URL7],"--start-maximized",,9222)
Sleep, 10000

CastPage(URL1, Chromecast1)
CastPage(URL2, Chromecast2)
CastPage(URL3, Chromecast3)
CastPage(URL4, Chromecast4)
CastPage(URL5, Chromecast5)
CastPage(URL6, Chromecast6)
CastPage(URL7, Chromecast7)


CastPage(URL:="", NameOfCast:="") {
    if !(PageInst := ChromeInst.GetPageByUrl(URL))
    {
        MsgBox, Could not retrieve page!
        ChromeInst.Kill()
    }
    else
    {
        /*
		    Waits for the page's readyState to match the DesiredState.

			DesiredState - The state to wait for the page's ReadyState to match
			Interval     - How often it should check whether the state matches
		*/
        PageInst.WaitForLoad("complete", 100)
        TabCast := ChromeInst.GetPageByUrl(URL)

        TabCast.Call("Cast.enable")
        TabCast.Call("Cast.startTabMirroring", { sinkName: NameOfCast})
    }
}

^q::
ExitApp
Like I said before, this message only appears after the 3rd website gets casted.
Attachments
error.PNG
error.PNG (10.34 KiB) Viewed 778 times
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: SecurityError with Chromecast program

15 Jun 2021, 12:24

rgoijre23 wrote:
15 Jun 2021, 12:19
load 7 tabs with different websites in each,
There is a default limit of 6 in Windows for this kind of websocket connections. See https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890&p=358760#p358760

Edit:
PS: The ChromeInst also counts as one connection, afaik, and I think you are creating (unnecessarily) two websocket connections per page per function call. One should suffice.
Still for separate 7 tabs you would need to raise the limit at least to 8 (one Chrome instance + 7 page instances).
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: SecurityError with Chromecast program

15 Jun 2021, 13:13

gregster wrote:
15 Jun 2021, 12:24
rgoijre23 wrote:
15 Jun 2021, 12:19
load 7 tabs with different websites in each,
There is a default limit of 6 in Windows for this kind of websocket connections. See https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890&p=358760#p358760

Edit:
PS: The ChromeInst also counts as one connection, afaik, and I think you are creating (unnecessarily) two websocket connections per page per function call. One should suffice.
Still for separate 7 tabs you would need to raise the limit at least to 8 (one Chrome instance + 7 page instances).
Thanks for the input @gregster. My registry editor does not contain the FeatureControl or FEATURE_WEBSOCKET_MAXCONNECTIONSPERSERVER key so do I have to add those before running the script (if I end up doing a script)? How do I check how many websocket connections I am running when I run my code? Is that just checking with http 127.0.0.1:9222/?
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: SecurityError with Chromecast program

15 Jun 2021, 23:24

rgoijre23 wrote:
15 Jun 2021, 13:13
My registry editor does not contain the FeatureControl or FEATURE_WEBSOCKET_MAXCONNECTIONSPERSERVER key so do I have to add those before running the script (if I end up doing a script)?[/url]?
I think RegWrite will create that key, if it doesn't exist (did you check both HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE ?) - but only if the script has sufficient rights. Probably you would have to run the script 'as admin' (Errorlevel and A_LastError should tell you about problems that occurred)

rgoijre23 wrote:
15 Jun 2021, 13:13
How do I check how many websocket connections I am running when I run my code? Is that just checking with http://127.0.0.1:9222/?
Usually, I just count how many active page instances/websocket connections I created in my script - if I get an error message, I know that I have to many. (In your function above you create two per function call, afaics). http://127.0.0.1:9222/ on the other hand will only show you the list of available targets/tabs to connect to. There is probably some way to determine the number of active websocket connections of a process - but so far I didn't care to investigate :shifty:

It's also possible to close websocket connections which are not needed anymore (by using the Disconnect() method on specific tabs). Since the Chromecasts don't seem to care, if the mirrored tab is still connected to the chrome debug protocol, you can even disconnect each tab immediately after starting its casting session - at least from what I can say with my tests with a single Chromecast. So you could probably do this without raising the websocket limit:

Code: Select all

CastPage(URL, NameOfCast) {
    if !(PageInst := ChromeInst.GetPageByUrl(URL))
    {
        MsgBox, Could not retrieve page!
        ChromeInst.Kill()
    }
    else
    {
        /*
		    Waits for the page's readyState to match the DesiredState.

			DesiredState - The state to wait for the page's ReadyState to match
			Interval     - How often it should check whether the state matches
		*/
        PageInst.WaitForLoad("complete", 100)
		
		PageInst.Call("Cast.enable")
        PageInst.Call("Cast.startTabMirroring", { sinkName: NameOfCast})
		PageInst.Disconnect()
    }
}
This way, there is no additional websocket connection left, after the function call has finished/returned.
Temporarily, though, you'll create one connection per function call but it'll get disconnected immediately after starting the casting session.
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: SecurityError with Chromecast program

16 Jun 2021, 12:26

gregster wrote:
15 Jun 2021, 23:24
It's also possible to close websocket connections which are not needed anymore (by using the Disconnect() method on specific tabs). Since the Chromecasts don't seem to care, if the mirrored tab is still connected to the chrome debug protocol, you can even disconnect each tab immediately after starting its casting session - at least from what I can say with my tests with a single Chromecast. So you could probably do this without raising the websocket limit:

Code: Select all

CastPage(URL, NameOfCast) {
    if !(PageInst := ChromeInst.GetPageByUrl(URL))
    {
        MsgBox, Could not retrieve page!
        ChromeInst.Kill()
    }
    else
    {
        /*
		    Waits for the page's readyState to match the DesiredState.

			DesiredState - The state to wait for the page's ReadyState to match
			Interval     - How often it should check whether the state matches
		*/
        PageInst.WaitForLoad("complete", 100)
		
		PageInst.Call("Cast.enable")
        PageInst.Call("Cast.startTabMirroring", { sinkName: NameOfCast})
		PageInst.Disconnect()
    }
}
This way, there is no additional websocket connection left, after the function call has finished/returned.
Temporarily, though, you'll create one connection per function call but it'll get disconnected immediately after starting the casting session.
This is much better than increasing the limit of websockets for my project. I appreciate the support @gregster. Do you know how I could check if one of the Chromecasts goes offline by accident that I could somehow immediately "recast" it? I am checking Chrome.ahk to see if there are some functions I can utilize, but no luck. I am assuming it might be an event call?
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: SecurityError with Chromecast program

16 Jun 2021, 12:32

Yeah, probably. Cast.sinksUpdated "is fired whenever the list of available sinks changes" and Cast.issueUpdated" is fired whenever the outstanding issue/error message changes. |issueMessage| is empty if there is no issue." These events might help, checked via callback function. I will make some tests later today.

Even if you know that the number of sinks changed, you'd still have to loop through the array of available sinks... to identify the missing one (but then you probably can't do much, other than restarting that Chromecast manually). But if "just" the session ended for some reason, that shcould be identifiable, too (but remains to be determined). Then (if the Chromecast is still/again available), you can probably simply restart the corresponding session.
Perhaps the issueUpdated event can help with this, too. It depends on what it actually reports...
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: SecurityError with Chromecast program

16 Jun 2021, 12:52

gregster wrote:
16 Jun 2021, 12:32
Yeah, probably. Cast.sinksUpdated "is fired whenever the list of available sinks changes" and Cast.issueUpdated" is fired whenever the outstanding issue/error message changes. |issueMessage| is empty if there is no issue." These events might help, checked via callback function. I will make some tests later today.

Even if you know that the number of sinks changed, you'd still have to loop through the array of available sinks... to identify the missing one (but then you probably can't do much, other than restarting that Chromecast manually). But if "just" the session ended for some reason, that shcould be identifiable, too (but remains to be determined). Then (if the Chromecast is still/again available), you can probably simply restart the corresponding session.
Perhaps the issueUpdated event can help with this, too. It depends on what it actually reports...
Awesome thank you for the input :D
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: SecurityError with Chromecast program

17 Jun 2021, 03:42

I didn't get very far - the messages from these Chromecast sink events don't seem to be very consistent, I'll need some more testing.
Last edited by gregster on 17 Jun 2021, 04:46, edited 1 time in total.
Reason: edit
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: SecurityError with Chromecast program

17 Jun 2021, 12:01

gregster wrote:
17 Jun 2021, 03:42
I didn't get very far - the messages from these Chromecast sink events don't seem to be very consistent, I'll need some more testing.
What do you mean they aren't consistent? Meaning if they shutoff they will either go back online or not?
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: SecurityError with Chromecast program

17 Jun 2021, 14:47

@gregster Actually a more important question that I am curious about.

Code: Select all

; Declaring and initializing URL and Chromecast variables
URL1 := "NameOfURL1"
Chromecast1 := "NameOfCast1"
URL2 := "NameOfURL2"
Chromecast2 := "NameOfCast2"
URL3 := "NameOfURL3"
Chromecast3 := "NameOfCast3"
URL4 := "NameOfURL4"
Chromecast4 := "NameOfCast4"
URL5 := "NameOfURL5"
Chromecast5 := "NameOfCast5"
URL6 := "NameOfURL6"
Chromecast6 := "NameOfCast6" 
URL7 := "NameOfURL7"
Chromecast7 := "NameOfCast7"
Say for example, a URL or name of a Chromecast changes, is there any way to initialize the variables in a way such that they are not "hardcoded"?
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: SecurityError with Chromecast program

17 Jun 2021, 22:33

rgoijre23 wrote:
17 Jun 2021, 12:01
gregster wrote:
17 Jun 2021, 03:42
I didn't get very far - the messages from these Chromecast sink events don't seem to be very consistent, I'll need some more testing.
What do you mean they aren't consistent? Meaning if they shutoff they will either go back online or not?
Yeah, the notifications act sometimes sluggish, sometimes seemingly without obvious reason, (temporarily) report duplicates, or chromecasts that have been already shut off, and sometimes sinks like 'pseudo cloud' which is probably something Chromecast-internal (it's definitely not a Chromecast in my wifi). It's probably a matter of filtering these messages a bit, not acting on every single update and perhaps only check every x minutes, if the intended sessions are still alive. I will definitely have another look at it - it's just too hot currently in my apartment :)
Say for example, a URL or name of a Chromecast changes, is there any way to initialize the variables in a way such that they are not "hardcoded"?
I guess that would be another topic - but if the names or URLs rarely change, I would just edit the original script.
Of course, you could also use an ini-file (or something similar) and edit that instead (see IniRead, IniWrite, ...), or even create a nice GUI for managing these changes. It depends on what you have in mind. But I would start a separate topic for this more general question, as this is not really Chrome.ahk- or Chromecast-related anymore.
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: SecurityError with Chromecast program

18 Jun 2021, 12:00

gregster wrote:
17 Jun 2021, 22:33
rgoijre23 wrote:
17 Jun 2021, 12:01
gregster wrote:
17 Jun 2021, 03:42
I didn't get very far - the messages from these Chromecast sink events don't seem to be very consistent, I'll need some more testing.
What do you mean they aren't consistent? Meaning if they shutoff they will either go back online or not?
Yeah, the notifications act sometimes sluggish, sometimes seemingly without obvious reason, (temporarily) report duplicates, or chromecasts that have been already shut off, and sometimes sinks like 'pseudo cloud' which is probably something Chromecast-internal (it's definitely not a Chromecast in my wifi). It's probably a matter of filtering these messages a bit, not acting on every single update and perhaps only check every x minutes, if the intended sessions are still alive. I will definitely have another look at it - it's just too hot currently in my apartment :)
Say for example, a URL or name of a Chromecast changes, is there any way to initialize the variables in a way such that they are not "hardcoded"?
I guess that would be another topic - but if the names or URLs rarely change, I would just edit the original script.
Of course, you could also use an ini-file (or something similar) and edit that instead (see IniRead, IniWrite, ...), or even create a nice GUI for managing these changes. It depends on what you have in mind. But I would start a separate topic for this more general question, as this is not really Chrome.ahk- or Chromecast-related anymore.
Appreciate the input! :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 172 guests