2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-16 20:41:47 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package websocket implements the WebSocket protocol defined in RFC 6455.
|
|
|
|
//
|
|
|
|
// Overview
|
|
|
|
//
|
2017-07-10 17:21:27 +03:00
|
|
|
// The Conn type represents a WebSocket connection. A server application calls
|
|
|
|
// the Upgrader.Upgrade method from an HTTP request handler to get a *Conn:
|
2014-04-18 02:40:10 +04:00
|
|
|
//
|
|
|
|
// var upgrader = websocket.Upgrader{
|
|
|
|
// ReadBufferSize: 1024,
|
|
|
|
// WriteBufferSize: 1024,
|
|
|
|
// }
|
2013-10-16 20:41:47 +04:00
|
|
|
//
|
|
|
|
// func handler(w http.ResponseWriter, r *http.Request) {
|
2014-04-18 02:40:10 +04:00
|
|
|
// conn, err := upgrader.Upgrade(w, r, nil)
|
2014-04-20 22:38:35 +04:00
|
|
|
// if err != nil {
|
2013-12-21 03:57:02 +04:00
|
|
|
// log.Println(err)
|
|
|
|
// return
|
|
|
|
// }
|
2013-10-16 20:41:47 +04:00
|
|
|
// ... Use conn to send and receive messages.
|
|
|
|
// }
|
|
|
|
//
|
2015-05-30 02:26:01 +03:00
|
|
|
// Call the connection's WriteMessage and ReadMessage methods to send and
|
2013-10-30 04:43:03 +04:00
|
|
|
// receive messages as a slice of bytes. This snippet of code shows how to echo
|
|
|
|
// messages using these methods:
|
2013-10-16 20:41:47 +04:00
|
|
|
//
|
|
|
|
// for {
|
2013-10-30 04:43:03 +04:00
|
|
|
// messageType, p, err := conn.ReadMessage()
|
2013-10-16 20:41:47 +04:00
|
|
|
// if err != nil {
|
2017-11-12 15:01:55 +03:00
|
|
|
// log.Println(err)
|
2013-10-16 20:41:47 +04:00
|
|
|
// return
|
|
|
|
// }
|
2017-07-02 13:47:41 +03:00
|
|
|
// if err := conn.WriteMessage(messageType, p); err != nil {
|
2017-11-12 15:01:55 +03:00
|
|
|
// log.Println(err)
|
|
|
|
// return
|
2013-10-16 20:41:47 +04:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2013-10-30 04:43:03 +04:00
|
|
|
// In above snippet of code, p is a []byte and messageType is an int with value
|
|
|
|
// websocket.BinaryMessage or websocket.TextMessage.
|
|
|
|
//
|
|
|
|
// An application can also send and receive messages using the io.WriteCloser
|
|
|
|
// and io.Reader interfaces. To send a message, call the connection NextWriter
|
|
|
|
// method to get an io.WriteCloser, write the message to the writer and close
|
|
|
|
// the writer when done. To receive a message, call the connection NextReader
|
|
|
|
// method to get an io.Reader and read until io.EOF is returned. This snippet
|
2016-02-11 03:22:44 +03:00
|
|
|
// shows how to echo messages using the NextWriter and NextReader methods:
|
2013-10-16 20:41:47 +04:00
|
|
|
//
|
|
|
|
// for {
|
2013-10-30 04:43:03 +04:00
|
|
|
// messageType, r, err := conn.NextReader()
|
2013-10-16 20:41:47 +04:00
|
|
|
// if err != nil {
|
|
|
|
// return
|
|
|
|
// }
|
2013-10-30 04:43:03 +04:00
|
|
|
// w, err := conn.NextWriter(messageType)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// if _, err := io.Copy(w, r); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// if err := w.Close(); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Data Messages
|
|
|
|
//
|
|
|
|
// The WebSocket protocol distinguishes between text and binary data messages.
|
|
|
|
// Text messages are interpreted as UTF-8 encoded text. The interpretation of
|
|
|
|
// binary messages is left to the application.
|
|
|
|
//
|
2013-10-30 04:43:03 +04:00
|
|
|
// This package uses the TextMessage and BinaryMessage integer constants to
|
|
|
|
// identify the two data message types. The ReadMessage and NextReader methods
|
|
|
|
// return the type of the received message. The messageType argument to the
|
|
|
|
// WriteMessage and NextWriter methods specifies the type of a sent message.
|
|
|
|
//
|
|
|
|
// It is the application's responsibility to ensure that text messages are
|
|
|
|
// valid UTF-8 encoded text.
|
2013-10-16 20:41:47 +04:00
|
|
|
//
|
|
|
|
// Control Messages
|
|
|
|
//
|
|
|
|
// The WebSocket protocol defines three types of control messages: close, ping
|
|
|
|
// and pong. Call the connection WriteControl, WriteMessage or NextWriter
|
|
|
|
// methods to send a control message to the peer.
|
|
|
|
//
|
2017-11-20 00:55:18 +03:00
|
|
|
// Connections handle received close messages by calling the handler function
|
|
|
|
// set with the SetCloseHandler method and by returning a *CloseError from the
|
|
|
|
// NextReader, ReadMessage or the message Read method. The default close
|
|
|
|
// handler sends a close message to the peer.
|
|
|
|
//
|
|
|
|
// Connections handle received ping messages by calling the handler function
|
|
|
|
// set with the SetPingHandler method. The default ping handler sends a pong
|
|
|
|
// message to the peer.
|
|
|
|
//
|
|
|
|
// Connections handle received pong messages by calling the handler function
|
|
|
|
// set with the SetPongHandler method. The default pong handler does nothing.
|
|
|
|
// If an application sends ping messages, then the application should set a
|
|
|
|
// pong handler to receive the corresponding pong.
|
|
|
|
//
|
|
|
|
// The control message handler functions are called from the NextReader,
|
|
|
|
// ReadMessage and message reader Read methods. The default close and ping
|
|
|
|
// handlers can block these methods for a short time when the handler writes to
|
|
|
|
// the connection.
|
|
|
|
//
|
|
|
|
// The application must read the connection to process close, ping and pong
|
2016-03-13 21:57:37 +03:00
|
|
|
// messages sent from the peer. If the application is not otherwise interested
|
|
|
|
// in messages from the peer, then the application should start a goroutine to
|
2016-02-17 02:03:54 +03:00
|
|
|
// read and discard messages from the peer. A simple example is:
|
|
|
|
//
|
|
|
|
// func readLoop(c *websocket.Conn) {
|
|
|
|
// for {
|
|
|
|
// if _, _, err := c.NextReader(); err != nil {
|
|
|
|
// c.Close()
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2013-10-30 04:43:03 +04:00
|
|
|
//
|
|
|
|
// Concurrency
|
|
|
|
//
|
2015-10-20 01:05:01 +03:00
|
|
|
// Connections support one concurrent reader and one concurrent writer.
|
|
|
|
//
|
|
|
|
// Applications are responsible for ensuring that no more than one goroutine
|
|
|
|
// calls the write methods (NextWriter, SetWriteDeadline, WriteMessage,
|
2017-01-19 22:00:23 +03:00
|
|
|
// WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and
|
|
|
|
// that no more than one goroutine calls the read methods (NextReader,
|
|
|
|
// SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler)
|
|
|
|
// concurrently.
|
2014-11-01 00:51:45 +03:00
|
|
|
//
|
|
|
|
// The Close and WriteControl methods can be called concurrently with all other
|
|
|
|
// methods.
|
2013-10-30 04:43:03 +04:00
|
|
|
//
|
2014-10-12 20:34:51 +04:00
|
|
|
// Origin Considerations
|
|
|
|
//
|
|
|
|
// Web browsers allow Javascript applications to open a WebSocket connection to
|
|
|
|
// any host. It's up to the server to enforce an origin policy using the Origin
|
|
|
|
// request header sent by the browser.
|
|
|
|
//
|
|
|
|
// The Upgrader calls the function specified in the CheckOrigin field to check
|
|
|
|
// the origin. If the CheckOrigin function returns false, then the Upgrade
|
|
|
|
// method fails the WebSocket handshake with HTTP status 403.
|
|
|
|
//
|
|
|
|
// If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
|
2018-01-25 21:51:21 +03:00
|
|
|
// the handshake if the Origin request header is present and the Origin host is
|
|
|
|
// not equal to the Host request header.
|
2014-10-12 20:34:51 +04:00
|
|
|
//
|
2017-07-10 17:21:27 +03:00
|
|
|
// The deprecated package-level Upgrade function does not perform origin
|
|
|
|
// checking. The application is responsible for checking the Origin header
|
|
|
|
// before calling the Upgrade function.
|
2016-09-30 07:52:15 +03:00
|
|
|
//
|
2017-01-11 22:29:28 +03:00
|
|
|
// Compression EXPERIMENTAL
|
2016-09-30 07:52:15 +03:00
|
|
|
//
|
|
|
|
// Per message compression extensions (RFC 7692) are experimentally supported
|
2016-10-18 03:30:22 +03:00
|
|
|
// by this package in a limited capacity. Setting the EnableCompression option
|
|
|
|
// to true in Dialer or Upgrader will attempt to negotiate per message deflate
|
2017-01-26 21:48:11 +03:00
|
|
|
// support.
|
|
|
|
//
|
|
|
|
// var upgrader = websocket.Upgrader{
|
|
|
|
// EnableCompression: true,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// If compression was successfully negotiated with the connection's peer, any
|
|
|
|
// message received in compressed form will be automatically decompressed.
|
|
|
|
// All Read methods will return uncompressed bytes.
|
2016-09-30 07:52:15 +03:00
|
|
|
//
|
|
|
|
// Per message compression of messages written to a connection can be enabled
|
|
|
|
// or disabled by calling the corresponding Conn method:
|
|
|
|
//
|
2017-01-26 21:48:11 +03:00
|
|
|
// conn.EnableWriteCompression(false)
|
2016-09-30 07:52:15 +03:00
|
|
|
//
|
2019-02-03 08:49:04 +03:00
|
|
|
// Currently this package supports compression with "context takeover".
|
2016-09-30 07:52:15 +03:00
|
|
|
// This means that messages must be compressed and decompressed in isolation,
|
|
|
|
// without retaining sliding window or dictionary state across messages. For
|
|
|
|
// more details refer to RFC 7692.
|
|
|
|
//
|
2018-02-07 05:36:55 +03:00
|
|
|
// If you want to use it, please do as follows.
|
|
|
|
//
|
|
|
|
// var upgrader = websocket.Upgrader{
|
|
|
|
// EnableCompression: true,
|
2018-03-05 12:37:27 +03:00
|
|
|
// AllowServerContextTakeover: true,
|
2018-02-07 05:36:55 +03:00
|
|
|
// }
|
|
|
|
//
|
2016-09-30 07:52:15 +03:00
|
|
|
// Use of compression is experimental and may result in decreased performance.
|
2013-10-16 20:41:47 +04:00
|
|
|
package websocket
|