doc.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package websocket implements the WebSocket protocol defined in RFC 6455.
  5. //
  6. // Overview
  7. //
  8. // The Conn type represents a WebSocket connection. A server application calls
  9. // the Upgrader.Upgrade method from an HTTP request handler to get a *Conn:
  10. //
  11. // var upgrader = websocket.Upgrader{
  12. // ReadBufferSize: 1024,
  13. // WriteBufferSize: 1024,
  14. // }
  15. //
  16. // func handler(w http.ResponseWriter, r *http.Request) {
  17. // conn, err := upgrader.Upgrade(w, r, nil)
  18. // if err != nil {
  19. // log.Println(err)
  20. // return
  21. // }
  22. // ... Use conn to send and receive messages.
  23. // }
  24. //
  25. // Call the connection's WriteMessage and ReadMessage 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:
  28. //
  29. // for {
  30. // messageType, p, err := conn.ReadMessage()
  31. // if err != nil {
  32. // return
  33. // }
  34. // if err := conn.WriteMessage(messageType, p); err != nil {
  35. // return err
  36. // }
  37. // }
  38. //
  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. // shows how to echo messages using the NextWriter and NextReader methods:
  48. //
  49. // for {
  50. // messageType, r, err := conn.NextReader()
  51. // if err != nil {
  52. // return
  53. // }
  54. // w, err := conn.NextWriter(messageType)
  55. // if err != nil {
  56. // return err
  57. // }
  58. // if _, err := io.Copy(w, r); err != nil {
  59. // return err
  60. // }
  61. // if err := w.Close(); err != nil {
  62. // return err
  63. // }
  64. // }
  65. //
  66. // Data Messages
  67. //
  68. // The WebSocket protocol distinguishes between text and binary data messages.
  69. // Text messages are interpreted as UTF-8 encoded text. The interpretation of
  70. // binary messages is left to the application.
  71. //
  72. // This package uses the TextMessage and BinaryMessage integer constants to
  73. // identify the two data message types. The ReadMessage and NextReader methods
  74. // return the type of the received message. The messageType argument to the
  75. // WriteMessage and NextWriter methods specifies the type of a sent message.
  76. //
  77. // It is the application's responsibility to ensure that text messages are
  78. // valid UTF-8 encoded text.
  79. //
  80. // Control Messages
  81. //
  82. // The WebSocket protocol defines three types of control messages: close, ping
  83. // and pong. Call the connection WriteControl, WriteMessage or NextWriter
  84. // methods to send a control message to the peer.
  85. //
  86. // Connections handle received close messages by sending a close message to the
  87. // peer and returning a *CloseError from the the NextReader, ReadMessage or the
  88. // message Read method.
  89. //
  90. // Connections handle received ping and pong messages by invoking callback
  91. // functions set with SetPingHandler and SetPongHandler methods. The callback
  92. // functions are called from the NextReader, ReadMessage and the message Read
  93. // methods.
  94. //
  95. // The default ping handler sends a pong to the peer. The application's reading
  96. // goroutine can block for a short time while the handler writes the pong data
  97. // to the connection.
  98. //
  99. // The application must read the connection to process ping, pong and close
  100. // messages sent from the peer. If the application is not otherwise interested
  101. // in messages from the peer, then the application should start a goroutine to
  102. // read and discard messages from the peer. A simple example is:
  103. //
  104. // func readLoop(c *websocket.Conn) {
  105. // for {
  106. // if _, _, err := c.NextReader(); err != nil {
  107. // c.Close()
  108. // break
  109. // }
  110. // }
  111. // }
  112. //
  113. // Concurrency
  114. //
  115. // Connections support one concurrent reader and one concurrent writer.
  116. //
  117. // Applications are responsible for ensuring that no more than one goroutine
  118. // calls the write methods (NextWriter, SetWriteDeadline, WriteMessage,
  119. // WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and
  120. // that no more than one goroutine calls the read methods (NextReader,
  121. // SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler)
  122. // concurrently.
  123. //
  124. // The Close and WriteControl methods can be called concurrently with all other
  125. // methods.
  126. //
  127. // Origin Considerations
  128. //
  129. // Web browsers allow Javascript applications to open a WebSocket connection to
  130. // any host. It's up to the server to enforce an origin policy using the Origin
  131. // request header sent by the browser.
  132. //
  133. // The Upgrader calls the function specified in the CheckOrigin field to check
  134. // the origin. If the CheckOrigin function returns false, then the Upgrade
  135. // method fails the WebSocket handshake with HTTP status 403.
  136. //
  137. // If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
  138. // the handshake if the Origin request header is present and not equal to the
  139. // Host request header.
  140. //
  141. // An application can allow connections from any origin by specifying a
  142. // function that always returns true:
  143. //
  144. // var upgrader = websocket.Upgrader{
  145. // CheckOrigin: func(r *http.Request) bool { return true },
  146. // }
  147. //
  148. // The deprecated package-level Upgrade function does not perform origin
  149. // checking. The application is responsible for checking the Origin header
  150. // before calling the Upgrade function.
  151. //
  152. // Compression EXPERIMENTAL
  153. //
  154. // Per message compression extensions (RFC 7692) are experimentally supported
  155. // by this package in a limited capacity. Setting the EnableCompression option
  156. // to true in Dialer or Upgrader will attempt to negotiate per message deflate
  157. // support.
  158. //
  159. // var upgrader = websocket.Upgrader{
  160. // EnableCompression: true,
  161. // }
  162. //
  163. // If compression was successfully negotiated with the connection's peer, any
  164. // message received in compressed form will be automatically decompressed.
  165. // All Read methods will return uncompressed bytes.
  166. //
  167. // Per message compression of messages written to a connection can be enabled
  168. // or disabled by calling the corresponding Conn method:
  169. //
  170. // conn.EnableWriteCompression(false)
  171. //
  172. // Currently this package does not support compression with "context takeover".
  173. // This means that messages must be compressed and decompressed in isolation,
  174. // without retaining sliding window or dictionary state across messages. For
  175. // more details refer to RFC 7692.
  176. //
  177. // Use of compression is experimental and may result in decreased performance.
  178. package websocket