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
7bd8cc3c
Commit
7bd8cc3c
authored
Sep 19, 2012
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added websocket example files
parent
828f6cd7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
0 deletions
+93
-0
websocket.c
examples/websocket.c
+59
-0
index.html
examples/websocket_html_root/index.html
+34
-0
No files found.
examples/websocket.c
0 → 100644
View file @
7bd8cc3c
// Copyright (c) 2004-2012 Sergey Lyubka
// This file is a part of mongoose project, http://github.com/valenok/mongoose
#include <stdio.h>
#include <string.h>
#include "mongoose.h"
static
void
*
callback
(
enum
mg_event
event
,
struct
mg_connection
*
conn
)
{
if
(
event
==
MG_WEBSOCKET_READY
)
{
static
const
char
*
hello
=
"hello from mongoose! waiting for message ..."
;
char
frame
[
2
];
// Prepare websocket frame.
frame
[
0
]
=
0x81
;
// text frame
frame
[
1
]
=
strlen
(
hello
);
// length is < 126
// Write frame and a text message
mg_write
(
conn
,
frame
,
sizeof
(
frame
));
mg_write
(
conn
,
hello
,
strlen
(
hello
));
return
""
;
}
else
if
(
event
==
MG_WEBSOCKET_MESSAGE
)
{
unsigned
char
buf
[
500
],
reply
[
500
];
int
len
,
msg_len
,
i
,
mask_len
,
xor
;
// Read message from the client and echo it back
if
((
len
=
mg_read
(
conn
,
buf
,
sizeof
(
buf
)))
>
8
)
{
msg_len
=
buf
[
1
]
&
127
;
mask_len
=
(
buf
[
1
]
&
128
)
?
4
:
0
;
if
(
msg_len
<
126
)
{
reply
[
0
]
=
0x81
;
// text, FIN set
reply
[
1
]
=
msg_len
;
for
(
i
=
0
;
i
<
msg_len
;
i
++
)
{
xor
=
mask_len
==
0
?
0
:
buf
[
2
+
(
i
%
4
)];
reply
[
i
+
2
]
=
buf
[
i
+
2
+
mask_len
]
^
xor
;
}
mg_write
(
conn
,
reply
,
2
+
msg_len
);
}
}
return
""
;
// Return non-NULL: stop websocket conversation
}
else
{
return
NULL
;
}
}
int
main
(
void
)
{
struct
mg_context
*
ctx
;
const
char
*
options
[]
=
{
"listening_ports"
,
"8080"
,
"document_root"
,
"websocket_html_root"
,
NULL
};
ctx
=
mg_start
(
&
callback
,
NULL
,
options
);
getchar
();
// Wait until user hits "enter"
mg_stop
(
ctx
);
return
0
;
}
examples/websocket_html_root/index.html
0 → 100644
View file @
7bd8cc3c
<!DOCTYPE html>
<meta
charset=
"utf-8"
/>
<title>
WebSocket Test
</title>
<script
language=
"javascript"
type=
"text/javascript"
>
var
writeToScreen
=
function
(
message
)
{
var
div
=
document
.
createElement
(
'div'
);
div
.
innerHTML
=
message
;
document
.
getElementById
(
'output'
).
appendChild
(
div
);
};
window
.
onload
=
function
()
{
var
url
=
'ws://'
+
window
.
location
.
host
+
'/foo'
;
websocket
=
new
WebSocket
(
url
);
websocket
.
onopen
=
function
(
ev
)
{
writeToScreen
(
'CONNECTED'
);
var
message
=
'Не всё подчиняется разуму. Но всё подчиняется упорству. '
;
writeToScreen
(
'SENT: '
+
message
);
websocket
.
send
(
message
);
};
websocket
.
onclose
=
function
(
ev
)
{
writeToScreen
(
'DISCONNECTED'
);
};
websocket
.
onmessage
=
function
(
ev
)
{
writeToScreen
(
'<span style="color: blue;">RESPONSE: '
+
ev
.
data
+
' </span>'
);
websocket
.
close
();
};
websocket
.
onerror
=
function
(
ev
)
{
writeToScreen
(
'<span style="color: red; ">ERROR: </span> '
+
ev
.
data
);
};
};
</script>
<h2>
WebSocket Test
</h2>
<div
id=
"output"
></div>
</html>
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