client

package
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2018 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = cli.Command{
	Name:  "client",
	Usage: "Run a chat client which will connect to a server",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "server",
			Usage: "The address of the server to connect to",
			Value: "localhost:2428",
		},
	},
	Before: func(c *cli.Context) error {
		if c.NArg() < 1 {
			return errors.Errorf("you must provide a name")
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		conn, err := net.Dial("tcp", c.String("server"))
		if err != nil {
			return errors.Wrap(err, "chat: failed to connect to server")
		}

		tr := transports.NewTcpTransport(conn)
		cl := NewClient(c.Args().First(), tr)
		defer cl.Disconnect()

		go func() {
			logrus.Debug("Reading messages from the client")
			for msg := range cl.Messages() {
				logrus.Debug("Printing message from the client")
				fmt.Printf("[%s]: %s\n", msg.Sender(), msg.Message())
			}
			logrus.Debug("Stopped reading messages from the client")
		}()

		scanner := bufio.NewScanner(os.Stdin)
		logrus.Debug("")
		for scanner.Scan() {
			cl.SendMessage(scanner.Text())
		}

		return errors.Wrap(scanner.Err(), "chat: failed to read message")
	},
}

Functions

This section is empty.

Types

type Client

type Client struct {
	Name string
	// contains filtered or unexported fields
}

func NewClient

func NewClient(name string, tr transports.Transport) *Client

func (*Client) Disconnect

func (c *Client) Disconnect() error

func (*Client) Messages

func (c *Client) Messages() <-chan protocol.MessageCommand

func (*Client) SendMessage

func (c *Client) SendMessage(msg string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL