Skip to content

dougblack/chatty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

local chat server in go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages