Documentation ¶
Index ¶
- Constants
- func SetDefault(v *Server)
- func WithContext(ctx context.Context, v *Server) context.Context
- type Event
- type EventLog
- type EventStreamReader
- type Server
- func (s *Server) Close()
- func (s *Server) CreateStream(id string) *Stream
- func (s *Server) Publish(id string, event *Event)
- func (s *Server) RemoveStream(id string)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) StreamExists(id string) bool
- func (s *Server) TryPublish(id string, event *Event) bool
- type Stream
- type Subscriber
Constants ¶
const DefaultBufferSize = 1024
DefaultBufferSize size of the queue that holds the streams messages.
Variables ¶
This section is empty.
Functions ¶
func SetDefault ¶
func SetDefault(v *Server)
Types ¶
type Event ¶
type Event struct { // 事件 ID,会成为当前 EventSource 对象的内部属性“最后一个事件 ID”的属性值。 ID []byte // 消息的数据字段。 // 当 EventSource 接收到多个以 data: 开头的连续行时,会将它们连接起来,在它们之间插入一个换行符。 // 末尾的换行符会被删除。 Data []byte // 一个用于标识事件类型的字符串。 // 如果指定了这个字符串,浏览器会将具有指定事件名称的事件分派给相应的监听器; // 网站源代码应该使用 addEventListener() 来监听指定的事件。 // 如果一个消息没有指定事件名称,那么 onmessage 处理程序就会被调用。 Event []byte // 重新连接的时间。 // 如果与服务器的连接丢失,浏览器将等待指定的时间,然后尝试重新连接。 // 这必须是一个整数,以毫秒为单位指定重新连接的时间。 // 如果指定了一个非整数值,该字段将被忽略。 Retry []byte Comment []byte // contains filtered or unexported fields }
Event holds all of the event source fields
type EventStreamReader ¶
type EventStreamReader struct {
// contains filtered or unexported fields
}
EventStreamReader scans an io.Reader looking for EventStream messages.
func NewEventStreamReader ¶
func NewEventStreamReader(eventStream io.Reader, maxBufferSize int) *EventStreamReader
NewEventStreamReader creates an instance of EventStreamReader.
func (*EventStreamReader) ReadEvent ¶
func (e *EventStreamReader) ReadEvent() ([]byte, error)
ReadEvent scans the EventStream for events.
type Server ¶
type Server struct { // Extra headers adding to the HTTP response to each client Headers map[string]string // Sets a ttl that prevents old events from being transmitted EventTTL time.Duration // Specifies the size of the message buffer for each stream BufferSize int // Encodes all data as base64 EncodeBase64 bool // Splits an events data into multiple data: entries SplitData bool // Enables creation of a stream when a client connects AutoStream bool // Enables automatic replay for each new subscriber that connects AutoReplay bool // Specifies the function to run when client subscribe or un-subscribe OnSubscribe func(streamID string, sub *Subscriber) OnUnsubscribe func(streamID string, sub *Subscriber) // contains filtered or unexported fields }
Server Is our main struct
func FromContext ¶
func NewWithCallback ¶
func NewWithCallback(onSubscribe, onUnsubscribe func(streamID string, sub *Subscriber)) *Server
NewWithCallback will create a server and setup defaults with callback function
func (*Server) Close ¶
func (s *Server) Close()
Close shuts down the server, closes all of the streams and connections
func (*Server) CreateStream ¶
CreateStream will create a new stream and register it
func (*Server) Publish ¶
Publish sends a mesage to every client in a streamID. If the stream's buffer is full, it blocks until the message is sent out to all subscribers (but not necessarily arrived the clients), or when the stream is closed.
func (*Server) RemoveStream ¶
RemoveStream will remove a stream
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves new connections with events for a given stream ...
func (*Server) StreamExists ¶
StreamExists checks whether a stream by a given id exists
func (*Server) TryPublish ¶
TryPublish is the same as Publish except that when the operation would cause the call to be blocked, it simply drops the message and returns false. Together with a small BufferSize, it can be useful when publishing the latest message ASAP is more important than reliable delivery.
type Stream ¶
type Stream struct { ID string Eventlog EventLog // Enables replaying of eventlog to newly added subscribers AutoReplay bool // Specifies the function to run when client subscribe or un-subscribe OnSubscribe func(streamID string, sub *Subscriber) OnUnsubscribe func(streamID string, sub *Subscriber) // contains filtered or unexported fields }
Stream ...
type Subscriber ¶
Subscriber ...