Exemple #1
0
func (c *conn) decode(reader io.Reader) (common.Message, error) {
	buf := bufio.NewReader(reader)
	eventName, err := buf.ReadString(':')
	if err != nil {
		return nil, err
	}
	id, err := buf.ReadString(':')
	if err != nil {
		return nil, err
	}
	namespace, err := buf.ReadString(':')
	if err != nil {
		return nil, err
	}
	content, err := ioutil.ReadAll(buf)
	if err != nil {
		return nil, err
	}

	return common.NewMsg(
		unescaper.Replace(eventName[:len(eventName)-1]),
		unescaper.Replace(id[:len(id)-1]),
		unescaper.Replace(namespace[:len(namespace)-1]),
		content), nil
}
Exemple #2
0
func (c *conn) nextpackage() error {
	msgType, reader, err := c.socket.NextReader()
	if err != nil {
		return err
	}
	if msgType != websocket.TextMessage {
		return nil
	}
	events, err := sse.Decode(reader)
	if err != nil {
		return err
	}
	for _, event := range events {
		c.pending.Enqueue(common.NewMsg(
			event.Event,
			event.Id,
			"",
			event.Data))
	}
	return nil
}
Exemple #3
0
func (c *conn) Emit(eventName string, data []byte) error {
	return c.Send(common.NewMsg(eventName, "", "", data))
}