feat: server 包 websocket 服务器支持压缩
This commit is contained in:
parent
9dc73bf281
commit
6962cf4989
|
@ -40,6 +40,33 @@ type runtime struct {
|
||||||
prod bool // 是否为生产模式
|
prod bool // 是否为生产模式
|
||||||
ticker *timer.Ticker // 定时器
|
ticker *timer.Ticker // 定时器
|
||||||
websocketReadDeadline time.Duration // websocket连接超时时间
|
websocketReadDeadline time.Duration // websocket连接超时时间
|
||||||
|
websocketCompression int // websocket压缩等级
|
||||||
|
websocketWriteCompression bool // websocket写入压缩
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithWebsocketWriteCompression 通过数据写入压缩的方式创建Websocket服务器
|
||||||
|
// - 默认不开启数据压缩
|
||||||
|
func WithWebsocketWriteCompression() Option {
|
||||||
|
return func(srv *Server) {
|
||||||
|
if srv.network != NetworkWebsocket {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
srv.websocketWriteCompression = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithWebsocketCompression 通过数据压缩的方式创建Websocket服务器
|
||||||
|
// - 默认不开启数据压缩
|
||||||
|
func WithWebsocketCompression(level int) Option {
|
||||||
|
return func(srv *Server) {
|
||||||
|
if srv.network != NetworkWebsocket {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !(-2 <= level && level <= 9) {
|
||||||
|
panic("websocket: invalid compression level")
|
||||||
|
}
|
||||||
|
srv.websocketCompression = level
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithMessageChannelSize 通过指定消息通道大小的方式创建服务器
|
// WithMessageChannelSize 通过指定消息通道大小的方式创建服务器
|
||||||
|
|
|
@ -250,7 +250,10 @@ func (slf *Server) Run(addr string) error {
|
||||||
ip = addr[0:index]
|
ip = addr[0:index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if slf.websocketCompression > 0 {
|
||||||
|
_ = ws.SetCompressionLevel(slf.websocketCompression)
|
||||||
|
}
|
||||||
|
ws.EnableWriteCompression(slf.websocketWriteCompression)
|
||||||
conn := newWebsocketConn(slf, ws, ip)
|
conn := newWebsocketConn(slf, ws, ip)
|
||||||
for k, v := range request.URL.Query() {
|
for k, v := range request.URL.Query() {
|
||||||
if len(v) == 1 {
|
if len(v) == 1 {
|
||||||
|
|
Loading…
Reference in New Issue