github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/transport/ws/options.go (about) 1 package ws 2 3 import ( 4 "net/http" 5 "time" 6 ) 7 8 var defOptions = &Options{ 9 Name: "WebSocket", 10 Addr: ":9060", 11 MaxPacketSize: 4096, 12 MaxConn: 10000, 13 WorkerPoolSize: 1, 14 MaxWorkerTaskLen: 128, 15 MaxMsgChanLen: 128, 16 ManagerSize: 1, 17 ReadBufferSize: 1024, 18 WriteBufferSize: 1024, 19 WriteWait: 10 * time.Second, 20 } 21 22 type AuthHandler = func(r *http.Request, sid string, cid uint64) (uid int, ok bool) 23 24 type Option func(*Options) 25 26 type Options struct { 27 ID string //服务器ID 28 Name string //服务器的名称 29 Addr string //服务绑定的地址 30 MaxPacketSize int //都需数据包的最大值 31 MaxConn int //当前服务器主机允许的最大链接个数 32 WorkerPoolSize int //业务工作Worker池的数量 33 MaxWorkerTaskLen int //业务工作Worker对应负责的任务队列最大任务存储数量 34 MaxMsgChanLen int //SendBuffMsg发送消息的缓冲最大长度 35 ManagerSize int //连接管理器个数 36 ReadBufferSize int //接收缓冲区 37 WriteBufferSize int //发送缓冲区 38 WriteWait time.Duration //写入客户端超时 39 40 Router *Engine //请求路由 41 OnConnStart func(conn Connection) //该Server的连接创建开始时Hook函数 42 OnConnStop func(conn Connection) //该Server的连接断开时的Hook函数 43 OnConnAuth AuthHandler //该Server的连接鉴权完成的Hook函数 44 } 45 46 func WithID(id string) Option { 47 return func(o *Options) { 48 o.ID = id 49 } 50 } 51 52 func WithName(n string) Option { 53 return func(o *Options) { 54 o.Name = n 55 } 56 } 57 58 func WithAddr(addr string) Option { 59 return func(o *Options) { 60 o.Addr = addr 61 } 62 } 63 64 func WithRouter(r *Engine) Option { 65 return func(o *Options) { 66 o.Router = r 67 } 68 } 69 70 func WithMaxPacketSize(size int) Option { 71 return func(o *Options) { 72 o.MaxPacketSize = size 73 } 74 } 75 76 func WithMaxConn(size int) Option { 77 return func(o *Options) { 78 o.MaxConn = size 79 } 80 } 81 82 func WithWorkerPoolSize(size int) Option { 83 return func(o *Options) { 84 o.WorkerPoolSize = size 85 } 86 } 87 88 func WithMaxWorkerTaskLen(size int) Option { 89 return func(o *Options) { 90 o.MaxWorkerTaskLen = size 91 } 92 } 93 94 func WithMaxMsgChanLen(size int) Option { 95 return func(o *Options) { 96 o.MaxMsgChanLen = size 97 } 98 } 99 100 func WithManagerSize(size int) Option { 101 return func(o *Options) { 102 o.ManagerSize = size 103 } 104 } 105 106 func WithReadBufferSize(size int) Option { 107 return func(o *Options) { 108 o.ReadBufferSize = size 109 } 110 } 111 112 func WithWriteBufferSize(size int) Option { 113 return func(o *Options) { 114 o.WriteBufferSize = size 115 } 116 } 117 118 func WithWriteWait(d time.Duration) Option { 119 return func(o *Options) { 120 o.WriteWait = d 121 } 122 } 123 124 func WithOnConnStart(f func(conn Connection)) Option { 125 return func(o *Options) { 126 o.OnConnStart = f 127 } 128 } 129 130 func WithOnConnStop(f func(conn Connection)) Option { 131 return func(o *Options) { 132 o.OnConnStop = f 133 } 134 } 135 136 func WithOnConnAuth(f AuthHandler) Option { 137 return func(o *Options) { 138 o.OnConnAuth = f 139 } 140 }