Reference website:
uWebSockets Project Address: https://github.com/uNetworking/uWebSockets
uSockets Project Address: https://github.com/uNetworking/uSockets
First, the summary:
1, compile and use uWebSockets environment for win10, vs2017 Professional Edition, all dependent libraries are 32 debug version, no attempt on Linux
2, uWebSockets dependent libraries required to compile:
<1> libuv: Please refer to my previous post, this library is compiled into a static library: https://blog.csdn.net/wangdamingll/article/details/101619464
<2> uSockets: Please refer to my previous post, this library is compiled into a static library: https://blog.csdn.net/wangdamingll/article/details/102571887
<3> zlib: here is not how to compile into a library, the official website URL: http://zlib.net/
Second, the project to build vs2017
Because uWebSockets addition to uSocekts, using the C ++ 17 standard headers build, not need to compile, directly include the header file src to the project under uWebSockets address.
What is my common library file structure:
common contains four folders:
libuv: header contains libuv libraries and static libraries
uSockets: uSocekts contains header files and static libraries
zlib: zlib contains header files and static libraries
uWebSockets: directly contains all files under src uWebSockets project address
1, the new empty project vs2017
<1> Project Settings
1) VC ++ directory -> contains the directory will contain the common header files come in all folders
2) VC ++ directory -> library catalog, the common static library path for all folders include it
3) C / C ++ -> Preprocessor, add UWS_NO_ZLIB, remove the zlib function, I am here to access the zlib static library project always unsuccessful, so a direct function disabled zlib
4) C / C ++ -> language, in line mode to No, or the compiler error
5) C / C ++ -> language, C ++ language to the standard ISO C ++ 17 standard (/ std: c ++ 17)
6) Linker -> Input -> Additional Dependencies added zlib.lib
libuv.lib
uSockets_32.lib
userenv.lib
Iphlpapi.lib
psapi.lib
7) Linker -> Input -> Ignore specific default library, add LIBCMTD
<2> add source files in the project
1) Right-click the header files -> Add Existing Item, all the files under the common library uWebSockets under added to project
2) Right-click on the source file -> Add Existing Item, the item under uWebSockets address added to the project under the examples in EchoServer.cpp
<3> compile the project
Oh, you read right, wrong, if you do not have this wrong, so very congratulate you. Here at least I was unsuccessful. I checked this problem for a long time, found clues from the issue under uWebSockets project addresses. So here I can only guess (after all, the official examples of other people to run):
uWS::App().ws<PerSocketData>("/*", {.....})
{.....} content of this should be C ++ 17 standard syntax is to App.h in this structure member variable assignment:
struct WebSocketBehavior {
CompressOptions compression = DISABLED;
int maxPayloadLength = 16 * 1024;
int idleTimeout = 120;
int maxBackpressure = 1 * 1024 * 1204;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
};
However, very sorry that my vs 2017 Professional Edition does not support this syntax, although I vs 2017 language standard choice is C ++ 17 standard.
<4> way modify the source code changing said assignment
Here I only modified portions of the source code App.h:
The original template WebSocketBehavior increase in the namespace, and struct TemplatedApp {...} WebSocketBehavior commented under the category,
Then modify struct TemplatedApp {...} The second parameter under the category of ws, complete source code modifications to the following examples:
namespace {UWS
...
template <bool SSL>
struct WebSocketBehavior {
CompressOptions compression = DISABLED;
int maxPayloadLength = 16 * 1024;
int idleTimeout = 120;
int maxBackpressure = 1 * 1024 * 1204;
fu2::unique_function<void(uWS::WebSocket<false, true> *, HttpRequest *)> open = nullptr;
fu2::unique_function<void(uWS::WebSocket<false, true> *, std::string_view, uWS::OpCode)> message = nullptr;
fu2::unique_function<void(uWS::WebSocket<false, true> *)> drain = nullptr;
fu2::unique_function<void(uWS::WebSocket<false, true> *)> ping = nullptr;
fu2::unique_function<void(uWS::WebSocket<false, true> *)> pong = nullptr;
fu2::unique_function<void(uWS::WebSocket<false, true> *, int, std::string_view)> close = nullptr;
};
typedef WebSocketBehavior<false> Behavior;
typedef WebSocketBehavior<true> SSLBehavior;
...
template <bool SSL>
struct TemplatedApp {
...
/*
struct WebSocketBehavior {
CompressOptions compression = DISABLED;
int maxPayloadLength = 16 * 1024;
int idleTimeout = 120;
int maxBackpressure = 1 * 1024 * 1204;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
};
*/
...
template <typename UserData>
//TemplatedApp &&ws(std::string pattern, WebSocketBehavior &&behavior) {
TemplatedApp &&ws(std::string pattern, WebSocketBehavior<SSL> &&behavior) {
...
}
...
}
}
<5> to a modified example of official
This is my uploaded to the URL on github: https://github.com/wangdamingll/uWebSocektsDemo.git
int main() {
/* ws->getUserData returns one of these */
struct PerSocketData {
};
auto app = uWS::App();
auto behavior = uWS::Behavior();
behavior.compression= uWS::DISABLED;
behavior.maxPayloadLength = 16 * 1024;
behavior.idleTimeout = 10;
behavior.open = [](auto *ws, auto *req) {
};
behavior.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
std::cout << "message: " << message << std::endl;
ws->send(message, opCode);
};
behavior.drain = [](auto *ws) {
}?
behavior.ping = [] (auto * ws ) {
};
behavior.pong = [](auto *ws) {
};
behavior.close = [](auto *ws, int code, std::string_view message) {
};
app.ws<PerSocketData>("/*", (uWS::Behavior&&)behavior).listen(9001, [](auto *token) {
if (token) {
std::cout << "Listening on port " << 9001 << std::endl;
}
}).run();
}
Operating Example <6> recompile
2, summary
Personal sum up, this library is not easy to use, compile too much trouble, less use of official information to the demo