doc.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.
  9. //
  10. // A server application calls the Upgrade function to get a pointer to a Conn:
  11. //
  12. // func handler(w http.ResponseWriter, r *http.Request) {
  13. // conn, err := websocket.Upgrade(w, r.Header, 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. // WebSocket messages are represented by the io.Reader interface when receiving
  25. // a message and by the io.WriteCloser interface when sending a message. An
  26. // application receives a message by calling the Conn.NextReader method and
  27. // reading the returned io.Reader to EOF. An application sends a message by
  28. // calling the Conn.NextWriter method and writing the message to the returned
  29. // io.WriteCloser. The application terminates the message by closing the
  30. // io.WriteCloser.
  31. //
  32. // The following example shows how to use the connection NextReader and
  33. // NextWriter method to echo messages:
  34. //
  35. // for {
  36. // mt, r, err := conn.NextReader()
  37. // if err != nil {
  38. // return
  39. // }
  40. // w, err := conn.NextWriter(mt)
  41. // if err != nil {
  42. // return err
  43. // }
  44. // if _, err := io.Copy(w, r); err != nil {
  45. // return err
  46. // }
  47. // if err := w.Close(); err != nil {
  48. // return err
  49. // }
  50. // }
  51. //
  52. // The connection ReadMessage and WriteMessage methods are helpers for reading
  53. // or writing an entire message in one method call. The following example shows
  54. // how to echo messages using these connection helper methods:
  55. //
  56. // for {
  57. // mt, p, err := conn.ReadMessage()
  58. // if err != nil {
  59. // return
  60. // }
  61. // if _, err := conn.WriteMessaage(mt, p); err != nil {
  62. // return err
  63. // }
  64. // }
  65. //
  66. // Concurrency
  67. //
  68. // A Conn supports a single concurrent caller to the write methods (NextWriter,
  69. // SetWriteDeadline, WriteMessage) and a single concurrent caller to the read
  70. // methods (NextReader, SetReadDeadline, ReadMessage). The Close and
  71. // WriteControl methods can be called concurrently with all other methods.
  72. //
  73. // Data Messages
  74. //
  75. // The WebSocket protocol distinguishes between text and binary data messages.
  76. // Text messages are interpreted as UTF-8 encoded text. The interpretation of
  77. // binary messages is left to the application.
  78. //
  79. // This package uses the same types and methods to work with both types of data
  80. // messages. It is the application's reponsiblity to ensure that text messages
  81. // are valid UTF-8 encoded text.
  82. //
  83. // Control Messages
  84. //
  85. // The WebSocket protocol defines three types of control messages: close, ping
  86. // and pong. Call the connection WriteControl, WriteMessage or NextWriter
  87. // methods to send a control message to the peer.
  88. //
  89. // Connections handle received ping and pong messages by invoking a callback
  90. // function set with SetPingHandler and SetPongHandler methods. These callback
  91. // functions can be invoked from the ReadMessage method, the NextReader method
  92. // or from a call to the data message reader returned from NextReader.
  93. //
  94. // Connections handle received close messages by returning an error from the
  95. // ReadMessage method, the NextReader method or from a call to the data message
  96. // reader returned from NextReader.
  97. package websocket