2015-12-09 07:45:11 +00:00
|
|
|
|
## 8.10. 示例: 聊天服務
|
|
|
|
|
|
2015-12-21 09:25:03 +00:00
|
|
|
|
我們用一個聊天服務器來終結本章節的內容,這個程序可以讓一些用戶通過服務器向其它所有用戶廣播文本消息。這個程序中有四種goroutine。main和broadcaster各自是一個goroutine實例,每一個客戶端的連接都會有一個handleConn和clientWriter的goroutine。broadcaster是select用法的不錯的樣例,因爲它需要處理三種不同類型的消息。
|
|
|
|
|
下面演示的main goroutine的工作,是listen和accept(譯註:網絡編程里的概念)從客戶端過來的連接。對每一個連接,程序都會建立一個新的handleConn的goroutine,就像我們在本章開頭的併發的echo服務器里所做的那樣。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
gopl.io/ch8/chat
|
|
|
|
|
func main() {
|
|
|
|
|
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-26 12:05:30 +00:00
|
|
|
|
然後是broadcaster的goroutine。他的內部變量clients會記録當前建立連接的客戶端集合。其記録的內容是每一個客戶端的消息發出channel的"資格"信息。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type client chan<- string // an outgoing message channel
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
entering = make(chan client)
|
|
|
|
|
leaving = make(chan client)
|
|
|
|
|
messages = make(chan string) // all incoming client messages
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func broadcaster() {
|
|
|
|
|
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-26 12:05:30 +00:00
|
|
|
|
broadcaster監聽來自全局的entering和leaving的channel來穫知客戶端的到來和離開事件。當其接收到其中的一個事件時,會更新clients集合,當該事件是離開行爲時,它會關閉客戶端的消息發出channel。broadcaster也會監聽全局的消息channel,所有的客戶端都會向這個channel中發送消息。當broadcaster接收到什麽消息時,就會將其廣播至所有連接到服務端的客戶端。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
2015-12-26 12:05:30 +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) {
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func clientWriter(conn net.Conn, ch <-chan string) {
|
|
|
|
|
for msg := range ch {
|
|
|
|
|
fmt.Fprintln(conn, msg) // NOTE: ignoring network errors
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-12-26 12:05:30 +00:00
|
|
|
|
另外,handleConn爲每一個客戶端創建了一個clientWriter的goroutine來接收向客戶端發出消息channel中發送的廣播消息,併將它們寫入到客戶端的網絡連接。客戶端的讀取方循環會在broadcaster接收到leaving通知併關閉了channel後終止。
|
2015-12-21 06:10:24 +00:00
|
|
|
|
|
2015-12-21 09:25:03 +00:00
|
|
|
|
下面演示的是當服務器有兩個活動的客戶端連接,併且在兩個窗口中運行的情況,使用netcat來聊天:
|
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!
|
|
|
|
|
127.0.0.1:64208: Hi!
|
|
|
|
|
127.0.0.1:64208: Hi!
|
|
|
|
|
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”
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2015-12-21 09:25:03 +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
|
|
|
|
|
2015-12-21 09:25:03 +00:00
|
|
|
|
練習8.12: 使broadcaster能夠將arrival事件通知當前所有的客戶端。爲了達成這個目的,你需要有一個客戶端的集合,併且在entering和leaving的channel中記録客戶端的名字。
|
|
|
|
|
練習8.13: 使聊天服務器能夠斷開空閒的客戶端連接,比如最近五分鐘之後沒有發送任何消息的那些客戶端。提示:可以在其它goroutine中調用conn.Close()來解除Read調用,就像input.Scanner()所做的那樣。
|
|
|
|
|
練習8.14: 脩改聊天服務器的網絡協議這樣每一個客戶端就可以在entering時可以提供它們的名字。將消息前綴由之前的網絡地址改爲這個名字。
|
2015-12-26 12:05:30 +00:00
|
|
|
|
練習8.15: 如果一個客戶端沒有及時地讀取數據可能會導致所有的客戶端被阻塞。脩改broadcaster來跳過一條消息,而不是等待這個客戶端一直到其準備好寫。或者爲每一個客戶端的消息發出channel建立緩衝區,這樣大部分的消息便不會被丟掉;broadcaster應該用一個非阻塞的send向這個channel中發消息。
|