Skip to content

Commit

Permalink
wsutil: package doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gobwas committed Jul 10, 2017
1 parent ef8daf1 commit 8d5ebf8
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions wsutil/wsutil.go
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

0 comments on commit 8d5ebf8

Please sign in to comment.