-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Package wsutil exports tools and helpers to simplify the work with WebSocket | ||
protocol. The main purpose of this subpackage is to provide helpers that are | ||
easy to use as possible. | ||
Overview | ||
// Read masked text message from peer and check utf8 encoding. | ||
header, err := ws.ReadHeader(conn) | ||
if err != nil { | ||
// handle err | ||
} | ||
// Preapre to read payload. | ||
r := io.LimitReader(conn, header.Length) | ||
r = wsutil.NewCipherReader(r, header.Mask) | ||
r = wsutil.NewUTF8Reader(r) | ||
payload, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
// handle err | ||
} | ||
You could get the same behavior using just `wsutil.Reader`: | ||
r := wsutil.Reader{ | ||
Source: conn, | ||
CheckUTF8: true, | ||
} | ||
payload, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
// handle err | ||
} | ||
Or even simplest: | ||
payload, err := wsutil.ReadClientText(conn) | ||
if err != nil { | ||
// handle err | ||
} | ||
Pacakge provides also tool for buffered writing: | ||
// Create buffered writer, that will buffer output bytes and send them as | ||
// 128-length fragments (with exception on large writes, see the doc). | ||
writer := wsutil.NewWriterSize(conn, ws.StateServerSide, ws.OpText, 128) | ||
_, err := io.CopyN(writer, rand.Reader, 100) | ||
if err == nil { | ||
err = writer.Flush() | ||
} | ||
if err != nil { | ||
// handle error | ||
} | ||
For more utils and helpers see the documentation. | ||
*/ | ||
package wsutil |