Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
9ac67770
Commit
9ac67770
authored
11 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
mongoose5 example
parent
3330bca4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/qcomm.c
+54
-0
54 additions, 0 deletions
examples/qcomm.c
with
54 additions
and
0 deletions
examples/qcomm.c
0 → 100644
+
54
−
0
View file @
9ac67770
static
void
iterate_callback
(
struct
mg_connection
*
c
,
void
*
param
)
{
if
(
c
->
is_websocket
)
{
char
buf
[
20
];
int
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%d"
,
*
(
int
*
)
param
);
mg_websocket_write
(
c
,
1
,
buf
,
len
);
}
}
// This thread sends heartbeats to all websocket connections with 1s interval.
// The heartbeat message is simply an iteration counter.
static
void
*
timer_thread
(
void
*
param
)
{
struct
mg_server
*
server
=
(
struct
mg_server
*
)
param
;
int
i
;
for
(
i
=
0
;
i
<
9999999
;
i
++
)
{
sleep
(
1
);
mg_iterate_over_connections
(
server
,
iterate_callback
,
&
i
);
}
return
NULL
;
}
// This handler is called for each incoming websocket frame, one or more
// times for connection lifetime.
static
int
handler
(
struct
mg_connection
*
conn
)
{
static
const
char
oops
[]
=
"HTTP/1.0 200 OK
\r\n\r\n
websocket data expected
\n
"
;
if
(
!
conn
->
is_websocket
)
{
mg_write
(
conn
,
oops
,
sizeof
(
oops
)
-
1
);
return
1
;
}
mg_websocket_write
(
conn
,
1
,
conn
->
content
,
conn
->
content_len
);
DBG
((
"WS msg len: %d"
,
conn
->
content_len
));
return
conn
->
content_len
==
4
&&
!
memcmp
(
conn
->
content
,
"exit"
,
4
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_set_option
(
server
,
"document_root"
,
argc
>
1
?
argv
[
1
]
:
"."
);
mg_add_uri_handler
(
server
,
"/ws"
,
handler
);
mg_start_thread
(
timer_thread
,
server
);
printf
(
"Started on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
mg_poll_server
(
server
,
3000
);
}
mg_destroy_server
(
&
server
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment