chat

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 13 Imported by: 0

README

Chat Server - A Go Concurrency Learning Project

Go Reference Tests

This chat server accepts TCP connections, and allows users to broadcast messages, change their nickname, and of course, disconnect. You can connect to the chat server using a utility like netcat or telnet.

Usage

One-time Setup

Install the chat server using one of these methods:

  • Run go install github.com/ivanfetch/chatserver/cmd/chatserver@latest
  • Directly downloading a release
  • Building from source, after downloading or cloning this repository, by running make build
Example
  • Run the chat server:
$ chatserver
INFO   [0000] listening for connections on :40001 - press CTRL-c to stop the chat   server
  • Connect to the chat server, and set your nickname:
$ nc localhost 40001
Well hello there!

Anything you type will be sent to all other users of this chat server.
A line that begins with a slash (/) is considered a command - enter /help for a list of valid commands. 
-> [::1]:57715 has joined the chat
/nickname Ivan
-> "[::1]:57715" is now known as "Ivan"
Hello, to anyone else who is connected!
> Hello, to anyone else who is connected!
/quit
You're leaving? Ok - have a nice day. :)

When you are done using it, exit the chat server by pressing CTRL-c.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var GitCommit string = "unknown" // Populated at build time
View Source
var Version string = "development" // Populated at build time

Functions

func RunCLI

func RunCLI() int

RunCLI processes command-line arguments, instantiates a new chat server, calls ListenAndServe, waits for the chat server routines to cleanup and exit, then returns an exit status code.

func RunCLIWithoutWaitingForExit

func RunCLIWithoutWaitingForExit() int

RunCLIWithoutWaitingForExit processes command-line arguments, instantiates a new chat server, calls ListenAndServe, then returns an exit status code without waiting for the chat server routines to exit. This is useful for Go TestScript tests, which can avoid retaining and calling cleanup methods on the chat server.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server holds the TCP listener, configuration, and communication channels used by goroutines.

func NewServer

func NewServer(options ...ServerOption) (*Server, error)

NewServer returns a *Server, including optionally specified configuration. optional parameters can be specified via With*() functional options.

Example
package main

import (
	"fmt"

	chat "github.com/ivanfetch/chatserver"
)

func main() {
	server, err := chat.NewServer(chat.WithDebugLogging(), chat.WithListenAddress(":9999"))
	if err != nil {
		panic(err)
	}
	fmt.Printf("This server has debugging enabled and is listening on %s", server.GetListenAddress())
}
Output:

This server has debugging enabled and is listening on :9999

func NewServerFromArgs

func NewServerFromArgs(args []string) (*Server, error)

NewServerFromArgs returns a type *Server after processing command-line arguments.

Example
server, err := NewServerFromArgs([]string{"--debug-logging", "--listen-address", ":9999"})
if err != nil {
	panic(err)
}
fmt.Printf("This server has debugging enabled and is listening on %s", server.GetListenAddress())
Output:

This server has debugging enabled and is listening on :9999

func (Server) GetListenAddress

func (s Server) GetListenAddress() string

GetListenAddress returns the listen address on which the chat server is listening, or intends to use, depending on whether the server is currently listening for connections.

func (*Server) HasExited

func (s *Server) HasExited() bool

HasExited returns true if the chat-server goroutines have all finished and exited. This is useful to verify cleanup is complete in a non-blocking way.

func (*Server) InitiateShutdown

func (s *Server) InitiateShutdown()

InitiateShutdown starts shutting down goroutines for the chat server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe begins listening for new connections, and starts the connection-and-message-manager.

func (*Server) WaitForExit

func (s *Server) WaitForExit()

WaitForExit waits for the chat server goroutines to finish.

type ServerOption

type ServerOption func(*Server) error

ServerOption uses a function to set fields on a type Server by operating on that type as an argument. This is the "functional options" pattern, and provides optional configuration and minimizes required parameters for the constructor.

func WithDebugLogging

func WithDebugLogging() ServerOption

WithDebugLogging outputs debug logs to standard error. By default, minimal informative log messages are output.

func WithListenAddress

func WithListenAddress(l string) ServerOption

WithListenAddress sets the corresponding field in a type Server.

func WithLogWriter

func WithLogWriter(w io.Writer) ServerOption

WithLogWriter sets the io.Writer where log output is written.

Directories

Path Synopsis
cmd
chatserver command

Jump to

Keyboard shortcuts

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