diff --git a/conn.go b/channel.go similarity index 85% rename from conn.go rename to channel.go index 34dd9e7..061047b 100644 --- a/conn.go +++ b/channel.go @@ -12,22 +12,8 @@ import ( "time" log "github.com/golang/glog" - "github.com/gorilla/websocket" ) -// An User Connection -type Connection struct { - SocketID string - Socket *websocket.Conn -} - -// A Channel Subscription -type Subscription struct { - Connection *Connection - Id string - Data string -} - // A Channel type Channel struct { sync.Mutex @@ -37,11 +23,6 @@ type Channel struct { Subscriptions map[string]*Subscription } -// Create a new Subscription -func NewSubscription(conn *Connection, data string) *Subscription { - return &Subscription{Connection: conn, Data: data} -} - // Return true if the channel has at least one subscriber func (c *Channel) IsOccupied() bool { return c.TotalSubscriptions() > 0 @@ -194,13 +175,6 @@ func NewChannel(channelID string) *Channel { return &Channel{ChannelID: channelID, CreatedAt: time.Now(), Subscriptions: make(map[string]*Subscription)} } -// Create a new Subscriber -func NewConnection(socketID string, s *websocket.Conn) *Connection { - log.Infof("Creating a new Subscriber %+v", socketID) - - return &Connection{SocketID: socketID, Socket: s} -} - // Publish a MemberAddedEvent to all subscriptions func (c *Channel) PublishMemberAddedEvent(a *App, data string, subscription *Subscription) { for _, subs := range c.Subscriptions { @@ -248,12 +222,3 @@ func (c *Channel) Publish(a *App, event RawEvent, ignore string) error { return nil } - -// Publish the message to websocket atached to this client -func (conn *Connection) Publish(m interface{}) { - go func() { - if err := conn.Socket.WriteJSON(m); err != nil { - log.Errorf("Error publishing message to connection %+v, %s", conn, err) - } - }() -} diff --git a/connection.go b/connection.go new file mode 100644 index 0000000..f354109 --- /dev/null +++ b/connection.go @@ -0,0 +1,32 @@ +// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package main + +import ( + log "github.com/golang/glog" + "github.com/gorilla/websocket" +) + +// An User Connection +type Connection struct { + SocketID string + Socket *websocket.Conn +} + +// Create a new Subscriber +func NewConnection(socketID string, s *websocket.Conn) *Connection { + log.Infof("Creating a new Subscriber %+v", socketID) + + return &Connection{SocketID: socketID, Socket: s} +} + +// Publish the message to websocket atached to this client +func (conn *Connection) Publish(m interface{}) { + go func() { + if err := conn.Socket.WriteJSON(m); err != nil { + log.Errorf("Error publishing message to connection %+v, %s", conn, err) + } + }() +} diff --git a/conn_test.go b/connection_test.go similarity index 100% rename from conn_test.go rename to connection_test.go diff --git a/subscription.go b/subscription.go new file mode 100644 index 0000000..f8754ee --- /dev/null +++ b/subscription.go @@ -0,0 +1,17 @@ +// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package main + +// A Channel Subscription +type Subscription struct { + Connection *Connection + Id string + Data string +} + +// Create a new Subscription +func NewSubscription(conn *Connection, data string) *Subscription { + return &Subscription{Connection: conn, Data: data} +}