feat: expose websocket connection constructor
This commit is contained in:
parent
edf8653059
commit
f54949ba3a
63
server.go
63
server.go
|
@ -25,7 +25,8 @@ var (
|
||||||
ErrorConnectionNotFound = errors.New("Connection not found")
|
ErrorConnectionNotFound = errors.New("Connection not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
socket.io server instance
|
socket.io server instance
|
||||||
*/
|
*/
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
@ -42,7 +43,8 @@ type Server struct {
|
||||||
tr transport.Transport
|
tr transport.Transport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Close current channel
|
Close current channel
|
||||||
*/
|
*/
|
||||||
func (c *Channel) Close() {
|
func (c *Channel) Close() {
|
||||||
|
@ -51,7 +53,8 @@ func (c *Channel) Close() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get ip of socket client
|
Get ip of socket client
|
||||||
*/
|
*/
|
||||||
func (c *Channel) Ip() string {
|
func (c *Channel) Ip() string {
|
||||||
|
@ -62,14 +65,16 @@ func (c *Channel) Ip() string {
|
||||||
return c.ip
|
return c.ip
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get request header of this connection
|
Get request header of this connection
|
||||||
*/
|
*/
|
||||||
func (c *Channel) RequestHeader() http.Header {
|
func (c *Channel) RequestHeader() http.Header {
|
||||||
return c.requestHeader
|
return c.requestHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get channel by it's sid
|
Get channel by it's sid
|
||||||
*/
|
*/
|
||||||
func (s *Server) GetChannel(sid string) (*Channel, error) {
|
func (s *Server) GetChannel(sid string) (*Channel, error) {
|
||||||
|
@ -84,7 +89,8 @@ func (s *Server) GetChannel(sid string) (*Channel, error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Join this channel to given room
|
Join this channel to given room
|
||||||
*/
|
*/
|
||||||
func (c *Channel) Join(room string) error {
|
func (c *Channel) Join(room string) error {
|
||||||
|
@ -111,7 +117,8 @@ func (c *Channel) Join(room string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Remove this channel from given room
|
Remove this channel from given room
|
||||||
*/
|
*/
|
||||||
func (c *Channel) Leave(room string) error {
|
func (c *Channel) Leave(room string) error {
|
||||||
|
@ -138,7 +145,8 @@ func (c *Channel) Leave(room string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get amount of channels, joined to given room, using channel
|
Get amount of channels, joined to given room, using channel
|
||||||
*/
|
*/
|
||||||
func (c *Channel) Amount(room string) int {
|
func (c *Channel) Amount(room string) int {
|
||||||
|
@ -149,7 +157,8 @@ func (c *Channel) Amount(room string) int {
|
||||||
return c.server.Amount(room)
|
return c.server.Amount(room)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get amount of channels, joined to given room, using server
|
Get amount of channels, joined to given room, using server
|
||||||
*/
|
*/
|
||||||
func (s *Server) Amount(room string) int {
|
func (s *Server) Amount(room string) int {
|
||||||
|
@ -160,7 +169,8 @@ func (s *Server) Amount(room string) int {
|
||||||
return len(roomChannels)
|
return len(roomChannels)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get list of channels, joined to given room, using channel
|
Get list of channels, joined to given room, using channel
|
||||||
*/
|
*/
|
||||||
func (c *Channel) List(room string) []*Channel {
|
func (c *Channel) List(room string) []*Channel {
|
||||||
|
@ -171,7 +181,8 @@ func (c *Channel) List(room string) []*Channel {
|
||||||
return c.server.List(room)
|
return c.server.List(room)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get list of channels, joined to given room, using server
|
Get list of channels, joined to given room, using server
|
||||||
*/
|
*/
|
||||||
func (s *Server) List(room string) []*Channel {
|
func (s *Server) List(room string) []*Channel {
|
||||||
|
@ -201,7 +212,8 @@ func (c *Channel) BroadcastTo(room, method string, args interface{}) {
|
||||||
c.server.BroadcastTo(room, method, args)
|
c.server.BroadcastTo(room, method, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Broadcast message to all room channels
|
Broadcast message to all room channels
|
||||||
*/
|
*/
|
||||||
func (s *Server) BroadcastTo(room, method string, args interface{}) {
|
func (s *Server) BroadcastTo(room, method string, args interface{}) {
|
||||||
|
@ -220,7 +232,8 @@ func (s *Server) BroadcastTo(room, method string, args interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Broadcast to all clients
|
Broadcast to all clients
|
||||||
*/
|
*/
|
||||||
func (s *Server) BroadcastToAll(method string, args interface{}) {
|
func (s *Server) BroadcastToAll(method string, args interface{}) {
|
||||||
|
@ -234,7 +247,8 @@ func (s *Server) BroadcastToAll(method string, args interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Generate new id for socket.io connection
|
Generate new id for socket.io connection
|
||||||
*/
|
*/
|
||||||
func generateNewId(custom string) string {
|
func generateNewId(custom string) string {
|
||||||
|
@ -247,7 +261,8 @@ func generateNewId(custom string) string {
|
||||||
return buf.String()[:20]
|
return buf.String()[:20]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
On connection system handler, store sid
|
On connection system handler, store sid
|
||||||
*/
|
*/
|
||||||
func onConnectStore(c *Channel) {
|
func onConnectStore(c *Channel) {
|
||||||
|
@ -257,7 +272,8 @@ func onConnectStore(c *Channel) {
|
||||||
c.server.sids[c.Id()] = c
|
c.server.sids[c.Id()] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
On disconnection system handler, clean joins and sid
|
On disconnection system handler, clean joins and sid
|
||||||
*/
|
*/
|
||||||
func onDisconnectCleanup(c *Channel) {
|
func onDisconnectCleanup(c *Channel) {
|
||||||
|
@ -301,7 +317,8 @@ func (s *Server) SendOpenSequence(c *Channel) {
|
||||||
c.out <- protocol.MustEncode(&protocol.Message{Type: protocol.MessageTypeEmpty})
|
c.out <- protocol.MustEncode(&protocol.Message{Type: protocol.MessageTypeEmpty})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Setup event loop for given connection
|
Setup event loop for given connection
|
||||||
*/
|
*/
|
||||||
func (s *Server) SetupEventLoop(conn transport.Connection, remoteAddr string,
|
func (s *Server) SetupEventLoop(conn transport.Connection, remoteAddr string,
|
||||||
|
@ -332,7 +349,8 @@ func (s *Server) SetupEventLoop(conn transport.Connection, remoteAddr string,
|
||||||
s.callLoopEvent(c, OnConnection)
|
s.callLoopEvent(c, OnConnection)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
implements ServeHTTP function from http.Handler
|
implements ServeHTTP function from http.Handler
|
||||||
*/
|
*/
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -345,7 +363,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
s.tr.Serve(w, r)
|
s.tr.Serve(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get amount of current connected sids
|
Get amount of current connected sids
|
||||||
*/
|
*/
|
||||||
func (s *Server) AmountOfSids() int64 {
|
func (s *Server) AmountOfSids() int64 {
|
||||||
|
@ -355,7 +374,8 @@ func (s *Server) AmountOfSids() int64 {
|
||||||
return int64(len(s.sids))
|
return int64(len(s.sids))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Get amount of rooms with at least one channel(or sid) joined
|
Get amount of rooms with at least one channel(or sid) joined
|
||||||
*/
|
*/
|
||||||
func (s *Server) AmountOfRooms() int64 {
|
func (s *Server) AmountOfRooms() int64 {
|
||||||
|
@ -365,7 +385,8 @@ func (s *Server) AmountOfRooms() int64 {
|
||||||
return int64(len(s.channels))
|
return int64(len(s.channels))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Create new socket.io server
|
Create new socket.io server
|
||||||
*/
|
*/
|
||||||
func NewServer(tr transport.Transport) *Server {
|
func NewServer(tr transport.Transport) *Server {
|
||||||
|
|
|
@ -120,12 +120,14 @@ func (wst *WebsocketTransport) HandleConnection(
|
||||||
return &WebsocketConnection{socket, wst}, nil
|
return &WebsocketConnection{socket, wst}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Websocket connection do not require any additional processing
|
Websocket connection do not require any additional processing
|
||||||
*/
|
*/
|
||||||
func (wst *WebsocketTransport) Serve(w http.ResponseWriter, r *http.Request) {}
|
func (wst *WebsocketTransport) Serve(w http.ResponseWriter, r *http.Request) {}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
|
*
|
||||||
Returns websocket connection with default params
|
Returns websocket connection with default params
|
||||||
*/
|
*/
|
||||||
func GetDefaultWebsocketTransport() *WebsocketTransport {
|
func GetDefaultWebsocketTransport() *WebsocketTransport {
|
||||||
|
@ -137,3 +139,7 @@ func GetDefaultWebsocketTransport() *WebsocketTransport {
|
||||||
BufferSize: WsDefaultBufferSize,
|
BufferSize: WsDefaultBufferSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewWebsocketConnection(socket *websocket.Conn, transport *WebsocketTransport) *WebsocketConnection {
|
||||||
|
return &WebsocketConnection{socket, transport}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue