Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add AutoReconnect option #145

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open

feat: add AutoReconnect option #145

wants to merge 1 commit into from

Conversation

yi-ge
Copy link

@yi-ge yi-ge commented Jan 21, 2023

to: #75

@yi-ge
Copy link
Author

yi-ge commented Jan 21, 2023

Test code:

eventURL := "http://localhost:8000/events.php"
client := sse.NewClient(eventURL)
client.AutoReconnect = true

// disabling ssl verification for self signed certs
client.Connection.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

client.OnConnect(func(c *sse.Client) {
fmt.Println("Connected!")
})

client.OnDisconnect(func(c *sse.Client) {
fmt.Println("Disconnected!")
})

fmt.Println("Registered Server Events!")

err := client.Subscribe("messages", func(msg *sse.Event) {
if string(msg.Event) == "connected" {
	fmt.Println("Connected Event.")
} else if string(msg.Event) == "heartbeat" {
	fmt.Println("Receive heartbeat package.")
}
})

if err != nil {
fmt.Println(err.Error())
}

Console:

┌──(yige㉿kali)-[~/Project/sse-test]
└─$ go run .
Registered Server Events!
Connected!
Connected Event.
Receive heartbeat package.
Disconnected!
Connected!
Connected Event.
Receive heartbeat package.

If AutoReconnect is not set, OnDisconnect of the above instance will never be triggered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant