blob: c64f8c82901a3e17964df24f9b45151d7e6a5deb [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001// Copyright 2019 The Gorilla WebSocket Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package websocket
6
7import (
8 "io"
9 "strings"
10)
11
12// JoinMessages concatenates received messages to create a single io.Reader.
13// The string term is appended to each message. The returned reader does not
14// support concurrent calls to the Read method.
15func JoinMessages(c *Conn, term string) io.Reader {
16 return &joinReader{c: c, term: term}
17}
18
19type joinReader struct {
20 c *Conn
21 term string
22 r io.Reader
23}
24
25func (r *joinReader) Read(p []byte) (int, error) {
26 if r.r == nil {
27 var err error
28 _, r.r, err = r.c.NextReader()
29 if err != nil {
30 return 0, err
31 }
32 if r.term != "" {
33 r.r = io.MultiReader(r.r, strings.NewReader(r.term))
34 }
35 }
36 n, err := r.r.Read(p)
37 if err == io.EOF {
38 err = nil
39 r.r = nil
40 }
41 return n, err
42}