You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create an `index.html` file in the same directory with the following template. The original configuration options present in the old `hertz-contrib/swagger` middleware can be directly configured in the HTML file.
139
+
2.Create an `index.html` file in the same directory with the following template. The original configuration options present in the old `hertz-contrib/swagger` middleware can be directly configured in the HTML file.
136
140
137
141
**Note: The HTML file below is just a sample and should be modified accordingly to the user's needs.**
138
142
@@ -259,7 +263,7 @@ project/
259
263
└── go.sum
260
264
```
261
265
262
-
7. Serve static files
266
+
3. Serve static files
263
267
264
268
Add this line into `main.go`
265
269
@@ -274,7 +278,7 @@ h.Spin()
274
278
// Rest of code
275
279
```
276
280
277
-
8. Run program
281
+
4. Run program
278
282
279
283
```sh
280
284
go run main.go
@@ -288,7 +292,7 @@ Hertz middleware to automatically generate RESTful API documentation with Swagge
288
292
289
293
The implementation of the [swagger](https://github.com/hertz-contrib/swagger) extension refers to the implementation of [Gin](https://github.com/swaggo/gin-swagger).
290
294
291
-
## Usage
295
+
###Usage
292
296
293
297
1. Add comments to your API source code, See [Declarative Comments Format](https://github.com/swaggo/swag/blob/master/README.md#declarative-comments-format).
description: "Hertz implements support for Gorilla WebSocket based on hijack."
7
7
---
8
8
9
-
WebSocket is a type of full-duplex communication that can be performed on a single TCP connection and is located at the application layer of the OSI model.
10
-
WebSocket makes data exchange between client and server easier, allowing the server to actively push data to the client.
11
-
In the WebSocket API, the browser and the server only need to complete a handshake that a persistent connection can be created between the two, and two-way data transmission can be performed.
12
-
13
-
Hertz provides support for WebSocket and adapts it in Hertz by referring to [gorilla/websocket](https://github.com/gorilla/websocket) using `hijack`.
14
-
The usage and parameters are basically the same.
15
-
16
9
> **⚠️ Deprecated**
17
10
>
18
11
> The `hertz-contrib/websocket` middleware is now deprecated.
19
12
> Hertz recommends all users to migrate to the official `gorilla/websocket` toolchain using the [Hertz HTTP Adaptor](../../basic-feature/http-adaptor/).
20
13
>
21
14
> See the migration guide below.
22
15
16
+
## Overview
17
+
18
+
WebSocket is a type of full-duplex communication that can be performed on a single TCP connection and is located at the application layer of the OSI model.
19
+
WebSocket makes data exchange between client and server easier, allowing the server to actively push data to the client.
20
+
In the WebSocket API, the browser and the server only need to complete a handshake that a persistent connection can be created between the two, and two-way data transmission can be performed.
21
+
22
+
Historically, Hertz provided WebSocket support via [`hertz-contrib/websocket`](https://github.com/hertz-contrib/websocket), which adapts
23
+
[`gorilla/websocket`](https://github.com/gorilla/websocket) using HTTP connection hijacking.
24
+
This approach is now deprecated in favor of using the official Gorilla WebSocket implementation directly via the Hertz HTTP Adaptor.
In the example code above, the server includes a simple web client. To use this client, open [http://127.0.0.1:8080]() in your browser and follow the instructions on the page.
191
199
192
-
## Config
200
+
###Config
193
201
194
202
The following is the optional configuration parameters for using Hertz WebSocket.
195
203
@@ -205,7 +213,7 @@ This section is organized around the `websocket.HertzUpgrader` structure.
205
213
|`CheckOrigin`| Used to set a check function for Origin header for the request. If the Origin header of the request is acceptable, CheckOrigin returns true. |
206
214
|`EnableCompression`| Used to set whether the server should attempt to negotiate compression for each message (RFC 7692). Setting this value to true does not guarantee that compression will be supported. |
207
215
208
-
### WriteBufferPool
216
+
####WriteBufferPool
209
217
210
218
If this value is not set, an additional write buffer is initialized and allocated to the connection for the current lifetime. The buffer pool is most useful when the application has a moderate amount of writes on a large number of connections.
211
219
@@ -246,13 +254,13 @@ var upgrader = websocket.HertzUpgrader{
246
254
}
247
255
```
248
256
249
-
### Subprotocols
257
+
####Subprotocols
250
258
251
259
WebSocket simply defines a mechanism for exchanging arbitrary messages. What those messages mean, what kind of messages the client can expect at any given point in time, or what kind of messages they are allowed to send, depends entirely on the implementing application.
252
260
253
261
So you need an agreement between the server and the client about these things. The subprotocol parameters simply allow the client and server to formally exchange this information. You can make up any name for any protocol you want. The server can simply check that the client has followed that protocol during the handshake.
254
262
255
-
### Error
263
+
####Error
256
264
257
265
If Error is nil, then use the API provided by Hertz to generate the HTTP error response.
258
266
@@ -273,7 +281,7 @@ var upgrader = websocket.HertzUpgrader{
273
281
}
274
282
```
275
283
276
-
### CheckOrigin
284
+
####CheckOrigin
277
285
278
286
CheckOrigin returns true if the request Origin header is acceptable. If CheckOrigin is nil, then a safe default is used: return false if the Origin request header is present and the origin host is not equal to request Host header.
The server accepts one or more extension fields that are included in the `Sec-WebSocket-Extensions` header field extensions requested by the client. When EnableCompression is true, the server matches the extensions it currently supports with its extensions, and supports compression if the match is successful.
308
316
309
317
Currently only the "no context takeover" mode is supported, as described in the `HertzUpgrader.Upgrade`.
310
318
311
-
### SetReadTimeout/SetWriteTimeout
319
+
####SetReadTimeout/SetWriteTimeout
312
320
313
321
When using websockets for reading and writing, the read timeout or write timeout can be set similarly as follows.
314
322
@@ -330,7 +338,7 @@ func echo(_ context.Context, c *app.RequestContext) {
330
338
}
331
339
```
332
340
333
-
## NoHijackConnPool
341
+
###NoHijackConnPool
334
342
335
343
> The hijack conn used for Hertz connection hijacking is pooled and therefore does not support asynchronous operations when the hijacked connection is used in a websocket.
0 commit comments