[日常] Go语言圣经-基于select的多路复用习题
练习 8.8: 使用select来改造8.3节中的echo服务器,为其增加超时,这样服务器可以在客户端10秒中没有任何喊话时自动断开连接。
reverb3.go
package main
import (
"bufio"
"fmt"
"log"
"net"
"strings"
"sync"
"time"
)
func main() {
listener, err := net.Listen("tcp", ":8040")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err) // e.g., connection aborted
continue
}
go handleConn(conn) //新建goroutines处理连接
}
}
/*
1.启动一个goroutine,for死循环让他不能断掉
select语句case判断两个channel
一个是10秒后断掉连接
另一个是接收标准输入后发送过来的channel,接收到值后,启动goroutinue输出
2.for循环接收标准输入,接收到后发送给message的channel
*/
func handleConn(c net.Conn) {
input := bufio.NewScanner(c)
var wg sync.WaitGroup
var message = make(chan string)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-time.After(10 * time.Second):
c.Close()
case mes := <-message:
wg.Add(1)
go func(c net.Conn, shout string, delay time.Duration) {
defer wg.Done()
fmt.Fprintln(c, "\t", strings.ToUpper(shout))
time.Sleep(delay)
fmt.Fprintln(c, "\t", shout)
time.Sleep(delay)
fmt.Fprintln(c, "\t", strings.ToLower(shout))
//ch<-struct{}{}
}(c, mes, 1*time.Second)
}
}
}()
for input.Scan() {
text := input.Text()
message <- text
}
wg.Wait()
//cw := c.(*net.TCPConn)
//cw.CloseWrite()
c.Close()
}
十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》
一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。
开源地址:唯一客服(开源学习版)
官网地址:唯一客服官网
浙公网安备 33010602011771号