github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gorilla/websocket/doc.go (about) 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 5 // Package websocket implements the WebSocket protocol defined in RFC 6455. 6 // 7 // Overview 8 // 9 // The Conn type represents a WebSocket connection. A server application uses 10 // the Upgrade function from an Upgrader object with a HTTP request handler 11 // to get a pointer to a Conn: 12 // 13 // var upgrader = websocket.Upgrader{ 14 // ReadBufferSize: 1024, 15 // WriteBufferSize: 1024, 16 // } 17 // 18 // func handler(w http.ResponseWriter, r *http.Request) { 19 // conn, err := upgrader.Upgrade(w, r, nil) 20 // if err != nil { 21 // log.Println(err) 22 // return 23 // } 24 // ... Use conn to send and receive messages. 25 // } 26 // 27 // Call the connection's WriteMessage and ReadMessage methods to send and 28 // receive messages as a slice of bytes. This snippet of code shows how to echo 29 // messages using these methods: 30 // 31 // for { 32 // messageType, p, err := conn.ReadMessage() 33 // if err != nil { 34 // return 35 // } 36 // if err = conn.WriteMessage(messageType, p); err != nil { 37 // return err 38 // } 39 // } 40 // 41 // In above snippet of code, p is a []byte and messageType is an int with value 42 // websocket.BinaryMessage or websocket.TextMessage. 43 // 44 // An application can also send and receive messages using the io.WriteCloser 45 // and io.Reader interfaces. To send a message, call the connection NextWriter 46 // method to get an io.WriteCloser, write the message to the writer and close 47 // the writer when done. To receive a message, call the connection NextReader 48 // method to get an io.Reader and read until io.EOF is returned. This snippet 49 // shows how to echo messages using the NextWriter and NextReader 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 close messages by sending a close message to the 89 // peer and returning a *CloseError from the the NextReader, ReadMessage or the 90 // message Read method. 91 // 92 // Connections handle received ping and pong messages by invoking callback 93 // functions set with SetPingHandler and SetPongHandler methods. The callback 94 // functions are called from the NextReader, ReadMessage and the message Read 95 // methods. 96 // 97 // The default ping handler sends a pong to the peer. The application's reading 98 // goroutine can block for a short time while the handler writes the pong data 99 // to the connection. 100 // 101 // The application must read the connection to process ping, pong and close 102 // messages sent from the peer. If the application is not otherwise interested 103 // in messages from the peer, then the application should start a goroutine to 104 // read and discard messages from the peer. A simple example is: 105 // 106 // func readLoop(c *websocket.Conn) { 107 // for { 108 // if _, _, err := c.NextReader(); err != nil { 109 // c.Close() 110 // break 111 // } 112 // } 113 // } 114 // 115 // Concurrency 116 // 117 // Connections support one concurrent reader and one concurrent writer. 118 // 119 // Applications are responsible for ensuring that no more than one goroutine 120 // calls the write methods (NextWriter, SetWriteDeadline, WriteMessage, 121 // WriteJSON) concurrently and that no more than one goroutine calls the read 122 // methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, 123 // SetPingHandler) concurrently. 124 // 125 // The Close and WriteControl methods can be called concurrently with all other 126 // methods. 127 // 128 // Origin Considerations 129 // 130 // Web browsers allow Javascript applications to open a WebSocket connection to 131 // any host. It's up to the server to enforce an origin policy using the Origin 132 // request header sent by the browser. 133 // 134 // The Upgrader calls the function specified in the CheckOrigin field to check 135 // the origin. If the CheckOrigin function returns false, then the Upgrade 136 // method fails the WebSocket handshake with HTTP status 403. 137 // 138 // If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail 139 // the handshake if the Origin request header is present and not equal to the 140 // Host request header. 141 // 142 // An application can allow connections from any origin by specifying a 143 // function that always returns true: 144 // 145 // var upgrader = websocket.Upgrader{ 146 // CheckOrigin: func(r *http.Request) bool { return true }, 147 // } 148 // 149 // The deprecated Upgrade function does not enforce an origin policy. It's the 150 // application's responsibility to check the Origin header before calling 151 // Upgrade. 152 package websocket