2017-02-15 07:40:43 +01:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-02-22 20:24:10 +01:00
|
|
|
"strconv"
|
2017-02-15 07:40:43 +01:00
|
|
|
"time"
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
"github.com/kataras/iris/context"
|
2018-10-28 00:19:22 +02:00
|
|
|
|
|
|
|
"github.com/iris-contrib/go.uuid"
|
2017-02-15 07:40:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultWebsocketWriteTimeout 0, no timeout
|
|
|
|
DefaultWebsocketWriteTimeout = 0
|
|
|
|
// DefaultWebsocketReadTimeout 0, no timeout
|
|
|
|
DefaultWebsocketReadTimeout = 0
|
2019-02-14 02:28:41 +01:00
|
|
|
// DefaultWebsocketPingPeriod is 0 but
|
|
|
|
// could be 10 * time.Second.
|
|
|
|
DefaultWebsocketPingPeriod = 0
|
|
|
|
// DefaultWebsocketMaxMessageSize 0
|
|
|
|
DefaultWebsocketMaxMessageSize = 0
|
|
|
|
// DefaultWebsocketReadBufferSize 0
|
|
|
|
DefaultWebsocketReadBufferSize = 0
|
|
|
|
// DefaultWebsocketWriterBufferSize 0
|
|
|
|
DefaultWebsocketWriterBufferSize = 0
|
2018-10-28 00:19:22 +02:00
|
|
|
// DefaultEvtMessageKey is the default prefix of the underline websocket events
|
|
|
|
// that are being established under the hoods.
|
|
|
|
//
|
|
|
|
// Defaults to "iris-websocket-message:".
|
|
|
|
// Last character of the prefix should be ':'.
|
|
|
|
DefaultEvtMessageKey = "iris-websocket-message:"
|
2017-02-15 07:40:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-02-22 20:24:10 +01:00
|
|
|
// DefaultIDGenerator returns a random unique string for a new connection.
|
2018-10-28 00:19:22 +02:00
|
|
|
// Used when config.IDGenerator is nil.
|
|
|
|
DefaultIDGenerator = func(context.Context) string {
|
|
|
|
id, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
2019-02-22 20:24:10 +01:00
|
|
|
return strconv.FormatInt(time.Now().Unix(), 10)
|
2018-10-28 00:19:22 +02:00
|
|
|
}
|
|
|
|
return id.String()
|
|
|
|
}
|
2017-02-15 07:40:43 +01:00
|
|
|
)
|
|
|
|
|
2019-02-22 20:24:10 +01:00
|
|
|
// Config contains the websocket server's configuration, optional.
|
2017-02-15 07:40:43 +01:00
|
|
|
type Config struct {
|
2017-07-10 17:32:42 +02:00
|
|
|
// IDGenerator used to create (and later on, set)
|
|
|
|
// an ID for each incoming websocket connections (clients).
|
2018-10-28 00:19:22 +02:00
|
|
|
// The request is an input parameter which you can use to generate the ID (from headers for example).
|
2017-07-10 17:32:42 +02:00
|
|
|
// If empty then the ID is generated by DefaultIDGenerator: randomString(64)
|
|
|
|
IDGenerator func(ctx context.Context) string
|
2018-10-28 00:19:22 +02:00
|
|
|
// EvtMessagePrefix is the prefix of the underline websocket events that are being established under the hoods.
|
|
|
|
// This prefix is visible only to the javascript side (code) and it has nothing to do
|
|
|
|
// with the message that the end-user receives.
|
|
|
|
// Do not change it unless it is absolutely necessary.
|
|
|
|
//
|
|
|
|
// If empty then defaults to []byte("iris-websocket-message:").
|
|
|
|
EvtMessagePrefix []byte
|
2017-12-27 16:09:47 +01:00
|
|
|
// Error is the function that will be fired if any client couldn't upgrade the HTTP connection
|
|
|
|
// to a websocket connection, a handshake error.
|
|
|
|
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
|
|
|
|
// CheckOrigin a function that is called right before the handshake,
|
|
|
|
// if returns false then that client is not allowed to connect with the websocket server.
|
2017-07-10 17:32:42 +02:00
|
|
|
CheckOrigin func(r *http.Request) bool
|
|
|
|
// HandshakeTimeout specifies the duration for the handshake to complete.
|
|
|
|
HandshakeTimeout time.Duration
|
2017-02-15 07:40:43 +01:00
|
|
|
// WriteTimeout time allowed to write a message to the connection.
|
|
|
|
// 0 means no timeout.
|
|
|
|
// Default value is 0
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
// ReadTimeout time allowed to read a message from the connection.
|
|
|
|
// 0 means no timeout.
|
|
|
|
// Default value is 0
|
|
|
|
ReadTimeout time.Duration
|
2019-02-14 02:28:41 +01:00
|
|
|
// PingPeriod send ping messages to the connection repeatedly after this period.
|
|
|
|
// The value should be close to the ReadTimeout to avoid issues.
|
|
|
|
// Default value is 0.
|
2017-02-15 07:40:43 +01:00
|
|
|
PingPeriod time.Duration
|
|
|
|
// MaxMessageSize max message size allowed from connection.
|
|
|
|
// Default value is 1024
|
|
|
|
MaxMessageSize int64
|
|
|
|
// BinaryMessages set it to true in order to denotes binary data messages instead of utf-8 text
|
|
|
|
// compatible if you wanna use the Connection's EmitMessage to send a custom binary data to the client, like a native server-client communication.
|
2017-12-27 16:09:47 +01:00
|
|
|
// Default value is false
|
2017-02-15 07:40:43 +01:00
|
|
|
BinaryMessages bool
|
2019-02-14 02:28:41 +01:00
|
|
|
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
|
|
|
|
// size is zero, then buffers allocated by the HTTP server are used. The
|
|
|
|
// I/O buffer sizes do not limit the size of the messages that can be sent
|
|
|
|
// or received.
|
|
|
|
//
|
|
|
|
// Default value is 0.
|
|
|
|
ReadBufferSize, WriteBufferSize int
|
2017-07-10 17:32:42 +02:00
|
|
|
// EnableCompression specify if the server should attempt to negotiate per
|
|
|
|
// message compression (RFC 7692). Setting this value to true does not
|
|
|
|
// guarantee that compression will be supported. Currently only "no context
|
|
|
|
// takeover" modes are supported.
|
2017-12-27 16:09:47 +01:00
|
|
|
//
|
2018-01-04 15:34:04 +01:00
|
|
|
// Defaults to false and it should be remain as it is, unless special requirements.
|
2017-07-10 17:32:42 +02:00
|
|
|
EnableCompression bool
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
|
2017-06-02 23:13:46 +02:00
|
|
|
// Subprotocols specifies the server's supported protocols in order of
|
|
|
|
// preference. If this field is set, then the Upgrade method negotiates a
|
|
|
|
// subprotocol by selecting the first match in this list with a protocol
|
|
|
|
// requested by the client.
|
2017-06-02 23:09:21 +02:00
|
|
|
Subprotocols []string
|
2017-02-15 07:40:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the configuration
|
|
|
|
func (c Config) Validate() Config {
|
|
|
|
// 0 means no timeout.
|
|
|
|
if c.WriteTimeout < 0 {
|
|
|
|
c.WriteTimeout = DefaultWebsocketWriteTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ReadTimeout < 0 {
|
|
|
|
c.ReadTimeout = DefaultWebsocketReadTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.PingPeriod <= 0 {
|
|
|
|
c.PingPeriod = DefaultWebsocketPingPeriod
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MaxMessageSize <= 0 {
|
|
|
|
c.MaxMessageSize = DefaultWebsocketMaxMessageSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ReadBufferSize <= 0 {
|
|
|
|
c.ReadBufferSize = DefaultWebsocketReadBufferSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.WriteBufferSize <= 0 {
|
|
|
|
c.WriteBufferSize = DefaultWebsocketWriterBufferSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Error == nil {
|
|
|
|
c.Error = func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
|
|
|
//empty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.CheckOrigin == nil {
|
|
|
|
c.CheckOrigin = func(r *http.Request) bool {
|
|
|
|
// allow all connections by default
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 00:19:22 +02:00
|
|
|
if len(c.EvtMessagePrefix) == 0 {
|
|
|
|
c.EvtMessagePrefix = []byte(DefaultEvtMessageKey)
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:40:43 +01:00
|
|
|
if c.IDGenerator == nil {
|
|
|
|
c.IDGenerator = DefaultIDGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|