-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (60 loc) · 1.82 KB
/
Copy pathmain.cpp
File metadata and controls
88 lines (60 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <nodepp/nodepp.h>
#include <nodepp/worker.h>
#include <nodepp/timer.h>
#include <nodepp/http.h>
#include <nodepp/path.h>
#include <nodepp/ws.h>
#include <nodepp/fs.h>
using namespace nodepp;
void server(){
auto server = http::server([=]( http_t cli ){
cli.write_header( 200, header_t({
{ "Content-Security-Policy", "*" }
}) );
cli.write("Hello World!");
}); ws::server( server );
server.onConnect([=]( ws_t cli ){
console::log("connected");
cli.onData([=]( string_t data ){
cli.write( "<: received" );
console::log( data );
});
cli.onClose([=](){
console::log("closed");
});
});
server.onError([=]( except_t err ){
console::log( ">>", err.what() );
});
server.listen( "localhost", 8000, [=]( socket_t server ){
console::log("server started at http://localhost:8000");
});
}
void client() {
auto client = ws::client( "ws://localhost:8000/" );
client.onConnect([=]( ws_t cli ){
console::log("connected");
cli.onData([=]( string_t data ){
console::log( "client read>>", data );
});
cli.onClose([=](){
console::log("closed");
});
stream::pipe( cli );
timer ::add ([=](){
auto msg = regex::format( "hello world! ${0}", process::now() );
return cli.write( msg ) <= 0 ? -1 : 1 ;
},1000);
});
client.onError([=]( except_t err ){
console::log( "<>", err.data() );
});
}
void onMain() {
worker::add( coroutine::add( COROUTINE(){
coBegin /*--*/ ; server();
process::wait(); coFinish }));
worker::add( coroutine::add( COROUTINE(){
coBegin /*--*/ ; client();
process::wait(); coFinish }));
}