我有代碼(我使用 https://github.com/fiorix/go-smpp):// -----------------------------------------------// handleConnection new clients.// -----------------------------------------------func (_srv *ServerSmpp) handleConnection(_cfg *ConfigSmpp, c *conn) { defer c.Close() if err := _srv.auth(_cfg, c); err != nil { if err != io.EOF { log.Printf("smpp_server: server auth failed: %s\n", err) } return } notify := make(chan error) go func() { for { pb, err := c.Read() if err != nil { notify <- err return } err = _srv.Handler(_srv.RemoteProvider, c, pb) if err != nil { fmt.Printf("%s\n", err) notify <- err return } } }() for { select { case err:= <-notify: if io.EOF == err { fmt.Printf("Smpp server (read): %s\n", err) return } case <-time.After(time.Second * 10): fmt.Printf("Client disconnected by timeout.\n") return } }}調(diào)用的句柄連接的代碼:func (_srv *ServerSmpp) Serve(_cfg *ConfigSmpp) { for { client, err := _srv.NetListener.Accept() if err != nil { break } c := newConn(client) go _srv.handleConnection(_cfg, c) }}當(dāng)此代碼工作時,服務(wù)器會在超時 10 秒之前斷開所有客戶端的連接,但是當(dāng)客戶端不起作用 10 秒時,我該如何斷開連接呢?
如果客戶端在 10 秒內(nèi)未響應(yīng),如何通過超時關(guān)閉連接?
慕標(biāo)琳琳
2022-08-01 09:54:24