Example #1
0
func (c *conn) encode(msg common.Message) []byte {
	name := escaper.Replace(msg.Name())
	id := escaper.Replace(msg.Id())
	namespace := escaper.Replace(msg.Namespace())
	data := msg.Data()

	totalsize := len(name) + len(id) + len(namespace) + len(data) + 3
	buf := make([]byte, totalsize)

	// event name
	n := copy(buf, name)
	buf[n] = ':'
	n++

	// id
	n += copy(buf[n:], id)
	buf[n] = ':'
	n++

	// namespace
	n += copy(buf[n:], namespace)
	buf[n] = ':'
	n++

	// data
	copy(buf[n:], data)
	return buf
}
Example #2
0
func (c *conn) Send(msg common.Message) error {
	buf := new(bytes.Buffer)
	err := sse.Encode(buf, sse.Event{
		Event: msg.Name(),
		Id:    msg.Id(),
		Data:  msg.Data(),
	})
	if err != nil {
		return err
	}
	return c.socket.WriteMessage(websocket.TextMessage, buf.Bytes())
}