sseclient

package module
v0.0.0-...-949d9fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2019 License: MIT Imports: 4 Imported by: 0

README

sseclient

GoDoc Go Report Card Build Status Coverage Status

Client for Server sent events for Golang

Documentation

Overview

Package sseclient is the client of server side events

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrClientNil = errors.New("sse: client is nil")
	ErrReqNil    = errors.New("sse: req is nil")
	ErrChanNil   = errors.New("sse: channel is nil")
)

Errors

Functions

This section is empty.

Types

type CancelFunc

type CancelFunc func()

CancelFunc is the function use for cancel notify

func Notify

func Notify(client *http.Client, req *http.Request, ch chan *Event) (CancelFunc, error)

Notify does a request and notify to given channel when receive event CancelFunc use to close response body and channel Do not manually close channel

Example
req, err := http.NewRequest(http.MethodGet, "https://dinosaur-facts.firebaseio.com/dinosaurs.json", nil)
if err != nil {
	log.Fatal(err)
}
client := &http.Client{}
eventChan := make(chan *Event)
cancel, err := Notify(client, req, eventChan)
if err != nil {
	log.Fatal(err)
}
go func() {
	// cancel after 10 seconds passed
	time.Sleep(10 * time.Second)
	cancel()
}()
for event := range eventChan {
	log.Printf("%s: %s", event.Type, string(event.Data))
}
Output:

type Event

type Event struct {
	Type string
	Data []byte
}

Event is the event sent from server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL