Documentation ¶
Overview ¶
Package websocket implements the WebSocket protocol with additional functions.
Examples ¶
Echo server:
package main import ( "github.com/exelban/websocket" "github.com/go-chi/chi" "net/http" ) func main () { r := chi.NewRouter() wsServer := websocket.CreateAndRun() r.Get("/ws", wsServer.Handler) wsServer.On("echo", func(c *websocket.Conn, msg *websocket.Message) { c.Emit("echo", msg.Body) }) http.ListenAndServe(":8080", r) }
Websocket with group:
package main import ( "github.com/exelban/websocket" "github.com/go-chi/chi" "net/http" ) func main () { r := chi.NewRouter() wsServer := websocket.CreateAndRun() 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 ¶
- type Channel
- type Conn
- type HandlerFunc
- type Message
- type Server
- func (s *Server) Count() int
- func (s *Server) Emit(name string, body interface{})
- func (s *Server) Handler(w http.ResponseWriter, r *http.Request)
- func (s *Server) NewChannel(name 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()
- func (s *Server) Shutdown() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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 ¶
type Message struct { Name string `json:"name"` Body interface{} `json:"body"` }
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 Create ¶
func Create() *Server
Create a new websocket server handler with the provided options.
func CreateAndRun ¶
func CreateAndRun() *Server
CreateAndRun instantly create and run websocket server.
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.