In this lesson, we'll setup the ChatRoom and ChatUser structs.
Let's review the design of the data structures and some of the requirements:
- There is one
ChatRoom
in the app. - The
ChatRoom
must know about all of the active connections. - Each connection is tracked in a
ChatUser
object. - The
ChatRoom
must be able to receive messages from a single connection, and replay it back to the other connections. Otherwise, it will not be a very good chatroom! - When a new connection is established, the
ChatRoom
must be notified of these new connections.
- Find the
ChatRoom
struct inchat.go
.
type ChatRoom struct {
// TODO: populate this
}
- 🌟 Add to the
ChatRoom
struct: * a private memberusers
of typemap[string]*ChatUser
* a private member channelincoming
of typechan string
* a private member channeljoins
of typechan *ChatUser
* a private member channeldisconnects
of typechan string
- 🌟 Initialize all of these data structures in the
NewChatRoom()
constructor.
Stuck on any of the steps above? Ask your TA, or see the solution!
- Find the
ChatUser
struct inchat.go
.
type ChatUser struct {
// TODO: populate this
}
- 🌟 Add to the
ChatUser
struct * a private memberconn
of typenet.Conn
* a private memberdisconnect
of typebool
* a private memberusername
of typestring
* a private member channeloutgoing
of typechan string
* a private memberreader
of type*bufio.Reader
* a private memberwriter
of type*bufio.Writer
- 🌟 Initialize all of these data structures in the
NewChatUser()
constructor. *bufio.NewReader/bufio.NewWriter
should accept theconn
to create thereader
andwriter
variables. *disconnect
should be initially set to false
Stuck on any of the steps above? Ask your TA, or see the solution!
- Don't worry too much about what all of these variables are for right now. As we fill out the rest of the code, you will gain a better understanding of what they will be used for, or you can ask your TA for more information.
You can also view the comments on the structs here.
- Verify the code still runs when you type
go run chat.go
.
Next, proceed to Lesson 4 - where we actually read something from the user socket!