Documentation ¶
Overview ¶
Package comet implements a framework for dealing with WebSockets.
Example ¶
A broadcasting echo server:
func main() { r := gin.Default() m := comet.New() r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleMessage(func(s *comet.Session, msg []byte) { m.Push(msg) }) r.Run(":5000") }
Index ¶
- Constants
- Variables
- func FormatCloseMessage(closeCode int, text string) []byte
- func NewRooms(size uint64) *rooms
- type Comet
- func (c *Comet) Broadcast(msg []byte, rooms ...string) error
- func (c *Comet) BroadcastBinary(msg []byte, rooms ...string) error
- func (c *Comet) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool, rooms ...string) error
- func (c *Comet) BroadcastBinaryOthers(msg []byte, s *Session) error
- func (c *Comet) BroadcastFilter(msg []byte, fn func(*Session) bool, rooms ...string) error
- func (c *Comet) BroadcastOthers(msg []byte, s *Session, rooms ...string) error
- func (c *Comet) Close() error
- func (c *Comet) CloseWithMsg(msg []byte) error
- func (c *Comet) HandleClose(fn func(*Session, int, string) error)
- func (c *Comet) HandleConnect(fn func(*Session))
- func (c *Comet) HandleDisconnect(fn func(*Session))
- func (c *Comet) HandleError(fn func(*Session, error))
- func (c *Comet) HandleMessage(fn func(*Session, []byte))
- func (c *Comet) HandleMessageBinary(fn func(*Session, []byte))
- func (c *Comet) HandlePong(fn func(*Session))
- func (c *Comet) HandleRequest(w http.ResponseWriter, r *http.Request) error
- func (c *Comet) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]interface{}) error
- func (c *Comet) HandleSentMessage(fn func(*Session, []byte))
- func (c *Comet) HandleSentMessageBinary(fn func(*Session, []byte))
- func (c *Comet) IsClosed() bool
- func (c *Comet) Len() int
- type Config
- type LinkList
- func (l *LinkList) Back() *LinkNode
- func (l *LinkList) Front() *LinkNode
- func (l *LinkList) Init() *LinkList
- func (l *LinkList) InsertAfter(v *Session, mark *LinkNode) *LinkNode
- func (l *LinkList) InsertBefore(v *Session, mark *LinkNode) *LinkNode
- func (l *LinkList) Len() int
- func (l *LinkList) MoveAfter(e, mark *LinkNode)
- func (l *LinkList) MoveBefore(e, mark *LinkNode)
- func (l *LinkList) MoveToBack(e *LinkNode)
- func (l *LinkList) MoveToFront(e *LinkNode)
- func (l *LinkList) PushBack(v *Session) *LinkNode
- func (l *LinkList) PushBackList(other *LinkList)
- func (l *LinkList) PushFront(v *Session) *LinkNode
- func (l *LinkList) PushFrontList(other *LinkList)
- func (l *LinkList) Remove(e *LinkNode) interface{}
- type LinkNode
- type MelodyOpt
- type RingBuffer
- func (rb *RingBuffer) Cap() uint64
- func (rb *RingBuffer) Dispose()
- func (rb *RingBuffer) Get(timeouts ...time.Duration) (*envelope, error)
- func (rb *RingBuffer) IsDisposed() bool
- func (rb *RingBuffer) Len() uint64
- func (rb *RingBuffer) Poll(timeout time.Duration) (*envelope, error)
- func (rb *RingBuffer) Put(item *envelope) error
- type Rooms
- type Session
- func (s *Session) Close() error
- func (s *Session) CloseWithMsg(msg []byte) error
- func (s *Session) Get(key string) (value interface{}, exists bool)
- func (s *Session) IsClosed() bool
- func (s *Session) JoinRoom(name string)
- func (s *Session) MustGet(key string) interface{}
- func (s *Session) QuitRoom(name string)
- func (s *Session) Set(key string, value interface{})
- func (s *Session) Write(msg []byte) error
- func (s *Session) WriteBinary(msg []byte) error
Constants ¶
const ( CloseNormalClosure = 1000 CloseGoingAway = 1001 CloseProtocolError = 1002 CloseUnsupportedData = 1003 CloseNoStatusReceived = 1005 CloseAbnormalClosure = 1006 CloseInvalidFramePayloadData = 1007 ClosePolicyViolation = 1008 CloseMessageTooBig = 1009 CloseMandatoryExtension = 1010 CloseInternalServerErr = 1011 CloseServiceRestart = 1012 CloseTryAgainLater = 1013 CloseTLSHandshake = 1015 )
Close codes defined in RFC 6455, section 11.7. Duplicate of codes from gorilla/websocket for convenience.
Variables ¶
var ( // ErrDisposed is returned when an operation is performed on a disposed // queue. ErrDisposed = errors.New(`ring: disposed`) // ErrTimeout is returned when an applicable queue operation times out. ErrTimeout = errors.New(`ring: timed out`) // panic(`Ring buffer in a compromised state during a put operation.`) ErrPanic = errors.New(`ring: panic`) )
Functions ¶
func FormatCloseMessage ¶
FormatCloseMessage formats closeCode and text as a WebSocket close message.
Types ¶
type Comet ¶
type Comet struct { Config *Config Upgrader *websocket.Upgrader // contains filtered or unexported fields }
Comet implements a websocket manager.
func (*Comet) BroadcastBinary ¶
BroadcastBinary broadcasts a binary message to all sessions.
func (*Comet) BroadcastBinaryFilter ¶
BroadcastBinaryFilter broadcasts a binary message to all sessions that fn returns true for.
func (*Comet) BroadcastBinaryOthers ¶
BroadcastBinaryOthers broadcasts a binary message to all sessions except session s.
func (*Comet) BroadcastFilter ¶
BroadcastFilter broadcasts a text message to all sessions that fn returns true for.
func (*Comet) BroadcastOthers ¶
BroadcastOthers broadcasts a text message to all sessions except session s.
func (*Comet) CloseWithMsg ¶
CloseWithMsg closes the comet instance with the given close payload and all connected sessions. Use the FormatCloseMessage function to format a proper close message payload.
func (*Comet) HandleClose ¶
HandleClose sets the handler for close messages received from the session. The code argument to h is the received close code or CloseNoStatusReceived if the close message is empty. The default close handler sends a close frame back to the session.
The application must read the connection to process close messages as described in the section on Control Frames above.
The connection read methods return a CloseError when a close frame is received. Most applications should handle close messages as part of their normal error handling. Applications should only set a close handler when the application must perform some action before sending a close frame back to the session.
func (*Comet) HandleConnect ¶
HandleConnect fires fn when a session connects.
func (*Comet) HandleDisconnect ¶
HandleDisconnect fires fn when a session disconnects.
func (*Comet) HandleError ¶
HandleError fires fn when a session has an error.
func (*Comet) HandleMessage ¶
HandleMessage fires fn when a text message comes in.
func (*Comet) HandleMessageBinary ¶
HandleMessageBinary fires fn when a binary message comes in.
func (*Comet) HandlePong ¶
HandlePong fires fn when a pong is received from a session.
func (*Comet) HandleRequest ¶
HandleRequest upgrades http requests to websocket connections and dispatches them to be handled by the comet instance.
func (*Comet) HandleRequestWithKeys ¶
func (c *Comet) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]interface{}) error
HandleRequestWithKeys does the same as HandleRequest but populates session.keys with keys.
func (*Comet) HandleSentMessage ¶
HandleSentMessage fires fn when a text message is successfully sent.
func (*Comet) HandleSentMessageBinary ¶
HandleSentMessageBinary fires fn when a binary message is successfully sent.
type Config ¶
type Config struct { WriteWait time.Duration // Milliseconds until write times out. PongWait time.Duration // Timeout for waiting on pong. PingPeriod time.Duration // Milliseconds between pings. MaxMessageSize int64 // Maximum size in bytes of a message. MessageBufferSize uint64 // The max amount of messages that can be in a sessions buffer before it starts dropping them. WriteBufferPollSize uint64 Rooms Rooms }
Config comet configuration struct.
type LinkList ¶
type LinkList struct {
// contains filtered or unexported fields
}
LinkList represents a doubly linked list. The zero value for LinkList is an empty list ready to use.
func (*LinkList) InsertAfter ¶
InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*LinkList) InsertBefore ¶
InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*LinkList) MoveAfter ¶
MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*LinkList) MoveBefore ¶
MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*LinkList) MoveToBack ¶
MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*LinkList) MoveToFront ¶
MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*LinkList) PushBack ¶
PushBack inserts a new element e with value v at the back of list l and returns e.
func (*LinkList) PushBackList ¶
PushBackList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.
func (*LinkList) PushFront ¶
PushFront inserts a new element e with value v at the front of list l and returns e.
func (*LinkList) PushFrontList ¶
PushFrontList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.
type LinkNode ¶
type LinkNode struct { // The value stored with this element. Value *Session // contains filtered or unexported fields }
LinkNode is an element of a linked list.
type RingBuffer ¶
type RingBuffer struct {
// contains filtered or unexported fields
}
RingBuffer is a MPMC buffer that achieves threadsafety with CAS operations only. A put on full or get on empty call will block until an item is put or retrieved. Calling Dispose on the RingBuffer will unblock any blocked threads with an error. This buffer is similar to the buffer described here: http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue with some minor additions.
func NewRingBuffer ¶
func NewRingBuffer(size uint64) *RingBuffer
NewRingBuffer will allocate, initialize, and return a ring buffer with the specified size.
func (*RingBuffer) Cap ¶
func (rb *RingBuffer) Cap() uint64
Cap returns the capacity of this ring buffer.
func (*RingBuffer) Dispose ¶
func (rb *RingBuffer) Dispose()
Dispose will dispose of this queue and free any blocked threads in the Put and/or Get methods. Calling those methods on a disposed queue will return an error.
func (*RingBuffer) Get ¶
func (rb *RingBuffer) Get(timeouts ...time.Duration) (*envelope, error)
Get will return the next item in the queue. This call will block if the queue is empty. This call will unblock when an item is added to the queue or Dispose is called on the queue. An error will be returned if the queue is disposed.
func (*RingBuffer) IsDisposed ¶
func (rb *RingBuffer) IsDisposed() bool
IsDisposed will return a bool indicating if this queue has been disposed.
func (*RingBuffer) Len ¶
func (rb *RingBuffer) Len() uint64
Len returns the number of items in the queue.
func (*RingBuffer) Poll ¶
func (rb *RingBuffer) Poll(timeout time.Duration) (*envelope, error)
Poll will return the next item in the queue. This call will block if the queue is empty. This call will unblock when an item is added to the queue, Dispose is called on the queue, or the timeout is reached. An error will be returned if the queue is disposed or a timeout occurs. A non-positive timeout will block indefinitely.
func (*RingBuffer) Put ¶
func (rb *RingBuffer) Put(item *envelope) error
Put adds the provided item to the queue. If the queue is full, this call will block until an item is added to the queue or Dispose is called on the queue. An error will be returned if the queue is disposed.
type Session ¶
Session wrapper around websocket connections.
func (*Session) CloseWithMsg ¶
CloseWithMsg closes the session with the provided payload. Use the FormatCloseMessage function to format a proper close message payload.
func (*Session) Get ¶
Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)
func (*Session) MustGet ¶
MustGet returns the value for the given key if it exists, otherwise it panics.
func (*Session) Set ¶
Set is used to store a new key/value pair exclusivelly for this session. It also lazy initializes s.keys if it was not used previously.
func (*Session) WriteBinary ¶
WriteBinary writes a binary message to session.