server. list does not return error. amount of channels in room added.
This commit is contained in:
parent
ec90f20e1f
commit
533dd6f9e8
|
@ -85,8 +85,10 @@ var socket = io('ws://yourdomain.com', {transports: ['websocket']});
|
||||||
c.Join("room name")
|
c.Join("room name")
|
||||||
|
|
||||||
//of course, you can list the clients in the room, or account them
|
//of course, you can list the clients in the room, or account them
|
||||||
channels, _ := c.List(data.Channel)
|
channels := c.List(data.Channel)
|
||||||
log.Println(len(channels), "clients in room")
|
//or check the amount of clients in room
|
||||||
|
amount := c.Amount(data.Channel)
|
||||||
|
log.Println(amount, "clients in room")
|
||||||
})
|
})
|
||||||
//on disconnection handler, if client hangs connection unexpectedly, it will still occurs
|
//on disconnection handler, if client hangs connection unexpectedly, it will still occurs
|
||||||
//you can omit function args if you do not need them
|
//you can omit function args if you do not need them
|
||||||
|
|
32
server.go
32
server.go
|
@ -106,12 +106,34 @@ func (c *Channel) Leave(room string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Get amount of channels, joined to given room, using channel
|
||||||
|
*/
|
||||||
|
func (c *Channel) Amount(room string) int {
|
||||||
|
if c.server == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.server.Amount(room)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Get amount of channels, joined to given room, using server
|
||||||
|
*/
|
||||||
|
func (s *Server) Amount(room string) int {
|
||||||
|
s.channelsLock.RLock()
|
||||||
|
defer s.channelsLock.RUnlock()
|
||||||
|
|
||||||
|
roomChannels, _ := s.channels[room]
|
||||||
|
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, error) {
|
func (c *Channel) List(room string) []*Channel {
|
||||||
if c.server == nil {
|
if c.server == nil {
|
||||||
return nil, ErrorServerNotSet
|
return []*Channel{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.server.List(room)
|
return c.server.List(room)
|
||||||
|
@ -120,13 +142,13 @@ func (c *Channel) List(room string) ([]*Channel, error) {
|
||||||
/**
|
/**
|
||||||
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, error) {
|
func (s *Server) List(room string) []*Channel {
|
||||||
s.channelsLock.RLock()
|
s.channelsLock.RLock()
|
||||||
defer s.channelsLock.RUnlock()
|
defer s.channelsLock.RUnlock()
|
||||||
|
|
||||||
roomChannels, ok := s.channels[room]
|
roomChannels, ok := s.channels[room]
|
||||||
if !ok {
|
if !ok {
|
||||||
return []*Channel{}, nil
|
return []*Channel{}
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
|
@ -136,7 +158,7 @@ func (s *Server) List(room string) ([]*Channel, error) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return roomChannelsCopy, nil
|
return roomChannelsCopy
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue