Documentation ¶
Index ¶
- Constants
- Variables
- type ArgsUnmarshaler
- type Binary
- type Bytes
- type Client
- func (s Client) BroadcastToRoom(room string, event string, args ...interface{})
- func (c *Client) Close() error
- func (c *Client) Dial(rawurl string, requestHeader http.Header, dialer engine.Dialer, parser Parser) (err error)
- func (c *Client) Emit(nsp string, event string, args ...interface{}) (err error)
- func (s Client) EmitError(arg interface{}) (err error)
- func (s Client) GetHeader(key string) string
- func (s Client) GetQuery() url.Values
- func (s Client) HasBeenClosed() bool
- func (s Client) Join(room string)
- func (s Client) Leave(room string)
- func (s Client) LeaveAll()
- func (s Client) LocalAddr() net.Addr
- func (c *Client) Namespace(nsp string) Namespace
- func (c *Client) OnError(fn func(interface{}))
- func (s Client) RemoteAddr() net.Addr
- func (s Client) SetHeader(key, value string)
- func (c *Client) Sid() string
- type Decoder
- type Encoder
- type MessageType
- type Namespace
- type Packet
- type PacketType
- type Parser
- type Server
- type Socket
Examples ¶
Constants ¶
const (
// Revision is protocol version
Revision = "4"
)
Variables ¶
var ( errors.New("namespace unavailable") )ErrorNamespaceUnavaialble =
var (
WebsocketTransport = engine.WebsocketTransport
)
Functions ¶
This section is empty.
Types ¶
type ArgsUnmarshaler ¶
type ArgsUnmarshaler interface {
UnmarshalArgs(args []reflect.Type, data []byte, bin [][]byte) ([]reflect.Value, error)
}
ArgsUnmarshaler unmarshals func arguments `args` from data and binary (bin, if exists). Decoder should implement ArgsUnmarshaler. For `DefaultParser`, data denotes the data in the 1st Packet (w/ type string), while bin denotes binary data in following packets if available; For `MsgpackParser`, bin is not used since all data are packed in a single Packet; args are acquired from reflection, usually by calling `newCallback(func)`
type Binary ¶
type Binary interface { encoding.BinaryMarshaler encoding.BinaryUnmarshaler }
Binary refers to binary data to be exchanged between socket.io server and client
type Bytes ¶
type Bytes struct {
Data []byte
}
Bytes is default implementation of Binary interface, a helper to transfer `[]byte`
func (Bytes) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler
func (*Bytes) MarshalBinaryTo ¶
MarshalBinaryTo copies data into 'p', implementing msgp.Extension.MarshalBinaryTo
func (*Bytes) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is socket.io client
func NewClient ¶
func NewClient() (c *Client)
NewClient creates a Client instance; use Dial to initialize underlying network
func (Client) BroadcastToRoom ¶
func (*Client) Dial ¶
func (c *Client) Dial(rawurl string, requestHeader http.Header, dialer engine.Dialer, parser Parser) (err error)
Dial connects to a socket.io server represented by `rawurl` and create Client instance on success.
func (Client) EmitError ¶
func (s Client) EmitError(arg interface{}) (err error)
EmitError implements Socket.EmitError
func (Client) HasBeenClosed ¶
func (s Client) HasBeenClosed() bool
func (*Client) OnError ¶
func (c *Client) OnError(fn func(interface{}))
OnError registers fn as error callback
func (Client) RemoteAddr ¶
type Decoder ¶
type Decoder interface { Add(msgType MessageType, data []byte) error Decoded() <-chan *Packet ParseData(p *Packet) (string, []byte, [][]byte, error) ArgsUnmarshaler }
Decoder decodes data into a Packet
type MessageType ¶
type MessageType = engine.MessageType
MessageType is alias of engine.MessageType
const ( // MessageTypeString is alias of engine.MessageTypeString MessageTypeString MessageType = engine.MessageTypeString // MessageTypeBinary is alias of engine.MessageTypeBinary MessageTypeBinary MessageType = engine.MessageTypeBinary )
type Namespace ¶
type Namespace interface { // OnEvent registers event callback: // callback should be a valid function, 1st argument of which could be `socketio.Socket` or omitted; // the event callback would be called when a message received from a client with corresponding event; // upon invocation the corresponding `socketio.Socket` would be supplied if appropriate. OnEvent(event string, callback interface{}) Namespace // chainable // OnConnect registers fn as callback, which would be called when this Namespace is connected by a // client, i.e. upon receiving CONNECT packet (for non-root namespace) or connection establishment // ("/" namespace) OnConnect(fn func(so Socket)) Namespace // chainable // OnDisconnect registers fn as callback, which would be called when this Namespace is disconnected by a // client, i.e. upon receiving DISCONNECT packet or connection lost OnDisconnect(fn func(so Socket)) Namespace // chainable // OnError registers fn as callback, which would be called when error occurs in this Namespace OnError(fn func(so Socket, err ...interface{})) Namespace // chainable }
Namespace is socket.io `namespace` abstraction
type Packet ¶
type Packet struct { Type PacketType `msg:"type" json:"type"` Namespace string `msg:"nsp" json:"nsp"` Data interface{} `msg:"data" json:"data,omitempty"` ID *uint64 `msg:"id" json:"id,omitempty"` // contains filtered or unexported fields }
Packet is message abstraction, representing for data exchanged between socket.io server and client
func (*Packet) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
type PacketType ¶
type PacketType byte
PacketType indicates type of a Packet
const ( PacketTypeConnect PacketType = iota PacketTypeDisconnect PacketTypeEvent PacketTypeAck PacketTypeError PacketTypeBinaryEvent PacketTypeBinaryAck )
func (PacketType) String ¶
func (p PacketType) String() string
type Parser ¶
Parser provides Encoder and Decoder instance, like a factory
var ( // ErrUnknownPacket indicates packet invalid or unknown when parser encoding/decoding data ErrUnknownPacket = errors.New("unknown packet") // DefaultParser is default parser implementation for socket.io, compatible with `socket.io-parser`. DefaultParser Parser = &defaultParser{} // MsgpackParser is msgpack parser implementation for socket.io, compatible with `socket.io-msgpack-parser`. MsgpackParser Parser = &msgpackParser{} )
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is socket.io server implementation
Example ¶
server, _ := NewServer(time.Second*5, time.Second*5, DefaultParser) var onConnect = func(so Socket) { log.Println("connected:", so.RemoteAddr(), so.Sid(), so.Namespace()) go func() { for { <-time.After(time.Second * 2) if err := so.Emit("event", "check it out!", time.Now()); err != nil { log.Println("emit:", err) return } } }() so.Emit("event", "hello world!") } var onDisconnect = func(so Socket) { log.Printf("%v %v %q disconnected", so.Sid(), so.RemoteAddr(), so.Namespace()) } var onError = func(so Socket, err ...interface{}) { log.Println("socket", so.Sid(), so.RemoteAddr(), so.Namespace(), "error:", err) } server.Namespace("/"). OnConnect(onConnect). OnDisconnect(onDisconnect). OnError(onError). OnEvent("message", func(so Socket, data string) { if err := so.Emit("ack", "woot", func(msg string, b *Bytes) { bb, _ := b.MarshalBinary() log.Printf("%s=> %x", msg, bb) }); err != nil { log.Println("emit:", err) } }). OnEvent("binary", func(data interface{}, b Bytes) { bb, _ := b.MarshalBinary() log.Printf("%s <- %x", data, bb) }). OnEvent("foobar", func(data string) (string, string) { log.Println("foobar:", data) return "foo", "bar" }) server.Namespace("/ditto"). OnConnect(func(so Socket) { log.Println("connected:", so.RemoteAddr(), so.Sid(), so.Namespace()) }). OnDisconnect(onDisconnect). OnError(onError). OnEvent("disguise", func(msg interface{}, b Bytes) { bb, _ := b.MarshalBinary() log.Printf("%v: %x", msg, bb) }) server.OnError(func(err error) { log.Printf("server error: %v", err) }) defer server.Close() log.Fatalln(http.ListenAndServe("localhost:8081", server))
Output:
Example (WithMsgpackParser) ¶
server, _ := NewServer(time.Second*5, time.Second*5, MsgpackParser) server.Namespace("/"). OnConnect(func(so Socket) { log.Println("connected:", so.RemoteAddr(), so.Sid(), so.Namespace()) so.Emit("event", "hello world!", time.Now()) }). OnDisconnect(func(so Socket) { log.Printf("%v %v %q disconnected", so.Sid(), so.RemoteAddr(), so.Namespace()) }). OnEvent("message", func(b msgp.Raw, data foobar) { log.Printf("%x %v", b, data) }). OnError(func(so Socket, err ...interface{}) { log.Println("socket", so.Sid(), so.RemoteAddr(), so.Namespace(), "error:", err) }) server.OnError(func(err error) { log.Println("server err:", err) }) defer server.Close() log.Fatalln(http.ListenAndServe("localhost:8081", server))
Output:
func NewServer ¶
func NewServer(interval, timeout time.Duration, parser Parser, oc ...engine.OriginChecker) (server *Server, err error)
NewServer creates a socket.io server instance upon underlying engine.io transport
func (*Server) BroadcastToRoom ¶
type Socket ¶
type Socket interface { GetQuery() url.Values Emit(event string, args ...interface{}) (err error) EmitError(arg interface{}) (err error) Namespace() string RemoteAddr() net.Addr LocalAddr() net.Addr GetHeader(key string) string SetHeader(key, value string) Sid() string HasBeenClosed() bool io.Closer Join(room string) Leave(room string) LeaveAll() BroadcastToRoom(room string, event string, args ...interface{}) }
Socket is abstraction of bidirectional socket.io connection