mirror of https://github.com/gorilla/websocket.git
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.
This commit is contained in:
parent
2903ebc236
commit
93b1570a27
83
doc.go
83
doc.go
|
@ -6,9 +6,9 @@
|
||||||
//
|
//
|
||||||
// Overview
|
// Overview
|
||||||
//
|
//
|
||||||
// The Conn type represents a WebSocket connection.
|
// The Conn type represents a WebSocket connection. A server application calls
|
||||||
//
|
// the Upgrade function from an HTTP request handler to get a pointer to a
|
||||||
// A server application calls the Upgrade function to get a pointer to a Conn:
|
// Conn:
|
||||||
//
|
//
|
||||||
// func handler(w http.ResponseWriter, r *http.Request) {
|
// func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
// conn, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
|
// conn, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
|
||||||
|
@ -22,23 +22,37 @@
|
||||||
// ... Use conn to send and receive messages.
|
// ... Use conn to send and receive messages.
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// WebSocket messages are represented by the io.Reader interface when receiving
|
// Call the connection WriteMessage and ReadMessages methods to send and
|
||||||
// a message and by the io.WriteCloser interface when sending a message. An
|
// receive messages as a slice of bytes. This snippet of code shows how to echo
|
||||||
// application receives a message by calling the Conn.NextReader method and
|
// messages using these methods:
|
||||||
// reading the returned io.Reader to EOF. An application sends a message by
|
|
||||||
// calling the Conn.NextWriter method and writing the message to the returned
|
|
||||||
// io.WriteCloser. The application terminates the message by closing the
|
|
||||||
// io.WriteCloser.
|
|
||||||
//
|
|
||||||
// The following example shows how to use the connection NextReader and
|
|
||||||
// NextWriter method to echo messages:
|
|
||||||
//
|
//
|
||||||
// for {
|
// for {
|
||||||
// mt, r, err := conn.NextReader()
|
// messageType, p, err := conn.ReadMessage()
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
// w, err := conn.NextWriter(mt)
|
// if _, err := conn.WriteMessaage(messageType, p); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// In above snippet of code, p is a []byte and messageType is an int with value
|
||||||
|
// websocket.BinaryMessage or websocket.TextMessage.
|
||||||
|
//
|
||||||
|
// An application can also send and receive messages using the io.WriteCloser
|
||||||
|
// and io.Reader interfaces. To send a message, call the connection NextWriter
|
||||||
|
// method to get an io.WriteCloser, write the message to the writer and close
|
||||||
|
// the writer when done. To receive a message, call the connection NextReader
|
||||||
|
// method to get an io.Reader and read until io.EOF is returned. This snippet
|
||||||
|
// snippet shows how to echo messages using the NextWriter and NextReader
|
||||||
|
// methods:
|
||||||
|
//
|
||||||
|
// for {
|
||||||
|
// messageType, r, err := conn.NextReader()
|
||||||
|
// if err != nil {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// w, err := conn.NextWriter(messageType)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return err
|
// return err
|
||||||
// }
|
// }
|
||||||
|
@ -50,36 +64,19 @@
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// The connection ReadMessage and WriteMessage methods are helpers for reading
|
|
||||||
// or writing an entire message in one method call. The following example shows
|
|
||||||
// how to echo messages using these connection helper methods:
|
|
||||||
//
|
|
||||||
// for {
|
|
||||||
// mt, p, err := conn.ReadMessage()
|
|
||||||
// if err != nil {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// if _, err := conn.WriteMessaage(mt, p); err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Concurrency
|
|
||||||
//
|
|
||||||
// A Conn supports a single concurrent caller to the write methods (NextWriter,
|
|
||||||
// SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
|
|
||||||
// methods (NextReader, SetReadDeadline, ReadMessage). The Close and
|
|
||||||
// WriteControl methods can be called concurrently with all other methods.
|
|
||||||
//
|
|
||||||
// Data Messages
|
// Data Messages
|
||||||
//
|
//
|
||||||
// The WebSocket protocol distinguishes between text and binary data messages.
|
// The WebSocket protocol distinguishes between text and binary data messages.
|
||||||
// Text messages are interpreted as UTF-8 encoded text. The interpretation of
|
// Text messages are interpreted as UTF-8 encoded text. The interpretation of
|
||||||
// binary messages is left to the application.
|
// binary messages is left to the application.
|
||||||
//
|
//
|
||||||
// This package uses the same types and methods to work with both types of data
|
// This package uses the TextMessage and BinaryMessage integer constants to
|
||||||
// messages. It is the application's reponsiblity to ensure that text messages
|
// identify the two data message types. The ReadMessage and NextReader methods
|
||||||
// are valid UTF-8 encoded text.
|
// return the type of the received message. The messageType argument to the
|
||||||
|
// WriteMessage and NextWriter methods specifies the type of a sent message.
|
||||||
|
//
|
||||||
|
// It is the application's responsibility to ensure that text messages are
|
||||||
|
// valid UTF-8 encoded text.
|
||||||
//
|
//
|
||||||
// Control Messages
|
// Control Messages
|
||||||
//
|
//
|
||||||
|
@ -95,4 +92,12 @@
|
||||||
// Connections handle received close messages by returning an error from the
|
// Connections handle received close messages by returning an error from the
|
||||||
// ReadMessage method, the NextReader method or from a call to the data message
|
// ReadMessage method, the NextReader method or from a call to the data message
|
||||||
// reader returned from NextReader.
|
// reader returned from NextReader.
|
||||||
|
//
|
||||||
|
// Concurrency
|
||||||
|
//
|
||||||
|
// A Conn supports a single concurrent caller to the write methods (NextWriter,
|
||||||
|
// SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
|
||||||
|
// methods (NextReader, SetReadDeadline, ReadMessage). The Close and
|
||||||
|
// WriteControl methods can be called concurrently with all other methods.
|
||||||
|
//
|
||||||
package websocket
|
package websocket
|
||||||
|
|
|
@ -33,11 +33,11 @@ func (e HandshakeError) Error() string { return e.message }
|
||||||
// responsible for selecting a subprotocol that is acceptable to the client and
|
// responsible for selecting a subprotocol that is acceptable to the client and
|
||||||
// echoing that value back to the client. Use the Subprotocols function to get
|
// echoing that value back to the client. Use the Subprotocols function to get
|
||||||
// the list of protocols specified by the client. Use the
|
// the list of protocols specified by the client. Use the
|
||||||
// Sec-Websocket-Protocol response header to echo the selected protocol back
|
// Sec-Websocket-Protocol response header to echo the selected protocol back to
|
||||||
// to the client.
|
// the client.
|
||||||
//
|
//
|
||||||
// Appilcations can set cookies by adding a Set-Cookie header to the
|
// Appilcations can set cookies by adding a Set-Cookie header to the response
|
||||||
// response header.
|
// header.
|
||||||
//
|
//
|
||||||
// If the request is not a valid WebSocket handshake, then Upgrade returns an
|
// If the request is not a valid WebSocket handshake, then Upgrade returns an
|
||||||
// error of type HandshakeError. Applications should handle this error by
|
// error of type HandshakeError. Applications should handle this error by
|
||||||
|
|
Loading…
Reference in New Issue