Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 1008 Bytes

README.md

File metadata and controls

42 lines (32 loc) · 1008 Bytes

chatty

A local chat server in Go.

Example

Screenshot

Overview

Working on chatty was a nice, small introduction to working with goroutines, channels, and TCP sockets in Go.

Peering at the struct definitions gives us a good idea how the application is structured.

type Server struct {
	Sessions    map[string]*Session
	Port        int
	Listener    *net.TCPListener
	NewMessages chan *Message
	NewSessions chan *Session
}

type Session struct {
	User string
	Conn *net.TCPConn
}

type Message struct {
	User string
	Body string
}

We see that sessions are stored in a map on a Server, and new messages and new sessions are sent over corresponding channels. Session objects pair a username with a net.TCPConn and represent an active chat session. Text entered from any session is wrapped up in a Message and pushed to the Server.NewMessages channel. The Server will then pull it off the Server.NewMessages channel and broadcast it to all other sessions.