1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| func handleClient(server *Server, session *AppSession) { remoteAddr := session.conn.RemoteAddr()
fmt.Println("客户端[", session.ID, "]地址:", remoteAddr)
scanner := bufio.NewScanner(session.conn)
split := func(data []byte, atEOF bool) (int, []byte, error) { if atEOF { return 0, nil, errors.New("EOF") } if data[0] != '$' || data[3] != '#' { return 0, nil, errors.New("数据异常") } if len(data) > 4 { length := int16(0) binary.Read(bytes.NewReader(data[1:3]), binary.BigEndian, &length) if int(length)+4 <= len(data) { return int(length) + 4, data[4 : int(length)+4], nil } } return 0, nil, nil }
scanner.Split(split)
for scanner.Scan() { server.OnMessage(session, scanner.Bytes()) }
if err := scanner.Err(); err != nil { fmt.Println("客户端[", session.ID, "]数据接收错误, ", err) session.conn.Close() fmt.Println("客户端[", session.ID, "]连接已关闭!") } }
|