Skip to content

Commit 17626f3

Browse files
authored
docs: add websocket reverse proxy (cloudwego#876)
1 parent 4132da0 commit 17626f3

File tree

2 files changed

+64
-5
lines changed
  • content
    • en/docs/hertz/tutorials/basic-feature
    • zh/docs/hertz/tutorials/basic-feature

2 files changed

+64
-5
lines changed

content/en/docs/hertz/tutorials/basic-feature/proxy.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,41 @@ func main() {
217217
}
218218
```
219219

220+
### Websocket Reverse Proxy
221+
222+
Websocket reverse proxy for Hertz, inspired by [fasthttp-reverse-proxy](https://github.com/yeqown/fasthttp-reverse-proxy)
223+
224+
#### Example
225+
226+
```go
227+
package main
228+
229+
import (
230+
"github.com/cloudwego/hertz/pkg/app/server"
231+
"github.com/hertz-contrib/reverseproxy"
232+
)
233+
234+
func main() {
235+
h := server.Default()
236+
h.GET("/backend", reverseproxy.NewWSReverseProxy("ws://example.com").ServeHTTP)
237+
h.Spin()
238+
}
239+
```
240+
#### Configuration
241+
242+
| Configuration | Default | Description |
243+
|----------------|---------------------------|------------------------------|
244+
| `WithDirector` | `nil` | customize the forward header |
245+
| `WithDialer` | `gorillaws.DefaultDialer` | for dialer customization |
246+
| `WithUpgrader` | `hzws.HertzUpgrader` | for upgrader customization |
247+
220248
### More Examples
221249

222250
| Purpose | Sample Code |
223251
|-------------------------|-------------------------------------------------------------------------------------------|
224252
| Proxy tls | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/tls) |
225253
| Using service discovery | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/discovery) |
226254
| Use with middleware | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/use_middleware) |
255+
| Proxy websocket | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/websocket) |
227256

228257
For more usages, please refer to the following [examples](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy).

content/zh/docs/hertz/tutorials/basic-feature/proxy.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,42 @@ func main() {
208208
}
209209
```
210210

211+
### 反向代理 Websocket
212+
213+
Websocket 反向代理,受 [fasthttp-reverse-proxy](https://github.com/yeqown/fasthttp-reverse-proxy) 启发。
214+
215+
#### 示例
216+
217+
```go
218+
package main
219+
220+
import (
221+
"github.com/cloudwego/hertz/pkg/app/server"
222+
"github.com/hertz-contrib/reverseproxy"
223+
)
224+
225+
func main() {
226+
h := server.Default()
227+
h.GET("/backend", reverseproxy.NewWSReverseProxy("ws://example.com").ServeHTTP)
228+
h.Spin()
229+
}
230+
```
231+
232+
#### 配置
233+
234+
| 配置 | 默认值 | 描述 |
235+
|----------------|---------------------------|--------------|
236+
| `WithDirector` | `nil` | 自定义转发头 |
237+
| `WithDialer` | `gorillaws.DefaultDialer` | 自定义 Dialer |
238+
| `WithUpgrader` | `hzws.HertzUpgrader` | 自定义 Upgrader |
239+
211240
### 更多示例
212241

213-
| 用途 | 示例代码 |
214-
|---------|-------------------------------------------------------------------------------------------|
215-
| 代理 tls | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/tls) |
216-
| 使用服务发现 | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/discovery) |
217-
| 配合中间件使用 | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/use_middleware) |
242+
| 用途 | 示例代码 |
243+
|--------------|-------------------------------------------------------------------------------------------|
244+
| 代理 tls | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/tls) |
245+
| 使用服务发现 | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/discovery) |
246+
| 配合中间件使用 | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/use_middleware) |
247+
| 代理 websocket | [code](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy/websocket) |
218248

219249
更多使用方法可参考如下 [examples](https://github.com/cloudwego/hertz-examples/tree/main/reverseproxy)

0 commit comments

Comments
 (0)