Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
cfd334ac
Commit
cfd334ac
authored
Sep 09, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved websocket echo server example
parent
b5f6254c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
25 deletions
+31
-25
Makefile
examples/websocket_echo_server/Makefile
+12
-0
index.html
examples/websocket_echo_server/index.html
+0
-0
websocket_echo_server.c
examples/websocket_echo_server/websocket_echo_server.c
+19
-25
No files found.
examples/websocket_echo_server/Makefile
0 → 100644
View file @
cfd334ac
# Copyright (c) 2014 Cesanta Software
# All rights reserved
PROG
=
websocket_echo_server
CFLAGS
=
-W
-Wall
-I
../..
-g
-O0
$(CFLAGS_EXTRA)
SOURCES
=
$(PROG)
.c ../../mongoose.c
$(PROG)
:
$(SOURCES)
$(CC)
-o
$(PROG)
$(SOURCES)
$(CFLAGS)
clean
:
rm
-rf
$(PROG)
*
.exe
*
.dSYM
*
.obj
*
.exp .
*
o
*
.lib
examples/websocket.html
→
examples/websocket
_echo_server/index
.html
View file @
cfd334ac
File moved
examples/websocket.c
→
examples/websocket
_echo_server/websocket_echo_server
.c
View file @
cfd334ac
// Copyright (c) 2013-2014 Cesanta Software Limited
// $Date: 2014-09-09 08:27:35 UTC $
#include <string.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include "mongoose.h"
#include "mongoose.h"
#ifdef _WIN32
static
void
push_message
(
struct
mg_server
*
server
,
time_t
current_time
)
{
#define snprintf _snprintf
struct
mg_connection
*
c
;
#endif
char
buf
[
20
];
int
len
=
sprintf
(
buf
,
"%lu"
,
(
unsigned
long
)
current_time
);
extern
const
char
*
find_embedded_file
(
const
char
*
,
size_t
*
);
static
int
iterate_callback
(
struct
mg_connection
*
c
,
enum
mg_event
ev
)
{
// Iterate over all connections, and push current time message to websocket ones.
if
(
ev
==
MG_POLL
&&
c
->
is_websocket
)
{
for
(
c
=
mg_next
(
server
,
NULL
);
c
!=
NULL
;
c
=
mg_next
(
server
,
c
))
{
char
buf
[
20
];
if
(
c
->
is_websocket
)
{
int
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%lu"
,
mg_websocket_write
(
c
,
1
,
buf
,
len
);
(
unsigned
long
)
*
(
time_t
*
)
c
->
callback_param
);
}
mg_websocket_write
(
c
,
1
,
buf
,
len
);
}
}
return
MG_TRUE
;
}
}
static
int
send_reply
(
struct
mg_connection
*
conn
)
{
static
int
send_reply
(
struct
mg_connection
*
conn
)
{
size_t
index_size
;
const
char
*
index_html
=
find_embedded_file
(
"websocket.html"
,
&
index_size
);
if
(
conn
->
is_websocket
)
{
if
(
conn
->
is_websocket
)
{
// This handler is called for each incoming websocket frame, one or more
// This handler is called for each incoming websocket frame, one or more
// times for connection lifetime.
// times for connection lifetime.
...
@@ -30,19 +27,16 @@ static int send_reply(struct mg_connection *conn) {
...
@@ -30,19 +27,16 @@ static int send_reply(struct mg_connection *conn) {
return
conn
->
content_len
==
4
&&
!
memcmp
(
conn
->
content
,
"exit"
,
4
)
?
return
conn
->
content_len
==
4
&&
!
memcmp
(
conn
->
content
,
"exit"
,
4
)
?
MG_FALSE
:
MG_TRUE
;
MG_FALSE
:
MG_TRUE
;
}
else
{
}
else
{
mg_send_header
(
conn
,
"Content-Type"
,
"text/html"
);
mg_send_file
(
conn
,
"index.html"
);
mg_send_data
(
conn
,
index_html
,
index_size
);
return
MG_MORE
;
return
MG_TRUE
;
}
}
}
}
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_REQUEST
)
{
switch
(
ev
)
{
return
send_reply
(
conn
);
case
MG_AUTH
:
return
MG_TRUE
;
}
else
if
(
ev
==
MG_AUTH
)
{
case
MG_REQUEST
:
return
send_reply
(
conn
);
return
MG_TRUE
;
default:
return
MG_FALSE
;
}
else
{
return
MG_FALSE
;
}
}
}
}
...
@@ -58,7 +52,7 @@ int main(void) {
...
@@ -58,7 +52,7 @@ int main(void) {
current_timer
=
time
(
NULL
);
current_timer
=
time
(
NULL
);
if
(
current_timer
-
last_timer
>
0
)
{
if
(
current_timer
-
last_timer
>
0
)
{
last_timer
=
current_timer
;
last_timer
=
current_timer
;
mg_iterate_over_connections
(
server
,
iterate_callback
,
&
current_timer
);
push_message
(
server
,
current_timer
);
}
}
}
}
...
...
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