doc.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // A Conn supports a single concurrent caller to the write methods (NextWriter,
  100. // SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
  101. // methods (NextReader, SetReadDeadline, ReadMessage). The Close and
  102. // WriteControl methods can be called concurrently with all other methods.
  103. //
  104. package websocket