Esempio n. 1
0
func TestAppendContent(t *testing.T) {
	var conn net.Conn = nil
	clientObj := NewClient(conn)

	id := 1
	message := "Hello world"
	b := ([]byte)(message)

	idBytes := intAndBytesUtil.IntToBytes(id, byterOrder)

	// 将idByte和b合并
	b = append(idBytes, b...)

	// 获得数组的长度
	contentLength := len(b)

	// 将长度转化为字节数组
	header := intAndBytesUtil.IntToBytes(contentLength, byterOrder)

	// 将头部与内容组合在一起
	content := append(header, b...)

	clientObj.AppendContent(content)

	newId, newContent, ok := clientObj.GetValieMessage()
	if !ok {
		t.Errorf("AppendContent失败,应该有数据")
	} else {
		newMessage := string(newContent)
		if newMessage != message {
			t.Errorf("AppendContent失败,数据错误,newMessage应该为:%s", message)
		}

		if newId != id {
			t.Errorf("AppendContent失败,数据错误,newId应该为:%d", id)
		}
	}

	newId, newContent, ok = clientObj.GetValieMessage()
	if ok {
		t.Errorf("AppendContent失败,不应该有数据")
	}

	if clientObj.HasExpired() {
		t.Errorf("没有这么快过期的")
	}

	time.Sleep(2 * time.Second)
	if clientObj.HasExpired() == false {
		t.Errorf("应该已经过期了")
	}
}
Esempio n. 2
0
// 发送字节数组消息
// id:需要添加到b前发送的数据
// b:待发送的字节数组
func (c *Client) SendByteMessage(id int, b []byte) {
	idBytes := intAndBytesUtil.IntToBytes(id, byterOrder)

	// 将idByte和b合并
	b = append(idBytes, b...)

	// 获得数组的长度
	contentLength := len(b)

	// 将长度转化为字节数组
	header := intAndBytesUtil.IntToBytes(contentLength, byterOrder)

	// 将头部与内容组合在一起
	message := append(header, b...)

	// 发送消息
	c.conn.Write(message)
}
Esempio n. 3
0
// 发送字节数组消息
// b:待发送的字节数组
func (c *Client) SendByteMessage(b []byte) {
	// 获得数组的长度
	contentLength := len(b)

	// 将长度转化为字节数组
	header := intAndBytesUtil.IntToBytes(contentLength, ByterOrder)

	// 将头部与内容组合在一起
	message := append(header, b...)

	// 发送消息
	c.Conn.Write(message)
}