Documentation ¶
Overview ¶
Package websocket implements the WebSocket protocol with additional functions.
Examples ¶
Echo server:
package main import ( "github.com/pkgz/websocket" "github.com/go-chi/chi" "net/http" ) func main () { r := chi.NewRouter() wsServer := websocket.Start(context.Background()) r.Get("/ws", wsServer.Handler) wsServer.On("echo", func(c *websocket.Conn, msg *websocket.Message) { c.Emit("echo", msg.Data) }) http.ListenAndServe(":8080", r) }
Websocket with group:
package main import ( "github.com/pkgz/websocket" "github.com/go-chi/chi" "net/http" ) func main () { r := chi.NewRouter() wsServer := websocket.Start(context.Background()) ch := wsServer.NewChannel("test") wsServer.OnConnect(func(c *websocket.Conn) { ch.Add(c) ch.Emit("connection", []byte("new connection come")) }) r.Get("/ws", wsServer.Handler) http.ListenAndServe(":8080", r) }
Index ¶
- Variables
- type Channel
- type Conn
- type HandlerFunc
- type Message
- type Server
- func (s *Server) Channel(id string) *Channel
- func (s *Server) Channels() []string
- func (s *Server) Count() int
- func (s *Server) Emit(name string, data []byte)
- func (s *Server) Handler(w http.ResponseWriter, r *http.Request)
- func (s *Server) IsClosed() bool
- func (s *Server) NewChannel(id string) *Channel
- func (s *Server) On(name string, f HandlerFunc)
- func (s *Server) OnConnect(f func(c *Conn))
- func (s *Server) OnDisconnect(f func(c *Conn))
- func (s *Server) OnMessage(f func(c *Conn, h ws.Header, b []byte))
- func (s *Server) Run(ctx context.Context)
- func (s *Server) SendTo(id string, name string, message *Message) error
- func (s *Server) Shutdown() error
Constants ¶
This section is empty.
Variables ¶
var PingInterval = time.Second * 5
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel represent group of connections (similar to group in socket.io).
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn websocket connection
type HandlerFunc ¶
HandleFunc is a type for handle function all function which has callback have this struct as first element returns pointer to connection its give opportunity to close connection or emit message to exactly this connection.
type Message ¶
Message is a struct for data which sending between application and clients. Name using for matching callback function in On function. Body will be transformed to byte array and returned to callback.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server allow to keep connection list, broadcast channel and callbacks list.
func New ¶ added in v1.2.0
func New() *Server
Create a new websocket server handler with the provided options.
func (*Server) Handler ¶
func (s *Server) Handler(w http.ResponseWriter, r *http.Request)
Handler get upgrade connection to RFC 6455 and starting listener for it.
func (*Server) NewChannel ¶
NewChannel create new channel and proxy channel delConn for handling connection closing.
func (*Server) OnDisconnect ¶
Set function which will be called when new connections come.