client

package
v0.1.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2017 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package client provides a ChatterBox client for communicating with a ChatterBox server.

Usage is simple:

user := "some user"
channel := "some channel"
c, err := New("<host:port>", user)
if err != nil {
  // Do something
}

// Must subscribe before doing anything else.
if err := c.Subscribe("<channel name>"); err != nil {
  // Do something
}

stop := make(chan struct{})
// Receive messages.
go func() {
  for {
    select {
    case <-stop:
      fmt.Println("Exiting channel")
      return
    case m := <-c.Messages:
      // Ignore messages from yourself, we don't have separate panes to display in.
      if m.User == user {
        continue
      }
      fmt.Println("%s: %s", m.User, m.Text.Text)
    }
  }
}()

for {
  reader := bufio.NewReader(os.Stdin)
  fmt.Print(">")
  text, _ := reader.ReadString('\n')
  if text == "exit" {
    fmt.Printf("Exiting comm channel %s\n", channel)
    break
  }
  if err := c.SendText(text); err != nil {
    fmt.Printf("Error: %s, exiting....\n", err)
    break
  }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatterBox

type ChatterBox struct {

	// Messages are text messages arriving from the server to this client.
	Messages chan messages.Server
	// ServerErrors are errors sent from the server to the client.
	ServerErrors chan error
	// UserUpdates are updates to the users who are in the comm channel.
	UserUpdates chan []string
	// ChannelDrop are updates saying we've been unsubscribed to a comm channel.
	ChannelDrop chan struct{}
	// Subscribed is an update saying we've been subscribed to a comm channel.
	Subscribed chan messages.Server
	// Done indicates that the server connection is dead, this client is Done.
	Done chan struct{}
	// contains filtered or unexported fields
}

ChatterBox is a client for the ChatterBox service. You must service all publically available channels or the client might freeze. TODO(johnsiilver): Fix that, its ridiculous.

func New

func New(addr string) (*ChatterBox, error)

New is the constructor for ChatterBox.

func (*ChatterBox) Drop

func (c *ChatterBox) Drop() error

Drop disconnects from a comm channel channel.

func (*ChatterBox) SendText

func (c *ChatterBox) SendText(t string) error

SendText sends a text message to others on our comm channel.

func (*ChatterBox) Subscribe

func (c *ChatterBox) Subscribe(comm, user string) (users []string, err error)

Subscribe to a new comm channel.

Directories

Path Synopsis
chatterbox
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL