doc.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // log.Println(err)
  33. // return
  34. // }
  35. // if err := conn.WriteMessage(messageType, p); err != nil {
  36. // log.Println(err)
  37. // return
  38. // }
  39. // }
  40. //
  41. // In above snippet of code, p is a []byte and messageType is an int with value
  42. // websocket.BinaryMessage or websocket.TextMessage.
  43. //
  44. // An application can also send and receive messages using the io.WriteCloser
  45. // and io.Reader interfaces. To send a message, call the connection NextWriter
  46. // method to get an io.WriteCloser, write the message to the writer and close
  47. // the writer when done. To receive a message, call the connection NextReader
  48. // method to get an io.Reader and read until io.EOF is returned. This snippet
  49. // shows how to echo messages using the NextWriter and NextReader methods:
  50. //
  51. // for {
  52. // messageType, r, err := conn.NextReader()
  53. // if err != nil {
  54. // return
  55. // }
  56. // w, err := conn.NextWriter(messageType)
  57. // if err != nil {
  58. // return err
  59. // }
  60. // if _, err := io.Copy(w, r); err != nil {
  61. // return err
  62. // }
  63. // if err := w.Close(); err != nil {
  64. // return err
  65. // }
  66. // }
  67. //
  68. // Data Messages
  69. //
  70. // The WebSocket protocol distinguishes between text and binary data messages.
  71. // Text messages are interpreted as UTF-8 encoded text. The interpretation of
  72. // binary messages is left to the application.
  73. //
  74. // This package uses the TextMessage and BinaryMessage integer constants to
  75. // identify the two data message types. The ReadMessage and NextReader methods
  76. // return the type of the received message. The messageType argument to the
  77. // WriteMessage and NextWriter methods specifies the type of a sent message.
  78. //
  79. // It is the application's responsibility to ensure that text messages are
  80. // valid UTF-8 encoded text.
  81. //
  82. // Control Messages
  83. //
  84. // The WebSocket protocol defines three types of control messages: close, ping
  85. // and pong. Call the connection WriteControl, WriteMessage or NextWriter
  86. // methods to send a control message to the peer.
  87. //
  88. // Connections handle received close messages by calling the handler function
  89. // set with the SetCloseHandler method and by returning a *CloseError from the
  90. // NextReader, ReadMessage or the message Read method. The default close
  91. // handler sends a close message to the peer.
  92. //
  93. // Connections handle received ping messages by calling the handler function
  94. // set with the SetPingHandler method. The default ping handler sends a pong
  95. // message to the peer.
  96. //
  97. // Connections handle received pong messages by calling the handler function
  98. // set with the SetPongHandler method. The default pong handler does nothing.
  99. // If an application sends ping messages, then the application should set a
  100. // pong handler to receive the corresponding pong.
  101. //
  102. // The control message handler functions are called from the NextReader,
  103. // ReadMessage and message reader Read methods. The default close and ping
  104. // handlers can block these methods for a short time when the handler writes to
  105. // the connection.
  106. //
  107. // The application must read the connection to process close, ping and pong
  108. // messages sent from the peer. If the application is not otherwise interested
  109. // in messages from the peer, then the application should start a goroutine to
  110. // read and discard messages from the peer. A simple example is:
  111. //
  112. // func readLoop(c *websocket.Conn) {
  113. // for {
  114. // if _, _, err := c.NextReader(); err != nil {
  115. // c.Close()
  116. // break
  117. // }
  118. // }
  119. // }
  120. //
  121. // Concurrency
  122. //
  123. // Connections support one concurrent reader and one concurrent writer.
  124. //
  125. // Applications are responsible for ensuring that no more than one goroutine
  126. // calls the write methods (NextWriter, SetWriteDeadline, WriteMessage,
  127. // WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and
  128. // that no more than one goroutine calls the read methods (NextReader,
  129. // SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler)
  130. // concurrently.
  131. //
  132. // The Close and WriteControl methods can be called concurrently with all other
  133. // methods.
  134. //
  135. // Origin Considerations
  136. //
  137. // Web browsers allow Javascript applications to open a WebSocket connection to
  138. // any host. It's up to the server to enforce an origin policy using the Origin
  139. // request header sent by the browser.
  140. //
  141. // The Upgrader calls the function specified in the CheckOrigin field to check
  142. // the origin. If the CheckOrigin function returns false, then the Upgrade
  143. // method fails the WebSocket handshake with HTTP status 403.
  144. //
  145. // If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
  146. // the handshake if the Origin request header is present and the Origin host is
  147. // not equal to the Host request header.
  148. //
  149. // The deprecated package-level Upgrade function does not perform origin
  150. // checking. The application is responsible for checking the Origin header
  151. // before calling the Upgrade function.
  152. //
  153. // Buffers
  154. //
  155. // Connections buffer network input and output to reduce the number
  156. // of system calls when reading or writing messages.
  157. //
  158. // Write buffers are also used for constructing WebSocket frames. See RFC 6455,
  159. // Section 5 for a discussion of message framing. A WebSocket frame header is
  160. // written to the network each time a write buffer is flushed to the network.
  161. // Decreasing the size of the write buffer can increase the amount of framing
  162. // overhead on the connection.
  163. //
  164. // The buffer sizes in bytes are specified by the ReadBufferSize and
  165. // WriteBufferSize fields in the Dialer and Upgrader. The Dialer uses a default
  166. // size of 4096 when a buffer size field is set to zero. The Upgrader reuses
  167. // buffers created by the HTTP server when a buffer size field is set to zero.
  168. // The HTTP server buffers have a size of 4096 at the time of this writing.
  169. //
  170. // The buffer sizes do not limit the size of a message that can be read or
  171. // written by a connection.
  172. //
  173. // Buffers are held for the lifetime of the connection by default. If the
  174. // Dialer or Upgrader WriteBufferPool field is set, then a connection holds the
  175. // write buffer only when writing a message.
  176. //
  177. // Applications should tune the buffer sizes to balance memory use and
  178. // performance. Increasing the buffer size uses more memory, but can reduce the
  179. // number of system calls to read or write the network. In the case of writing,
  180. // increasing the buffer size can reduce the number of frame headers written to
  181. // the network.
  182. //
  183. // Some guidelines for setting buffer parameters are:
  184. //
  185. // Limit the buffer sizes to the maximum expected message size. Buffers larger
  186. // than the largest message do not provide any benefit.
  187. //
  188. // Depending on the distribution of message sizes, setting the buffer size to
  189. // a value less than the maximum expected message size can greatly reduce memory
  190. // use with a small impact on performance. Here's an example: If 99% of the
  191. // messages are smaller than 256 bytes and the maximum message size is 512
  192. // bytes, then a buffer size of 256 bytes will result in 1.01 more system calls
  193. // than a buffer size of 512 bytes. The memory savings is 50%.
  194. //
  195. // A write buffer pool is useful when the application has a modest number
  196. // writes over a large number of connections. when buffers are pooled, a larger
  197. // buffer size has a reduced impact on total memory use and has the benefit of
  198. // reducing system calls and frame overhead.
  199. //
  200. // Compression EXPERIMENTAL
  201. //
  202. // Per message compression extensions (RFC 7692) are experimentally supported
  203. // by this package in a limited capacity. Setting the EnableCompression option
  204. // to true in Dialer or Upgrader will attempt to negotiate per message deflate
  205. // support.
  206. //
  207. // var upgrader = websocket.Upgrader{
  208. // EnableCompression: true,
  209. // }
  210. //
  211. // If compression was successfully negotiated with the connection's peer, any
  212. // message received in compressed form will be automatically decompressed.
  213. // All Read methods will return uncompressed bytes.
  214. //
  215. // Per message compression of messages written to a connection can be enabled
  216. // or disabled by calling the corresponding Conn method:
  217. //
  218. // conn.EnableWriteCompression(false)
  219. //
  220. // Currently this package does not support compression with "context takeover".
  221. // This means that messages must be compressed and decompressed in isolation,
  222. // without retaining sliding window or dictionary state across messages. For
  223. // more details refer to RFC 7692.
  224. //
  225. // Use of compression is experimental and may result in decreased performance.
  226. package websocket