|
6 | 6 | //
|
7 | 7 | // Overview
|
8 | 8 | //
|
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: |
12 | 12 | //
|
13 | 13 | // func handler(w http.ResponseWriter, r *http.Request) {
|
14 | 14 | // conn, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
|
|
22 | 22 | // ... Use conn to send and receive messages.
|
23 | 23 | // }
|
24 | 24 | //
|
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: |
35 | 28 | //
|
36 | 29 | // for {
|
37 |
| -// mt, r, err := conn.NextReader() |
| 30 | +// messageType, p, err := conn.ReadMessage() |
38 | 31 | // if err != nil {
|
39 | 32 | // return
|
40 | 33 | // }
|
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 { |
49 | 35 | // return err
|
50 | 36 | // }
|
51 | 37 | // }
|
52 | 38 | //
|
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: |
56 | 49 | //
|
57 | 50 | // for {
|
58 |
| -// mt, p, err := conn.ReadMessage() |
| 51 | +// messageType, r, err := conn.NextReader() |
59 | 52 | // if err != nil {
|
60 | 53 | // return
|
61 | 54 | // }
|
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 { |
63 | 63 | // return err
|
64 | 64 | // }
|
65 | 65 | // }
|
66 | 66 | //
|
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 |
| -// |
74 | 67 | // Data Messages
|
75 | 68 | //
|
76 | 69 | // The WebSocket protocol distinguishes between text and binary data messages.
|
77 | 70 | // Text messages are interpreted as UTF-8 encoded text. The interpretation of
|
78 | 71 | // binary messages is left to the application.
|
79 | 72 | //
|
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. |
83 | 80 | //
|
84 | 81 | // Control Messages
|
85 | 82 | //
|
|
95 | 92 | // Connections handle received close messages by returning an error from the
|
96 | 93 | // ReadMessage method, the NextReader method or from a call to the data message
|
97 | 94 | // 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 | +// |
98 | 103 | package websocket
|
0 commit comments