Documentation ¶
Overview ¶
go-socket.io is a server implementation of socket.io in golang.
It is compatible with the official Node.js implementation.
Index ¶
- Constants
- type Attachment
- type BroadcastAdaptor
- type Namespace
- type Server
- func (s *Server) BroadcastTo(room, message string, args ...interface{})
- func (s *Server) Count() int
- func (s *Server) GetMaxConnection() int
- func (n Server) Name() string
- func (n Server) Of(name string) Namespace
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetAdaptor(adaptor BroadcastAdaptor)
- func (s *Server) SetAllowRequest(f func(*http.Request) error)
- func (s *Server) SetAllowUpgrades(allow bool)
- func (s *Server) SetCookie(prefix string)
- func (s *Server) SetMaxConnection(n int)
- func (s *Server) SetNewId(f func(*http.Request) string)
- func (s *Server) SetPingInterval(t time.Duration)
- func (s *Server) SetPingTimeout(t time.Duration)
- func (s *Server) SetSessionManager(sessions engineio.Sessions)
- type Socket
Constants ¶
const Protocol = 4
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attachment ¶
type Attachment struct { Data io.ReadWriter // contains filtered or unexported fields }
Attachment is an attachment handler used in emit args. All attachments will be sent as binary data in the transport layer. When using an attachment, make sure it is a pointer.
For example:
type Arg struct { Title string `json:"title"` File *Attachment `json:"file"` } f, _ := os.Open("./some_file") arg := Arg{ Title: "some_file", File: &Attachment{ Data: f, } } socket.Emit("send file", arg) socket.On("get file", func(so Socket, arg Arg) { b, _ := ioutil.ReadAll(arg.File.Data) })
func (Attachment) MarshalJSON ¶
func (a Attachment) MarshalJSON() ([]byte, error)
func (*Attachment) UnmarshalJSON ¶
func (a *Attachment) UnmarshalJSON(b []byte) error
type BroadcastAdaptor ¶
type BroadcastAdaptor interface { // Join causes the socket to join a room. Join(room string, socket Socket) error // Leave causes the socket to leave a room. Leave(room string, socket Socket) error // Send will send an event with args to the room. If "ignore" is not nil, the event will be excluded from being sent to "ignore". Send(ignore Socket, room, event string, args ...interface{}) error }
BroadcastAdaptor is the adaptor to handle broadcasts.
type Namespace ¶
type Namespace interface { // Name returns the name of the namespace. Name() string // Of returns the namespace with given name. Of(name string) Namespace // On registers the function f to handle an event. On(event string, f interface{}) error }
Namespace is the name space of a socket.io handler.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the server of socket.io.
func NewServer ¶
NewServer returns the server supported given transports. If transports is nil, the server will use ["polling", "websocket"] as default.
func (*Server) BroadcastTo ¶
BroadcastTo is a server level broadcast function.
func (*Server) GetMaxConnection ¶
GetMaxConnection returns the current max connection
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles http requests.
func (*Server) SetAdaptor ¶
func (s *Server) SetAdaptor(adaptor BroadcastAdaptor)
SetAdaptor sets the adaptor of broadcast. Default is an in-process broadcast implementation.
func (*Server) SetAllowRequest ¶
SetAllowRequest sets the middleware function when a connection is established. If a non-nil value is returned, the connection won't be established. Default will allow all connections.
func (*Server) SetAllowUpgrades ¶
SetAllowUpgrades sets whether server allows transport upgrades. Default is true.
func (*Server) SetCookie ¶
SetCookie sets the name of the cookie used by engine.io. Default is "io".
func (*Server) SetMaxConnection ¶
SetMaxConnection sets the maximum number of connections with clients. Default is 1000.
func (*Server) SetNewId ¶
SetNewId sets the callback func to generate new connection id. By default, id is generated from remote address + current time stamp
func (*Server) SetPingInterval ¶
SetPingInterval sets the interval of pings. Default is 25s.
func (*Server) SetPingTimeout ¶
SetPingTimeout sets the timeout of a connection ping. When it times out, the server will close the connection with the client. Default is 60s.
func (*Server) SetSessionManager ¶
func (s *Server) SetSessionManager(sessions engineio.Sessions)
SetSessionsManager sets the sessions as server's session manager. Default sessions is a single process manager. You can customize it as a load balancer.
type Socket ¶
type Socket interface { // Id returns the session id of socket. Id() string // Rooms returns the rooms name joined now. Rooms() []string // Request returns the first http request when established connection. Request() *http.Request // On registers the function f to handle an event. On(event string, f interface{}) error // Emit emits an event with given args. Emit(event string, args ...interface{}) error // Join joins the room. Join(room string) error // Leave leaves the room. Leave(room string) error // BroadcastTo broadcasts an event to the room with given args. BroadcastTo(room, event string, args ...interface{}) error }
Socket is the socket object of socket.io.