2016-02-15 03:06:34 +00:00
|
|
|
|
## 8.10. 示例: 聊天服务
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
我们用一个聊天服务器来终结本章节的内容,这个程序可以让一些用户通过服务器向其它所有用户广播文本消息。这个程序中有四种goroutine。main和broadcaster各自是一个goroutine实例,每一个客户端的连接都会有一个handleConn和clientWriter的goroutine。broadcaster是select用法的不错的样例,因为它需要处理三种不同类型的消息。
|
2016-01-21 02:39:06 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
下面演示的main goroutine的工作,是listen和accept(译注:网络编程里的概念)从客户端过来的连接。对每一个连接,程序都会建立一个新的handleConn的goroutine,就像我们在本章开头的并发的echo服务器里所做的那样。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
2016-01-21 02:39:06 +00:00
|
|
|
|
<u><i>gopl.io/ch8/chat</i></u>
|
2015-12-21 06:10:24 +00:00
|
|
|
|
```go
|
|
|
|
|
func main() {
|
2016-01-21 02:39:06 +00:00
|
|
|
|
listener, err := net.Listen("tcp", "localhost:8000")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
go broadcaster()
|
|
|
|
|
for {
|
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print(err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
go handleConn(conn)
|
|
|
|
|
}
|
2015-12-21 06:10:24 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
然后是broadcaster的goroutine。他的内部变量clients会记录当前建立连接的客户端集合。其记录的内容是每一个客户端的消息发出channel的"资格"信息。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type client chan<- string // an outgoing message channel
|
|
|
|
|
|
|
|
|
|
var (
|
2016-01-21 02:39:06 +00:00
|
|
|
|
entering = make(chan client)
|
|
|
|
|
leaving = make(chan client)
|
|
|
|
|
messages = make(chan string) // all incoming client messages
|
2015-12-21 06:10:24 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func broadcaster() {
|
2016-01-21 02:39:06 +00:00
|
|
|
|
clients := make(map[client]bool) // all connected clients
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case msg := <-messages:
|
|
|
|
|
// Broadcast incoming message to all
|
|
|
|
|
// clients' outgoing message channels.
|
|
|
|
|
for cli := range clients {
|
|
|
|
|
cli <- msg
|
|
|
|
|
}
|
|
|
|
|
case cli := <-entering:
|
|
|
|
|
clients[cli] = true
|
|
|
|
|
|
|
|
|
|
case cli := <-leaving:
|
|
|
|
|
delete(clients, cli)
|
|
|
|
|
close(cli)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-21 06:10:24 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
|
broadcaster监听来自全局的entering和leaving的channel来获知客户端的到来和离开事件。当其接收到其中的一个事件时,会更新clients集合,当该事件是离开行为时,它会关闭客户端的消息发送channel。broadcaster也会监听全局的消息channel,所有的客户端都会向这个channel中发送消息。当broadcaster接收到什么消息时,就会将其广播至所有连接到服务端的客户端。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
|
现在让我们看看每一个客户端的goroutine。handleConn函数会为它的客户端创建一个消息发送channel并通过entering channel来通知客户端的到来。然后它会读取客户端发来的每一行文本,并通过全局的消息channel来将这些文本发送出去,并为每条消息带上发送者的前缀来标明消息身份。当客户端发送完毕后,handleConn会通过leaving这个channel来通知客户端的离开并关闭连接。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func handleConn(conn net.Conn) {
|
2016-01-21 02:39:06 +00:00
|
|
|
|
ch := make(chan string) // outgoing client messages
|
|
|
|
|
go clientWriter(conn, ch)
|
|
|
|
|
|
|
|
|
|
who := conn.RemoteAddr().String()
|
|
|
|
|
ch <- "You are " + who
|
|
|
|
|
messages <- who + " has arrived"
|
|
|
|
|
entering <- ch
|
|
|
|
|
|
|
|
|
|
input := bufio.NewScanner(conn)
|
|
|
|
|
for input.Scan() {
|
|
|
|
|
messages <- who + ": " + input.Text()
|
|
|
|
|
}
|
|
|
|
|
// NOTE: ignoring potential errors from input.Err()
|
|
|
|
|
|
|
|
|
|
leaving <- ch
|
|
|
|
|
messages <- who + " has left"
|
|
|
|
|
conn.Close()
|
2015-12-21 06:10:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func clientWriter(conn net.Conn, ch <-chan string) {
|
2016-01-21 02:39:06 +00:00
|
|
|
|
for msg := range ch {
|
|
|
|
|
fmt.Fprintln(conn, msg) // NOTE: ignoring network errors
|
|
|
|
|
}
|
2015-12-21 06:10:24 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
|
另外,handleConn为每一个客户端创建了一个clientWriter的goroutine,用来接收向客户端发送消息的channel中的广播消息,并将它们写入到客户端的网络连接。客户端的读取循环会在broadcaster接收到leaving通知并关闭了channel后终止。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
下面演示的是当服务器有两个活动的客户端连接,并且在两个窗口中运行的情况,使用netcat来聊天:
|
2016-01-02 13:17:21 +00:00
|
|
|
|
|
2015-12-21 06:10:24 +00:00
|
|
|
|
```
|
|
|
|
|
$ go build gopl.io/ch8/chat
|
|
|
|
|
$ go build gopl.io/ch8/netcat3
|
|
|
|
|
$ ./chat &
|
|
|
|
|
$ ./netcat3
|
|
|
|
|
You are 127.0.0.1:64208 $ ./netcat3
|
|
|
|
|
127.0.0.1:64211 has arrived You are 127.0.0.1:64211
|
|
|
|
|
Hi!
|
2017-08-24 14:29:40 +00:00
|
|
|
|
127.0.0.1:64208: Hi! 127.0.0.1:64208: Hi!
|
2015-12-21 06:10:24 +00:00
|
|
|
|
Hi yourself.
|
|
|
|
|
127.0.0.1:64211: Hi yourself. 127.0.0.1:64211: Hi yourself.
|
|
|
|
|
^C
|
|
|
|
|
127.0.0.1:64208 has left
|
|
|
|
|
$ ./netcat3
|
|
|
|
|
You are 127.0.0.1:64216 127.0.0.1:64216 has arrived
|
|
|
|
|
Welcome.
|
|
|
|
|
127.0.0.1:64211: Welcome. 127.0.0.1:64211: Welcome.
|
|
|
|
|
^C
|
|
|
|
|
127.0.0.1:64211 has left”
|
|
|
|
|
```
|
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
|
当与n个客户端保持聊天session时,这个程序会有2n+2个并发的goroutine,然而这个程序却并不需要显式的锁(§9.2)。clients这个map被限制在了一个独立的goroutine中,broadcaster,所以它不能被并发地访问。多个goroutine共享的变量只有这些channel和net.Conn的实例,两个东西都是并发安全的。我们会在下一章中更多地讲解约束,并发安全以及goroutine中共享变量的含义。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
**练习 8.12:** 使broadcaster能够将arrival事件通知当前所有的客户端。为了达成这个目的,你需要有一个客户端的集合,并且在entering和leaving的channel中记录客户端的名字。
|
2016-01-18 03:14:19 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
**练习 8.13:** 使聊天服务器能够断开空闲的客户端连接,比如最近五分钟之后没有发送任何消息的那些客户端。提示:可以在其它goroutine中调用conn.Close()来解除Read调用,就像input.Scanner()所做的那样。
|
2016-01-18 03:14:19 +00:00
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
|
**练习 8.14:** 修改聊天服务器的网络协议,这样每一个客户端就可以在entering时提供他们的名字。将消息前缀由之前的网络地址改为这个名字。
|
2016-01-18 03:14:19 +00:00
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
|
**练习 8.15:** 如果一个客户端没有及时地读取数据可能会导致所有的客户端被阻塞。修改broadcaster来跳过一条消息,而不是等待这个客户端一直到其准备好读写。或者为每一个客户端的消息发送channel建立缓冲区,这样大部分的消息便不会被丢掉;broadcaster应该用一个非阻塞的send向这个channel中发消息。
|