doc.go 3.9 KB

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