khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1 | // 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 | |
| 5 | package websocket |
| 6 | |
| 7 | import ( |
| 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. |
| 15 | func JoinMessages(c *Conn, term string) io.Reader { |
| 16 | return &joinReader{c: c, term: term} |
| 17 | } |
| 18 | |
| 19 | type joinReader struct { |
| 20 | c *Conn |
| 21 | term string |
| 22 | r io.Reader |
| 23 | } |
| 24 | |
| 25 | func (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 | } |