Exemplo n.º 1
0
/*发送消息*/
func write(conn net.Conn, command int, message interface{}) {
	w, err := json.Marshal(message)
	if err != nil {
		return
	}
	buff := ace.NewBuffer()
	buff.WriteInt(command)
	buff.WriteBytes(w)
	conn.Write(buff.Bytes())
}
Exemplo n.º 2
0
/*群发*/
func brocast(command int, message interface{}) {
	w, err := json.Marshal(message)
	if err != nil {
		return
	}
	buff := ace.NewBuffer()
	buff.WriteInt(command)
	buff.WriteBytes(w)
	m := UserMap.GetMap()
	for k, _ := range m {
		k.Write(buff.Bytes())
	}
}
Exemplo n.º 3
0
func clientRead(channel *Channel) {
	buffer := make([]byte, 1024)
	buff := ace.NewBuffer()
	for bytelength, readSuccess := channel.Read(buffer); readSuccess; bytelength, readSuccess = channel.Read(buffer) {
		buff.WriteBytes(buffer[0:bytelength])
		/*length := buff.ReadInt()
		if length > buff.Length()-4 {
			buff.Reset()
			continue
		}*/
		fmt.Println("Length:", buff.Length())
		c := buff.ReadInt()
		m := buff.ReadBytes()
		go process(channel.Conn, SocketModel{c, m})
		buff.Clear()
	}
}