-
Notifications
You must be signed in to change notification settings - Fork 6
/
chat.go
162 lines (135 loc) · 3.52 KB
/
chat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bufio"
"log"
"net"
)
//
// ChatRoom is the main chatroom data structure.
//
// `users` contains connected ChatUser connections.
// `incoming` receives incoming messages from ChatUser connections.
// `joins` receives incoming new ChatUser connections.
// `disconnects` receives disconnect notifications.
//
type ChatRoom struct {
users map[string]*ChatUser
incoming chan string
joins chan *ChatUser
disconnects chan string
}
//
// NewChatRoom() will create a new chatroom.
//
func NewChatRoom() *ChatRoom {
return &ChatRoom{
users: make(map[string]*ChatUser),
incoming: make(chan string),
joins: make(chan *ChatUser),
disconnects: make(chan string),
}
}
func (cr *ChatRoom) ListenForMessages() {}
func (cr *ChatRoom) Logout(username string) {}
func (cr *ChatRoom) Join(conn net.Conn) {
user := NewChatUser(conn)
if user.Login(cr) == nil {
cr.joins <- user
}
}
func (cr *ChatRoom) Broadcast(msg string) {}
//
// ChatUser contains information for the connected user.
//
// `conn` is the socket.
// `disconnect` indicates whether or not the socket is disconnected.
// `username` is the chat username.
// `outgoing` is a channel with all pending outgoing messages
// to be written to the socket.
// `reader` is the buffered socket read stream.
// `writer` is the buffered socket write stream.
//
type ChatUser struct {
conn net.Conn
disconnect bool
username string
outgoing chan string
reader *bufio.Reader
writer *bufio.Writer
}
func NewChatUser(conn net.Conn) *ChatUser {
writer := bufio.NewWriter(conn)
reader := bufio.NewReader(conn)
cu := &ChatUser{
conn: conn,
disconnect: false,
reader: reader,
writer: writer,
outgoing: make(chan string),
}
return cu
}
func (cu *ChatUser) ReadIncomingMessages(chatroom *ChatRoom) {
// TODO: read incoming messages in a loop
}
func (cu *ChatUser) WriteOutgoingMessages(chatroom *ChatRoom) {
// TODO: wait for outgoing messages in a loop, and write them
}
func (cu *ChatUser) Login(chatroom *ChatRoom) error {
// TODO: login the user
var err error
cu.WriteString("Welcome to Jen's chat server!\n")
cu.WriteString("Please enter your username: ")
cu.username, err = cu.ReadLine()
if err != nil {
return err
}
log.Println("User logged in:", cu.username)
cu.WriteString("Welcome, " + cu.username + "\n")
return nil
}
func (cu *ChatUser) ReadLine() (string, error) {
bytes, _, err := cu.reader.ReadLine()
str := string(bytes)
return str, err
}
func (cu *ChatUser) WriteString(msg string) error {
_, err := cu.writer.WriteString(msg)
if err != nil {
return err
}
return cu.writer.Flush()
}
func (cu *ChatUser) Send(msg string) {
// TODO: put a message on the outgoing messages queue
}
func (cu *ChatUser) Close() {
// TODO: close the socket
}
//
// main will create a socket, bind to port 6677,
// and loop while waiting for connections.
//
// When it receives a connection it will pass it to
// `chatroom.Join()`.
//
func main() {
log.Println("Chat server starting!")
chatroom := NewChatRoom()
// This binds the socket to TCP port 6677
// on all interfaces.
listener, err := net.Listen("tcp", ":6677")
chatroom.ListenForMessages()
if err != nil {
log.Fatal("Unable to bind to 6677", err)
}
// Accept a connection, and print out the remote
// address so we know who has connected.
for {
conn, _ := listener.Accept()
log.Println("Connection joined.", conn.RemoteAddr())
// run this in a goroutine so more than one thing
// can connect
go chatroom.Join(conn)
}
}