Better file organization.
Moved connection and channel types for its own files.
This commit is contained in:
@@ -12,22 +12,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/golang/glog"
|
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
|
// A Channel
|
||||||
type Channel struct {
|
type Channel struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
@@ -37,11 +23,6 @@ type Channel struct {
|
|||||||
Subscriptions map[string]*Subscription
|
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
|
// Return true if the channel has at least one subscriber
|
||||||
func (c *Channel) IsOccupied() bool {
|
func (c *Channel) IsOccupied() bool {
|
||||||
return c.TotalSubscriptions() > 0
|
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)}
|
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
|
// Publish a MemberAddedEvent to all subscriptions
|
||||||
func (c *Channel) PublishMemberAddedEvent(a *App, data string, subscription *Subscription) {
|
func (c *Channel) PublishMemberAddedEvent(a *App, data string, subscription *Subscription) {
|
||||||
for _, subs := range c.Subscriptions {
|
for _, subs := range c.Subscriptions {
|
||||||
@@ -248,12 +222,3 @@ func (c *Channel) Publish(a *App, event RawEvent, ignore string) error {
|
|||||||
|
|
||||||
return nil
|
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user