mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
82afcc5aa6
https://github.com/kataras/go-websocket/issues/27 Former-commit-id: 0b7e52e0a61150a8bba973ef653986d8b3ddd26b
137 lines
4.2 KiB
Go
137 lines
4.2 KiB
Go
package websocket
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"gopkg.in/kataras/iris.v6"
|
|
)
|
|
|
|
const (
|
|
// DefaultWebsocketWriteTimeout 0, no timeout
|
|
DefaultWebsocketWriteTimeout = 0
|
|
// DefaultWebsocketReadTimeout 0, no timeout
|
|
DefaultWebsocketReadTimeout = 0
|
|
// DefaultWebsocketPongTimeout 60 * time.Second
|
|
DefaultWebsocketPongTimeout = 60 * time.Second
|
|
// DefaultWebsocketPingPeriod (DefaultPongTimeout * 9) / 10
|
|
DefaultWebsocketPingPeriod = (DefaultWebsocketPongTimeout * 9) / 10
|
|
// DefaultWebsocketMaxMessageSize 1024
|
|
DefaultWebsocketMaxMessageSize = 1024
|
|
// DefaultWebsocketReadBufferSize 4096
|
|
DefaultWebsocketReadBufferSize = 4096
|
|
// DefaultWebsocketWriterBufferSize 4096
|
|
DefaultWebsocketWriterBufferSize = 4096
|
|
// DefaultClientSourcePath "/iris-ws.js"
|
|
DefaultClientSourcePath = "/iris-ws.js"
|
|
)
|
|
|
|
var (
|
|
// DefaultIDGenerator returns the result of 64
|
|
// random combined characters as the id of a new connection.
|
|
// Used when config.IDGenerator is nil
|
|
DefaultIDGenerator = func(*iris.Context) string { return randomString(64) }
|
|
)
|
|
|
|
// Config the websocket server configuration
|
|
// all of these are optional.
|
|
type Config struct {
|
|
// Endpoint is the path which the websocket server will listen for clients/connections
|
|
// Default value is empty string, if you don't set it the Websocket server is disabled.
|
|
Endpoint string
|
|
// ClientSourcePath is is the path which the client-side
|
|
// will be auto-served when the server adapted to an Iris station.
|
|
// Default value is "/iris-ws.js"
|
|
ClientSourcePath string
|
|
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
|
|
CheckOrigin func(r *http.Request) bool
|
|
// 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
|
|
// PongTimeout allowed to read the next pong message from the connection.
|
|
// Default value is 60 * time.Second
|
|
PongTimeout time.Duration
|
|
// PingPeriod send ping messages to the connection with this period. Must be less than PongTimeout.
|
|
// Default value is 60 *time.Second
|
|
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.
|
|
// defaults to false
|
|
BinaryMessages bool
|
|
// ReadBufferSize is the buffer size for the underline reader
|
|
// Default value is 4096
|
|
ReadBufferSize int
|
|
// WriteBufferSize is the buffer size for the underline writer
|
|
// Default value is 4096
|
|
WriteBufferSize int
|
|
// IDGenerator used to create (and later on, set)
|
|
// an ID for each incoming websocket connections (clients).
|
|
// The request is an argument which you can use to generate the ID (from headers for example).
|
|
// If empty then the ID is generated by DefaultIDGenerator: randomString(64)
|
|
IDGenerator func(ctx *iris.Context) string
|
|
}
|
|
|
|
// Validate validates the configuration
|
|
func (c Config) Validate() Config {
|
|
|
|
if c.ClientSourcePath == "" {
|
|
c.ClientSourcePath = DefaultClientSourcePath
|
|
}
|
|
|
|
// 0 means no timeout.
|
|
if c.WriteTimeout < 0 {
|
|
c.WriteTimeout = DefaultWebsocketWriteTimeout
|
|
}
|
|
|
|
if c.ReadTimeout < 0 {
|
|
c.ReadTimeout = DefaultWebsocketReadTimeout
|
|
}
|
|
|
|
if c.PongTimeout < 0 {
|
|
c.PongTimeout = DefaultWebsocketPongTimeout
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
if c.IDGenerator == nil {
|
|
c.IDGenerator = DefaultIDGenerator
|
|
}
|
|
|
|
return c
|
|
}
|