Documentation ¶
Index ¶
- Constants
- func LastEventID(r *http.Request) string
- type BinaryMarshaler
- type Error
- type Stream
- func (s *Stream) Close() error
- func (s *Stream) Flush() error
- func (s *Stream) SetEvent(event string) error
- func (s *Stream) SetID(id int64) error
- func (s *Stream) SetRetry(retry time.Duration) error
- func (s *Stream) WriteBinary(message BinaryMarshaler) error
- func (s *Stream) WriteBytes(data []byte) error
- func (s *Stream) WriteEvent(id int64, event string, data []byte) error
- func (s *Stream) WriteFloat(num float64) error
- func (s *Stream) WriteInt(num int64) error
- func (s *Stream) WriteJSON(v interface{}) error
- func (s *Stream) WriteString(data string) error
- func (s *Stream) WriteText(message TextMarshaler) error
- type TextMarshaler
- type Upgrader
Examples ¶
Constants ¶
View Source
const ( ErrNotFlusher = Error("sse: not an HTTP-flusher") ErrNotHijacker = Error("sse: not an HTTP-hijacker") )
Variables ¶
This section is empty.
Functions ¶
func LastEventID ¶
LastEventID returns a last ID known by user. If it's not presented - empty string will be returnes
Types ¶
type BinaryMarshaler ¶
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
func UpgradeHTTP ¶
Example ¶
package main import ( "net/http" "github.com/cristalhq/sse" ) func main() { http.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) { stream, err := sse.UpgradeHTTP(r, w) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } data := struct { Text string `json:"text"` }{ Text: "hey there", } stream.WriteJSON(data) }) }
Output:
func (*Stream) WriteBinary ¶
func (s *Stream) WriteBinary(message BinaryMarshaler) error
func (*Stream) WriteBytes ¶
func (*Stream) WriteFloat ¶
func (*Stream) WriteString ¶
func (*Stream) WriteText ¶
func (s *Stream) WriteText(message TextMarshaler) error
type TextMarshaler ¶
type Upgrader ¶
Example ¶
package main import ( "log" "net" "github.com/cristalhq/sse" ) func main() { ln, err := net.Listen("tcp", "localhost:8080") if err != nil { panic(err) } var u sse.Upgrader for { conn, err := ln.Accept() if err != nil { log.Printf("accept err: %#v", err) continue } stream, err := u.Upgrade(conn) if err != nil { log.Printf("upgrade err: %#v", err) continue } go func() { defer stream.Close() err := stream.WriteString(`hey there`) if err != nil { log.Printf("send err: %#v", err) } }() } }
Output:
func (Upgrader) UpgradeHTTP ¶
Click to show internal directories.
Click to hide internal directories.