Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
mongoose
Commits
e62dc8f3
Commit
e62dc8f3
authored
8 years ago
by
Alexander Alashkin
Committed by
Cesanta Bot
8 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle MG_EV_CLOSE in examples
PUBLISHED_FROM=57c62a4123a54ecb455dbf91f3fe562fb0cf2ec2
parent
e9908834
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
5 deletions
+34
-5
client_example.md
docs/coap/client_example.md
+11
-4
client_example.md
docs/http/client_example.md
+3
-1
coap_client.c
examples/coap_client/coap_client.c
+8
-0
http_client.c
examples/http_client/http_client.c
+6
-0
restful_client.c
examples/restful_client/restful_client.c
+6
-0
No files found.
docs/coap/client_example.md
View file @
e62dc8f3
...
...
@@ -7,10 +7,10 @@ To create a CoAP client, follow this pattern:
1.
Create an outbound connection by calling
`mg_connect`
2.
Call
`mg_set_protocol_coap`
for created connection
3.
Create an event handler function that handles the following events:
-
`MG_EV_COAP_CON`
-
`MG_EV_COAP_NOC`
-
`MG_EV_COAP_ACK`
-
`MG_EV_COAP_RST`
-
`MG_EV_COAP_CON`
-
`MG_EV_COAP_NOC`
-
`MG_EV_COAP_ACK`
-
`MG_EV_COAP_RST`
Here's an example of the simplest CoAP client.
Error checking is omitted for the sake of clarity:
...
...
@@ -39,6 +39,13 @@ static void coap_handler(struct mg_connection *nc, int ev, void *p) {
s_time_to_exit
=
1
;
break
;
}
case
MG_EV_CLOSE
:
{
if
(
s_time_to_exit
==
0
)
{
printf
(
"Server closed connection
\n
"
);
s_time_to_exit
=
1
;
}
break
;
}
}
}
...
...
This diff is collapsed.
Click to expand it.
docs/http/client_example.md
View file @
e62dc8f3
...
...
@@ -22,7 +22,9 @@ static void ev_handler(struct mg_connection *c, int ev, void *p) {
fwrite
(
hm
->
message
.
p
,
1
,
hm
->
message
.
len
,
stdout
);
putchar
(
'\n'
);
exit_flag
=
1
;
}
}
else
if
(
ev
==
MG_EV_CLOSE
)
{
exit_flag
=
1
;
};
}
int
main
(
void
)
{
...
...
This diff is collapsed.
Click to expand it.
examples/coap_client/coap_client.c
View file @
e62dc8f3
...
...
@@ -34,9 +34,17 @@ static void coap_handler(struct mg_connection *nc, int ev, void *p) {
case
MG_EV_COAP_RST
:
{
struct
mg_coap_message
*
cm
=
(
struct
mg_coap_message
*
)
p
;
printf
(
"ACK/RST for message with msg_id = %d received
\n
"
,
cm
->
msg_id
);
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
s_time_to_exit
=
1
;
break
;
}
case
MG_EV_CLOSE
:
{
if
(
s_time_to_exit
==
0
)
{
printf
(
"Server closed connection
\n
"
);
s_time_to_exit
=
1
;
}
break
;
}
}
}
...
...
This diff is collapsed.
Click to expand it.
examples/http_client/http_client.c
View file @
e62dc8f3
...
...
@@ -31,6 +31,12 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
putchar
(
'\n'
);
s_exit_flag
=
1
;
break
;
case
MG_EV_CLOSE
:
if
(
s_exit_flag
==
0
)
{
printf
(
"Server closed connection
\n
"
);
s_exit_flag
=
1
;
}
break
;
default:
break
;
}
...
...
This diff is collapsed.
Click to expand it.
examples/restful_client/restful_client.c
View file @
e62dc8f3
...
...
@@ -28,6 +28,12 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
s_exit_flag
=
1
;
break
;
case
MG_EV_CLOSE
:
if
(
s_exit_flag
==
0
)
{
printf
(
"Server closed connection
\n
"
);
s_exit_flag
=
1
;
};
break
;
default:
break
;
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment