当前位置: 首页>>代码示例>>Golang>>正文


Golang Element.WriteBytes方法代码示例

本文整理汇总了Golang中github.com/skriptble/nine/element.Element.WriteBytes方法的典型用法代码示例。如果您正苦于以下问题:Golang Element.WriteBytes方法的具体用法?Golang Element.WriteBytes怎么用?Golang Element.WriteBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/skriptble/nine/element.Element的用法示例。


在下文中一共展示了Element.WriteBytes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: WriteElement

// WriteElement converts the element to bytes and writes to the underlying
// tcp connection. This method should generally be used for basic elements such
// as those used during SASL negotiation. WriteStanzas should be used when
// sending stanzas.
func (t *TCP) WriteElement(el element.Element) error {
	var b []byte
	b = el.WriteBytes()
	_, err := t.Write(b)
	return err
}
开发者ID:skriptble,项目名称:nine,代码行数:10,代码来源:tcp.go

示例2: TestNext

func TestNext(t *testing.T) {
	t.Parallel()

	var want, got interface{}
	var err error
	var el element.Element
	pipe1, pipe2 := net.Pipe()
	el = element.New("testing").AddAttr("foo", "bar").
		SetText("random text").
		AddChild(element.New("baz-quux"))
	tcpTsp := NewTCP(pipe1, stream.Receiving, nil, true)

	// Should be able to get a token from the transport
	go func() {
		_, err := pipe2.Write(el.WriteBytes())
		if err != nil {
			t.Errorf("An unexpected error occurred: %s", err)
		}
	}()

	want = el
	got, err = tcpTsp.Next()
	if err != nil {
		t.Errorf("An unexpected error occurred: %s", err)
	}

	if !reflect.DeepEqual(want, got) {
		t.Error("Should be able to get a token from the transport.")
		t.Errorf("\nWant:%+v\nGot :%+v", want, got)
	}

	// Stream element should return token and not attempt to read the entire stream.
	pipe1, pipe2 = net.Pipe()
	tcpTsp = NewTCP(pipe1, stream.Receiving, nil, true)
	go func() {
		_, err := pipe2.Write(stream.Header{}.WriteBytes())
		if err != nil {
			t.Errorf("An unexpected error occurred: %s", err)
		}
		_, err = pipe2.Write([]byte("<foo/></stream:stream>"))
		if err != nil {
			t.Errorf("An unexpected error occurred: %s", err)
		}
	}()
	el, err = tcpTsp.Next()
	if err != nil {
		t.Errorf("An unexpected error occurred: %s", err)
	}
	if el.Space != namespace.Stream || el.Tag != "stream" {
		t.Error("Stream element should return token and not attempt to read the entire stream.")
	}
	got, err = tcpTsp.Next()
	if err != nil {
		t.Errorf("An unexpected error occurred: %s", err)
	}
	want = element.New("foo")
	if !reflect.DeepEqual(want, got) {
		t.Error("Stream element should return token and not attempt to read the entire stream.")
		t.Errorf("\nWant:%+v\nGot :%+v", want, got)
	}
}
开发者ID:skriptble,项目名称:nine,代码行数:61,代码来源:tcp_test.go


注:本文中的github.com/skriptble/nine/element.Element.WriteBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。