Called to open a new connection and upgrade it to a websocket connection this is the main function to use to create a new websocket connection Use this function if the Type is set to "self"
(handler func(conn *Conn, ctx *pine.Ctx), config ...Config)
| 93 | // this is the main function to use to create a new websocket connection |
| 94 | // Use this function if the Type is set to "self" |
| 95 | func New(handler func(conn *Conn, ctx *pine.Ctx), config ...Config) pine.Handler { |
| 96 | var cfg Config |
| 97 | if len(config) > 0 { |
| 98 | userConfig := config[0] |
| 99 | if userConfig.ReadBufferSize != 0 { |
| 100 | cfg.ReadBufferSize = userConfig.ReadBufferSize |
| 101 | } |
| 102 | if userConfig.WriteBufferSize != 0 { |
| 103 | cfg.WriteBufferSize = userConfig.WriteBufferSize |
| 104 | } |
| 105 | if userConfig.SubprotocolsAllowed != nil { |
| 106 | cfg.SubprotocolsAllowed = userConfig.SubprotocolsAllowed |
| 107 | } |
| 108 | if userConfig.CheckOrigin != nil { |
| 109 | cfg.CheckOrigin = userConfig.CheckOrigin |
| 110 | } |
| 111 | if userConfig.Error != nil { |
| 112 | cfg.Error = userConfig.Error |
| 113 | } |
| 114 | if userConfig.EnableCompression { |
| 115 | cfg.EnableCompression = userConfig.EnableCompression |
| 116 | } |
| 117 | if userConfig.HandshakeTimeout != 0 { |
| 118 | cfg.HandshakeTimeout = userConfig.HandshakeTimeout |
| 119 | } |
| 120 | } else { |
| 121 | cfg = defaultConfig |
| 122 | } |
| 123 | |
| 124 | var upgrader = websocket.Upgrader{ |
| 125 | ReadBufferSize: cfg.ReadBufferSize, |
| 126 | WriteBufferSize: cfg.WriteBufferSize, |
| 127 | Error: cfg.Error, |
| 128 | Subprotocols: cfg.SubprotocolsAllowed, |
| 129 | EnableCompression: cfg.EnableCompression, |
| 130 | HandshakeTimeout: cfg.HandshakeTimeout, |
| 131 | } |
| 132 | // Only override CheckOrigin when the user explicitly provided one. |
| 133 | // When nil, Gorilla's safe default applies (Origin must match Host). |
| 134 | if cfg.CheckOrigin != nil { |
| 135 | upgrader.CheckOrigin = cfg.CheckOrigin |
| 136 | } |
| 137 | |
| 138 | return func(ctx *pine.Ctx) error { |
| 139 | Conn, err := upgrader.Upgrade(ctx.Response.ResponseWriter, ctx.Request, ctx.Response.Header()) |
| 140 | if err != nil { |
| 141 | fmt.Println(err) |
| 142 | return err |
| 143 | } |
| 144 | |
| 145 | conn := acquireConn() |
| 146 | conn.Conn = Conn |
| 147 | defer releaseConn(conn) |
| 148 | handler(conn, ctx) |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | } |
no test coverage detected