PageRenderTime 31ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/gorilla/websocket/doc.go

https://gitlab.com/willjharmer/kanban
Go | 151 lines | 1 code | 1 blank | 149 comment | 0 complexity | d7518d0424c14f4cd61bd5a9e5b6137d MD5 | raw file
  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 uses
  9. // the Upgrade function from an Upgrader object with a HTTP request handler
  10. // to get a pointer to a Conn:
  11. //
  12. // var upgrader = websocket.Upgrader{
  13. // ReadBufferSize: 1024,
  14. // WriteBufferSize: 1024,
  15. // }
  16. //
  17. // func handler(w http.ResponseWriter, r *http.Request) {
  18. // conn, err := upgrader.Upgrade(w, r, nil)
  19. // if err != nil {
  20. // log.Println(err)
  21. // return
  22. // }
  23. // ... Use conn to send and receive messages.
  24. // }
  25. //
  26. // Call the connection's WriteMessage and ReadMessage methods to send and
  27. // receive messages as a slice of bytes. This snippet of code shows how to echo
  28. // messages using these methods:
  29. //
  30. // for {
  31. // messageType, p, err := conn.ReadMessage()
  32. // if err != nil {
  33. // return
  34. // }
  35. // if err = conn.WriteMessage(messageType, p); err != nil {
  36. // return err
  37. // }
  38. // }
  39. //
  40. // In above snippet of code, p is a []byte and messageType is an int with value
  41. // websocket.BinaryMessage or websocket.TextMessage.
  42. //
  43. // An application can also send and receive messages using the io.WriteCloser
  44. // and io.Reader interfaces. To send a message, call the connection NextWriter
  45. // method to get an io.WriteCloser, write the message to the writer and close
  46. // the writer when done. To receive a message, call the connection NextReader
  47. // method to get an io.Reader and read until io.EOF is returned. This snippet
  48. // snippet shows how to echo messages using the NextWriter and NextReader
  49. // 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 ping and pong messages by invoking a callback
  89. // function set with SetPingHandler and SetPongHandler methods. These callback
  90. // functions can be invoked from the ReadMessage method, the NextReader method
  91. // or from a call to the data message reader returned from NextReader.
  92. //
  93. // Connections handle received close messages by returning an error from the
  94. // ReadMessage method, the NextReader method or from a call to the data message
  95. // reader returned from NextReader.
  96. //
  97. // Concurrency
  98. //
  99. // Connections support one concurrent reader and one concurrent writer.
  100. //
  101. // Applications are responsible for ensuring that no more than one goroutine
  102. // calls the write methods (NextWriter, SetWriteDeadline, WriteMessage,
  103. // WriteJSON) concurrently and that no more than one goroutine calls the read
  104. // methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler,
  105. // SetPingHandler) concurrently.
  106. //
  107. // The Close and WriteControl methods can be called concurrently with all other
  108. // methods.
  109. //
  110. // Read is Required
  111. //
  112. // The application must read the connection to process ping and close messages
  113. // sent from the peer. If the application is not otherwise interested in
  114. // messages from the peer, then the application should start a goroutine to read
  115. // and discard messages from the peer. A simple example is:
  116. //
  117. // func readLoop(c *websocket.Conn) {
  118. // for {
  119. // if _, _, err := c.NextReader(); err != nil {
  120. // c.Close()
  121. // break
  122. // }
  123. // }
  124. // }
  125. //
  126. // Origin Considerations
  127. //
  128. // Web browsers allow Javascript applications to open a WebSocket connection to
  129. // any host. It's up to the server to enforce an origin policy using the Origin
  130. // request header sent by the browser.
  131. //
  132. // The Upgrader calls the function specified in the CheckOrigin field to check
  133. // the origin. If the CheckOrigin function returns false, then the Upgrade
  134. // method fails the WebSocket handshake with HTTP status 403.
  135. //
  136. // If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
  137. // the handshake if the Origin request header is present and not equal to the
  138. // Host request header.
  139. //
  140. // An application can allow connections from any origin by specifying a
  141. // function that always returns true:
  142. //
  143. // var upgrader = websocket.Upgrader{
  144. // CheckOrigin: func(r *http.Request) bool { return true },
  145. // }
  146. //
  147. // The deprecated Upgrade function does not enforce an origin policy. It's the
  148. // application's responsibility to check the Origin header before calling
  149. // Upgrade.
  150. package websocket