Skip to content

Commit 93b1570

Browse files
committed
Improve documentation.
- Introduce ReadMessage and WriteMessage before NextReader and NextWriter in the package comment. It's better to introduce the easy methods first. - Move sections on message of types before the concurrency section.
1 parent 2903ebc commit 93b1570

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

doc.go

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77
// Overview
88
//
9-
// The Conn type represents a WebSocket connection.
10-
//
11-
// A server application calls the Upgrade function to get a pointer to a Conn:
9+
// The Conn type represents a WebSocket connection. A server application calls
10+
// the Upgrade function from an HTTP request handler to get a pointer to a
11+
// Conn:
1212
//
1313
// func handler(w http.ResponseWriter, r *http.Request) {
1414
// conn, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
@@ -22,64 +22,61 @@
2222
// ... Use conn to send and receive messages.
2323
// }
2424
//
25-
// WebSocket messages are represented by the io.Reader interface when receiving
26-
// a message and by the io.WriteCloser interface when sending a message. An
27-
// application receives a message by calling the Conn.NextReader method and
28-
// reading the returned io.Reader to EOF. An application sends a message by
29-
// calling the Conn.NextWriter method and writing the message to the returned
30-
// io.WriteCloser. The application terminates the message by closing the
31-
// io.WriteCloser.
32-
//
33-
// The following example shows how to use the connection NextReader and
34-
// NextWriter method to echo messages:
25+
// Call the connection WriteMessage and ReadMessages methods to send and
26+
// receive messages as a slice of bytes. This snippet of code shows how to echo
27+
// messages using these methods:
3528
//
3629
// for {
37-
// mt, r, err := conn.NextReader()
30+
// messageType, p, err := conn.ReadMessage()
3831
// if err != nil {
3932
// return
4033
// }
41-
// w, err := conn.NextWriter(mt)
42-
// if err != nil {
43-
// return err
44-
// }
45-
// if _, err := io.Copy(w, r); err != nil {
46-
// return err
47-
// }
48-
// if err := w.Close(); err != nil {
34+
// if _, err := conn.WriteMessaage(messageType, p); err != nil {
4935
// return err
5036
// }
5137
// }
5238
//
53-
// The connection ReadMessage and WriteMessage methods are helpers for reading
54-
// or writing an entire message in one method call. The following example shows
55-
// how to echo messages using these connection helper methods:
39+
// In above snippet of code, p is a []byte and messageType is an int with value
40+
// websocket.BinaryMessage or websocket.TextMessage.
41+
//
42+
// An application can also send and receive messages using the io.WriteCloser
43+
// and io.Reader interfaces. To send a message, call the connection NextWriter
44+
// method to get an io.WriteCloser, write the message to the writer and close
45+
// the writer when done. To receive a message, call the connection NextReader
46+
// method to get an io.Reader and read until io.EOF is returned. This snippet
47+
// snippet shows how to echo messages using the NextWriter and NextReader
48+
// methods:
5649
//
5750
// for {
58-
// mt, p, err := conn.ReadMessage()
51+
// messageType, r, err := conn.NextReader()
5952
// if err != nil {
6053
// return
6154
// }
62-
// if _, err := conn.WriteMessaage(mt, p); err != nil {
55+
// w, err := conn.NextWriter(messageType)
56+
// if err != nil {
57+
// return err
58+
// }
59+
// if _, err := io.Copy(w, r); err != nil {
60+
// return err
61+
// }
62+
// if err := w.Close(); err != nil {
6363
// return err
6464
// }
6565
// }
6666
//
67-
// Concurrency
68-
//
69-
// A Conn supports a single concurrent caller to the write methods (NextWriter,
70-
// SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
71-
// methods (NextReader, SetReadDeadline, ReadMessage). The Close and
72-
// WriteControl methods can be called concurrently with all other methods.
73-
//
7467
// Data Messages
7568
//
7669
// The WebSocket protocol distinguishes between text and binary data messages.
7770
// Text messages are interpreted as UTF-8 encoded text. The interpretation of
7871
// binary messages is left to the application.
7972
//
80-
// This package uses the same types and methods to work with both types of data
81-
// messages. It is the application's reponsiblity to ensure that text messages
82-
// are valid UTF-8 encoded text.
73+
// This package uses the TextMessage and BinaryMessage integer constants to
74+
// identify the two data message types. The ReadMessage and NextReader methods
75+
// return the type of the received message. The messageType argument to the
76+
// WriteMessage and NextWriter methods specifies the type of a sent message.
77+
//
78+
// It is the application's responsibility to ensure that text messages are
79+
// valid UTF-8 encoded text.
8380
//
8481
// Control Messages
8582
//
@@ -95,4 +92,12 @@
9592
// Connections handle received close messages by returning an error from the
9693
// ReadMessage method, the NextReader method or from a call to the data message
9794
// reader returned from NextReader.
95+
//
96+
// Concurrency
97+
//
98+
// A Conn supports a single concurrent caller to the write methods (NextWriter,
99+
// SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
100+
// methods (NextReader, SetReadDeadline, ReadMessage). The Close and
101+
// WriteControl methods can be called concurrently with all other methods.
102+
//
98103
package websocket

server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func (e HandshakeError) Error() string { return e.message }
3333
// responsible for selecting a subprotocol that is acceptable to the client and
3434
// echoing that value back to the client. Use the Subprotocols function to get
3535
// the list of protocols specified by the client. Use the
36-
// Sec-Websocket-Protocol response header to echo the selected protocol back
37-
// to the client.
36+
// Sec-Websocket-Protocol response header to echo the selected protocol back to
37+
// the client.
3838
//
39-
// Appilcations can set cookies by adding a Set-Cookie header to the
40-
// response header.
39+
// Appilcations can set cookies by adding a Set-Cookie header to the response
40+
// header.
4141
//
4242
// If the request is not a valid WebSocket handshake, then Upgrade returns an
4343
// error of type HandshakeError. Applications should handle this error by

0 commit comments

Comments
 (0)