Code Monkey home page Code Monkey logo

Comments (6)

raff avatar raff commented on August 12, 2024 1

Ok, so I spent a little time trying out your example. A few things:

  • the read message / closed network connection are "normal" for the way godet works. Basically Chrome debugger protocol exposes one websocket for each "tab", but godet connects to only one tab at a time. So when you create/switch to a new tab godet closes the current connection and opens a new one.
  • you don't give more details about the chrome crash and your example doesn't really crash, so I suspect that what you are doing is running your application multiple times and that causes chrome to keep creating new tabs. At some point Chrome runs out of memory and it crashes.

My suggestion for the crash, assuming you are running a limited number of tabs every time (as in your example) re-use existing tabs or close them. godet exposes both an ActivatTab method (to select an existing tab) and a CloseTab method (to close it). You can get the list of tabs by calling TabList or you can keep track of the opened tabs (the value returned by NewTab). I'll attach an updated example at the bottom that closes all tabs before opening the tabs from your example (and repeats the process 100 times). You can also close the tab after you take the screenshot.

For processing multiple pages in parallelel, things are a little more tricky.

The simplest thing to do it's probably to create a new instance of "godet" for every page you want to process. The instance would create one tab, load the page, take the screenshot, close the tab and terminate. All instances should be able to connect to the same chrome and since they have their own websocket connected to a specific tabs, the events you will receive will be related to that particular page.

This is the code that closes all "page" tabs before loading your pages:

`func main() {
remote, err := godet.Connect("localhost:9222", true)
if err != nil {
fmt.Println("cannot connnect to Chrome Insatnce:", err)
return
}

defer remote.Close()
remote.RuntimeEvents(true)
remote.NetworkEvents(true)
remote.PageEvents(true)

    for x := 0; x < 100; x++ {
        // get current list of tabs
        tabs, err := remote.TabList("page")
        if err != nil {
            fmt.Println("cannot get tab list:", err)
            return
        }

        // close open tabs
        for _, t := range tabs {
            remote.CloseTab(t)
        }

        // now open new list
        for _, url := range urlList {
                tab, _ := remote.NewTab(url)
                fmt.Println("new tab:", tab, url)
        }

        fmt.Println("------------------------------------------")
        time.Sleep(time.Second)
    }

fmt.Println("End.")

}
`

from godet.

yangluoshen avatar yangluoshen commented on August 12, 2024
"read message: read tcp [::1]:56088->[::1]:19222: use of closed network connection"

I found this error occurs usually.

from godet.

raff avatar raff commented on August 12, 2024

I can reproduce the "closed connection" problem (and actually I didn't build godet with the purpose of driving multiple tabs in mind).
I'll try to look at it and let you know. Thanks!

from godet.

raff avatar raff commented on August 12, 2024

This version runs the set of URLs in parallel (and for each one waits for the page to load and then takes a screenshot). Let me know if it does what you need:

`// +build ignore
package main

import (
"fmt"
"github.com/raff/godet"
"sync"
"time"
)

var (
urlList = []string{
"https://github.com",
"https://github.com/beetbox/beets",
"https://github.com/CreateJS/SoundJS",
"https://github.com/Soundnode/soundnode-app",
"https://github.com/gillesdemey/Cumulus",
"https://github.com/AudioKit/AudioKit",
"https://github.com/mopidy/mopidy",
"https://github.com/cashmusic/platform",
"https://github.com/musescore/MuseScore",
}
)

func processPage(id int, url string) {
var remote *godet.RemoteDebugger
var err error

//
// the Connect may temporary fail so retry a few times
//
for i := 0; i < 10; i++ {
	if i > 0 {
		time.Sleep(500 * time.Millisecond)
	}

	remote, err = godet.Connect("localhost:9222", false)
	if err == nil {
		break
	}

	fmt.Println(id, "connect", err)
}

if err != nil {
	fmt.Println(id, "cannot connect to browser")
	return
}

fmt.Println(id, "connected")
defer remote.Close()

done := make(chan bool)

//
// this should wait until the page request has loaded (if the page has multiple frames there
// may be more "frameStoppedLoading" events and the check should be more complicated)
//
remote.CallbackEvent("Page.frameStoppedLoading", func(params godet.Params) {
	fmt.Println(id, "page loaded", params)
	done <- true
})

tab, err := remote.NewTab(url)
if err != nil {
	fmt.Println(id, "cannot create tab:", err)
	return
}

defer func() {
	remote.CloseTab(tab)
	fmt.Println(id, "done")
}()

// events needs to be associated to current tab (enable AFTER NewTab)
remote.PageEvents(true)

_ = <-done

// here the page should be ready
// add code to process content or take screenshot

filename := fmt.Sprintf("%d.png", id)
remote.SaveScreenshot(filename, 0644, 0, true)

}

func main() {
var wg sync.WaitGroup

for x := 0; x < 10; x++ {

	// now open new list
	for p := range urlList {
		wg.Add(1)
		go func(page int) {
			id := x*100 + page
			processPage(id, urlList[page])
			wg.Done()
		}(p)
	}

	wg.Wait()
	fmt.Println(x, "------------------------------------------")
}

fmt.Println("DONE.")

}
`

from godet.

yangluoshen avatar yangluoshen commented on August 12, 2024

I am really really sorry for date updates and thanks for your effort.
Everything comes fine while I run chrome on localhost(mac os) (not in docker).
As what you say, I use godet in wrong way, I should create a few tab to connect to chrome if I want to navigate(or capturescreenshot) in parallel。So I try it, and everything seems fine.
Thanks very much!

from godet.

raff avatar raff commented on August 12, 2024

No problem! Happy to help, and I'll add some notes and/or an example.

from godet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.