github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/proto/v2/client/goframe_demo.go (about) 1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "github.com/gogo/protobuf/proto" 7 "github.com/qiuhoude/go-web/proto/v2/models" 8 "github.com/smallnest/goframe" 9 "io" 10 "log" 11 "net" 12 "time" 13 ) 14 15 func warpConn(conn net.Conn) goframe.FrameConn { 16 encoderConfig := goframe.EncoderConfig{ 17 ByteOrder: binary.BigEndian, 18 LengthFieldLength: 4, 19 LengthAdjustment: 0, 20 LengthIncludesLengthFieldLength: false, 21 } 22 23 decoderConfig := goframe.DecoderConfig{ 24 ByteOrder: binary.BigEndian, 25 LengthFieldOffset: 0, 26 LengthFieldLength: 4, 27 LengthAdjustment: 0, 28 InitialBytesToStrip: 4, 29 } 30 fc := goframe.NewLengthFieldBasedFrameConn(encoderConfig, decoderConfig, conn) 31 return fc 32 33 } 34 func recvMsg2(fc goframe.FrameConn) { 35 conn := fc.Conn() 36 for { 37 _ = conn.SetReadDeadline(time.Now().Add(3 * time.Minute)) 38 frameData, err := fc.ReadFrame() 39 if err != nil { 40 if err == io.EOF { 41 fmt.Printf("远程链接:%s已经关闭!\n", conn.RemoteAddr().String()) 42 } 43 break 44 } 45 46 basePb := &models.Base{} 47 err = proto.Unmarshal(frameData, basePb) 48 if err != nil { 49 log.Println(err) 50 continue 51 } 52 fmt.Printf("-> %v\n", basePb) 53 } 54 55 }