doc.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 _, ok := err.(websocket.HandshakeError); ok {
  20. // ... an error message already has been sent.
  21. // return
  22. // } else if err != nil {
  23. // log.Println(err)
  24. // return
  25. // }
  26. // ... Use conn to send and receive messages.
  27. // }
  28. //
  29. // Call the connection WriteMessage and ReadMessages methods to send and
  30. // receive messages as a slice of bytes. This snippet of code shows how to echo
  31. // messages using these methods:
  32. //
  33. // for {
  34. // messageType, p, err := conn.ReadMessage()
  35. // if err != nil {
  36. // return
  37. // }
  38. // if err = conn.WriteMessage(messageType, p); err != nil {
  39. // return err
  40. // }
  41. // }
  42. //
  43. // In above snippet of code, p is a []byte and messageType is an int with value
  44. // websocket.BinaryMessage or websocket.TextMessage.
  45. //
  46. // An application can also send and receive messages using the io.WriteCloser
  47. // and io.Reader interfaces. To send a message, call the connection NextWriter
  48. // method to get an io.WriteCloser, write the message to the writer and close
  49. // the writer when done. To receive a message, call the connection NextReader
  50. // method to get an io.Reader and read until io.EOF is returned. This snippet
  51. // snippet shows how to echo messages using the NextWriter and NextReader
  52. // methods:
  53. //
  54. // for {
  55. // messageType, r, err := conn.NextReader()
  56. // if err != nil {
  57. // return
  58. // }
  59. // w, err := conn.NextWriter(messageType)
  60. // if err != nil {
  61. // return err
  62. // }
  63. // if _, err := io.Copy(w, r); err != nil {
  64. // return err
  65. // }
  66. // if err := w.Close(); err != nil {
  67. // return err
  68. // }
  69. // }
  70. //
  71. // Data Messages
  72. //
  73. // The WebSocket protocol distinguishes between text and binary data messages.
  74. // Text messages are interpreted as UTF-8 encoded text. The interpretation of
  75. // binary messages is left to the application.
  76. //
  77. // This package uses the TextMessage and BinaryMessage integer constants to
  78. // identify the two data message types. The ReadMessage and NextReader methods
  79. // return the type of the received message. The messageType argument to the
  80. // WriteMessage and NextWriter methods specifies the type of a sent message.
  81. //
  82. // It is the application's responsibility to ensure that text messages are
  83. // valid UTF-8 encoded text.
  84. //
  85. // Control Messages
  86. //
  87. // The WebSocket protocol defines three types of control messages: close, ping
  88. // and pong. Call the connection WriteControl, WriteMessage or NextWriter
  89. // methods to send a control message to the peer.
  90. //
  91. // Connections handle received ping and pong messages by invoking a callback
  92. // function set with SetPingHandler and SetPongHandler methods. These callback
  93. // functions can be invoked from the ReadMessage method, the NextReader method
  94. // or from a call to the data message reader returned from NextReader.
  95. //
  96. // Connections handle received close messages by returning an error from the
  97. // ReadMessage method, the NextReader method or from a call to the data message
  98. // reader returned from NextReader.
  99. //
  100. // Concurrency
  101. //
  102. // A Conn supports a single concurrent caller to the write methods (NextWriter,
  103. // SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
  104. // methods (NextReader, SetReadDeadline, ReadMessage). The Close and
  105. // WriteControl methods can be called concurrently with all other methods.
  106. //
  107. package websocket