Просмотр исходного кода

Merge pull request #700 from duong2179/master

Fixed some typos and added example code snippet.
bel2125 6 лет назад
Родитель
Сommit
4a8a4bf172
3 измененных файлов с 30 добавлено и 3 удалено
  1. 1 1
      docs/api/mg_get_response_info.md
  2. 27 0
      docs/api/mg_read.md
  3. 2 2
      examples/ws_client/ws_client.c

+ 1 - 1
docs/api/mg_get_response_info.md

@@ -12,7 +12,7 @@
 
 | Type | Description |
 | :--- | :--- |
-|`const struct mg_request_info *`|Pointer to the requested info, or NULL if an error occurred|
+|`const struct mg_response_info *`|Pointer to the response info, or NULL if an error occurred|
 
 ### Description
 

+ 27 - 0
docs/api/mg_read.md

@@ -20,6 +20,33 @@
 
 The function `mg_read()` receives data over an existing connection. The data is handled as binary and is stored in a buffer whose address has been provided as a parameter. The function returns the number of read bytes when successful, the value **0** when the connection has been closed by peer and a negative value when no more data could be read from the connection.
 
+### Example
+
+```
+#define RECV_BUF_SIZE 1 << 20
+
+size_t read_data(struct mg_connection* conn,
+                        uint8_t* buff,
+                        size_t buff_len) {
+  size_t read_len = 0;
+  while (read_len < buff_len) {
+    size_t sz_to_read = std::min<size_t>(RECV_BUF_SIZE, buff_len - read_len);
+    int this_read = mg_read(conn, buff + read_len, sz_to_read);
+    if (this_read < 0) {
+      std::cerr << "[error] Failed to read data" << std::endl;
+      break;
+    } else {
+      read_len += size_t(this_read);
+      if (this_read > 0) {
+        std::cout << "[debug] Received " << this_read << " more bytes" << std::endl;
+      }
+    }
+  }
+
+  return read_len;
+}
+```
+
 ### See Also
 
 * [`mg_printf();`](mg_printf.md)

+ 2 - 2
examples/ws_client/ws_client.c

@@ -43,11 +43,11 @@ msgtypename(int flags)
 	case MG_WEBSOCKET_OPCODE_BINARY:
 		return "binary";
 	case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
-		return "clonnection close";
+		return "connection close";
 	case MG_WEBSOCKET_OPCODE_PING:
 		return "PING";
 	case MG_WEBSOCKET_OPCODE_PONG:
-		return "PING";
+		return "PONG";
 	}
 	return "unknown";
 }