Documentation ¶
Overview ¶
go-socket.io is the implement of socket.io in Go (golang).
It is compatible with node.js implementation.
Index ¶
- Constants
- Variables
- type Attachment
- type BroadcastAdaptor
- type EventHandlerFunc
- type Namespace
- type Server
- func (s *Server) BroadcastTo(room, message string, args ...interface{})
- 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 ¶
Functions ¶
This section is empty.
Types ¶
type Attachment ¶
type Attachment struct { // Data is the ReadWriter of the attachment data. Data io.ReadWriter // contains filtered or unexported fields }
Attachment is an attachment handler used in emit args. All attachments will send as binary in transport layer. When use attachment, make sure use as 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 lets socket join the t room. Join(room string, socket Socket) error // Leave let socket leave the room. Leave(room string, socket Socket) error // Send will send the message with args to room. If ignore is not nil, it won't send to the socket ignore. Send(ignore Socket, room, message string, args ...interface{}) error }
BroadcastAdaptor is the adaptor to handle broadcast.
type EventHandlerFunc ¶
PJS - could have it return more than just an error, if "rmsg" and "rbody" - then emit response? that makes it more like a RPC - call a func get back a response
type Namespace ¶
type Namespace interface { // Name returns the name of namespace. Name() string // Of returns the namespace with given name. Of(name string) Namespace // On registers the function f to handle message. On(message string, f interface{}) error }
Namespace is the name space of 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, server will use ["polling", "websocket"] as default.
func (*Server) BroadcastTo ¶
Server level broadcasts function.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles http request.
func (*Server) SetAdaptor ¶
func (s *Server) SetAdaptor(adaptor BroadcastAdaptor)
SetAdaptor sets the adaptor of broadcast. Default is in-process broadcast implement.
func (*Server) SetAllowRequest ¶
SetAllowRequest sets the middleware function when establish connection. If it return non-nil, connection won't be established. Default will allow all request.
func (*Server) SetAllowUpgrades ¶
SetAllowUpgrades sets whether server allows transport upgrade. Default is true.
func (*Server) SetCookie ¶
SetCookie sets the name of cookie which used by engine.io. Default is "io".
func (*Server) SetMaxConnection ¶
SetMaxConnection sets the max connetion. Default is 1000.
func (*Server) SetNewId ¶
SetNewId sets the callback func to generate new connection id. By default, id is generated from remote addr + current time stamp
func (*Server) SetPingInterval ¶
SetPingInterval sets the interval of ping. Default is 25s.
func (*Server) SetPingTimeout ¶
SetPingTimeout sets the timeout of ping. When time out, server will close connection. Default is 60s.
func (*Server) SetSessionManager ¶
SetSessionsManager sets the sessions as server's session manager. Default sessions is single process manager. You can custom it as load balance.
type Socket ¶
type Socket interface { Id() string // Id returns the session id of socket. Rooms() []string // Rooms returns the rooms name joined now. Request() *http.Request // Request returns the first http request when established connection. On(message string, f interface{}) error // On registers the function f to handle message. OnAny(f interface{}) error // Register a function that will get called on any message Emit(message string, args ...interface{}) error // Emit emits the message with given args. Join(room string) error // Join joins the room. Leave(room string) error // Leave leaves the room. BroadcastTo(room, message string, args ...interface{}) error // BroadcastTo broadcasts the message to the room with given args. }
Socket is the socket object of socket.io.