doc.go 9.8 KB

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