doc.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 WriteMessage and ReadMessages 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 do not support concurrent calls to the write methods
  100. // (NextWriter, SetWriteDeadline, WriteMessage) or concurrent calls to the read
  101. // methods methods (NextReader, SetReadDeadline, ReadMessage). Connections do
  102. // support a concurrent reader and writer.
  103. //
  104. // The Close and WriteControl methods can be called concurrently with all other
  105. // methods.
  106. //
  107. // Read is Required
  108. //
  109. // The application must read the connection to process ping and close messages
  110. // sent from the peer. If the application is not otherwise interested in
  111. // messages from the peer, then the application should start a goroutine to read
  112. // and discard messages from the peer. A simple example is:
  113. //
  114. // func readLoop(c *websocket.Conn) {
  115. // for {
  116. // if _, _, err := c.NextReader(); err != nil {
  117. // c.Close()
  118. // break
  119. // }
  120. // }
  121. // }
  122. //
  123. // Origin Considerations
  124. //
  125. // Web browsers allow Javascript applications to open a WebSocket connection to
  126. // any host. It's up to the server to enforce an origin policy using the Origin
  127. // request header sent by the browser.
  128. //
  129. // The Upgrader calls the function specified in the CheckOrigin field to check
  130. // the origin. If the CheckOrigin function returns false, then the Upgrade
  131. // method fails the WebSocket handshake with HTTP status 403.
  132. //
  133. // If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
  134. // the handshake if the Origin request header is present and not equal to the
  135. // Host request header.
  136. //
  137. // An application can allow connections from any origin by specifying a
  138. // function that always returns true:
  139. //
  140. // var upgrader = websocket.Upgrader{
  141. // CheckOrigin: func(r *http.Request) bool { return true },
  142. // }
  143. //
  144. // The deprecated Upgrade function does not enforce an origin policy. It's the
  145. // application's responsibility to check the Origin header before calling
  146. // Upgrade.
  147. package websocket