good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit 4357cbf9 authored by Anmol Sethi's avatar Anmol Sethi
Browse files

Add WriteOnly example

parent ee1f3c60
No related branches found
No related tags found
No related merge requests found
......@@ -59,3 +59,45 @@ func ExampleDial() {
c.Close(websocket.StatusNormalClosure, "")
}
func ExampleWriteOnly() {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
if err != nil {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
ctx, cancel := context.WithTimeout(r.Context(), time.Minute*10)
defer cancel()
go func() {
defer cancel()
_, _, err := c.Reader(ctx)
if err == nil {
c.Close(websocket.StatusPolicyViolation, "server doesn't accept data messages")
}
}()
t := time.NewTicker(time.Second * 30)
defer t.Stop()
for {
select {
case <-ctx.Done():
c.Close(websocket.StatusNormalClosure, "")
return
case <-t.C:
err = wsjson.Write(ctx, c, "hi")
if err != nil {
log.Println(err)
return
}
}
}
})
err := http.ListenAndServe("localhost:8080", fn)
log.Fatal(err)
}
......@@ -21,6 +21,9 @@ import (
// All methods may be called concurrently except for Reader, Read
// and SetReadLimit.
//
// You must always read from the connection. Otherwise control
// frames will not be handled. See the docs on Reader.
//
// Please be sure to call Close on the connection when you
// are finished with it to release the associated resources.
type Conn struct {
......@@ -299,7 +302,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
// You must read from the connection for close frames to be read.
// If you do not expect any data messages from the peer, just call
// Reader in a separate goroutine and close the connection with StatusPolicyViolation
// when it returns. Example at // TODO
// when it returns. See the WriteOnly example.
//
// Only one Reader may be open at a time.
//
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment