Example #1
0
/*
  sendPublicationWithTimeout
*/
func (self *Hub) sendPublicationWithTimeout(conn interfaces.Connection, i interface{}, timeout time.Duration) error {
	select {
	case conn.SendChannel() <- i:
		return nil
	case <-time.After(timeout):
		return errors.New("Send to connection timeout")
	}
}
/*
  ConnectionOrigin
*/
func (self *Hub) ConnectionOrigin(conn interfaces.Connection) (string, error) {
	b, err := self.isRegisteredConnection(conn)
	if err != nil {
		return "error", err
	}
	if !b {
		return "", errors.New("connection is not registered")
	}

	origin := conn.Origin()
	if origin == "" {
		origin = "unknown"
	}
	return origin, nil
}
/*
  ConnectionReference
*/
func (self *Hub) ConnectionReference(conn interfaces.Connection) (string, error) {
	b, err := self.isRegisteredConnection(conn)
	if err != nil {
		return "error", err
	}
	if !b {
		return "", errors.New("connection is not registered")
	}

	ref := conn.Reference()
	if ref == "" {
		ref = "unknown"
	}
	return ref, nil
}
/*
  removeConnection does the house keeping for removing a connection to the hub
*/
func (self *Hub) removeConnection(conn interfaces.Connection) error {
	b, err := self.isRegisteredConnection(conn)
	if err != nil {
		return errors.New("cannot remove connection: " + err.Error())
	}
	if !b {
		return errors.New("connection is not registered")
	}

	conn.SetHub(nil)
	self.removeConnectionSubscriptions(conn)
	delete(self.connections, conn)
	sendChan := conn.SendChannel()
	close(sendChan)
	return nil
}