mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-16 11:23:40 +00:00
90 lines
1.6 KiB
Plaintext
90 lines
1.6 KiB
Plaintext
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//!+
|
|
|
|
// Chat is a server that lets clients chat with each other.
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
)
|
|
|
|
//!+broadcaster
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
//!-broadcaster
|
|
|
|
//!+handleConn
|
|
func handleConn(conn net.Conn) {
|
|
ch := make(chan string) // outgoing client messages
|
|
go clientWriter(conn, ch)
|
|
|
|
who := conn.RemoteAddr().String()
|
|
entering <- ch
|
|
messages <- who + " has arrived"
|
|
input := bufio.NewScanner(conn)
|
|
for input.Scan() {
|
|
messages <- who + ": " + input.Text()
|
|
}
|
|
messages <- who + " has left"
|
|
leaving <- ch
|
|
conn.Close()
|
|
}
|
|
|
|
func clientWriter(conn net.Conn, ch <-chan string) {
|
|
for msg := range ch {
|
|
fmt.Fprintln(conn, msg)
|
|
}
|
|
}
|
|
|
|
//!-handleConn
|
|
|
|
//!+main
|
|
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)
|
|
}
|
|
}
|
|
|
|
//!-main
|