From 533dd6f9e8e99d8670f070158a2136c93e5fe3c5 Mon Sep 17 00:00:00 2001 From: Gennadii Kovalev Date: Fri, 27 May 2016 14:53:45 +0200 Subject: [PATCH] server. list does not return error. amount of channels in room added. --- README.md | 6 ++++-- server.go | 32 +++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fdc3f03..b3ceee3 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,10 @@ var socket = io('ws://yourdomain.com', {transports: ['websocket']}); c.Join("room name") //of course, you can list the clients in the room, or account them - channels, _ := c.List(data.Channel) - log.Println(len(channels), "clients in room") + channels := c.List(data.Channel) + //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 //you can omit function args if you do not need them diff --git a/server.go b/server.go index bb7adee..569a8b7 100644 --- a/server.go +++ b/server.go @@ -106,12 +106,34 @@ func (c *Channel) Leave(room string) error { 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 */ -func (c *Channel) List(room string) ([]*Channel, error) { +func (c *Channel) List(room string) []*Channel { if c.server == nil { - return nil, ErrorServerNotSet + return []*Channel{} } 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 */ -func (s *Server) List(room string) ([]*Channel, error) { +func (s *Server) List(room string) []*Channel { s.channelsLock.RLock() defer s.channelsLock.RUnlock() roomChannels, ok := s.channels[room] if !ok { - return []*Channel{}, nil + return []*Channel{} } i := 0 @@ -136,7 +158,7 @@ func (s *Server) List(room string) ([]*Channel, error) { i++ } - return roomChannelsCopy, nil + return roomChannelsCopy }