-
Notifications
You must be signed in to change notification settings - Fork 279
Sample Client/Server + Optional Sec-WebSocket-Protocol + No More Visual Studio Warnings #88
base: master
Are you sure you want to change the base?
Conversation
…d char, possible loss of data
whether they say "Web Socket Protocol Handshake" or "Switching Protocols" or anything else.
Thank you. I'll have to look into this in more detail, but first some general comments/thoughts:
Sorry for the brick wall of comments above, but it was a large PR:) Thank you for splitting it into several commits though, and your extensive comments! |
I completely forgot: you can supress the MSVC warnings from the headers by including them as system includes in your project. |
I just added the possibility to add additional headers in 06ce3a0. Here is an example: #include "client_ws.hpp"
#include "server_ws.hpp"
using namespace std;
using WsServer = SimpleWeb::SocketServer<SimpleWeb::WS>;
using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>;
int main() {
// WebSocket (WS)-server at port 8080 using 1 thread
WsServer server;
server.config.port = 8080;
auto &endpoint = server.endpoint["^/?$"];
endpoint.on_open = [](shared_ptr<WsServer::Connection> connection) {
cout << connection->header.find("Sec-WebSocket-Protocol")->second << endl;
};
thread server_thread([&server]() {
server.start();
});
this_thread::sleep_for(chrono::seconds(1));
WsClient client("localhost:8080");
client.config.header = {{"Sec-WebSocket-Protocol", "some_protocol"}};
client.start();
server_thread.join();
}
// Output:
// some_protocol |
MSVC vs CLANGYour points about MSVC and CLANG make a lot of sense. I just wish I could convince the rest of the office to switch. I'll be leaving soon, so I guess their in charge of their own destiny now. System HeadersThinking back, I tried the system header thing. I think there was a bug in either CMake or Visual Studio that prevented it from working. I'll try it again--it's been a while. Added WarningsSome of the weird warning stuff comes from the fact that I grabbed a lot of that CMakeLists.txt file from one we were using elsewhere. Their inclusion was an oversight on my part. SamplesI think that it's conceptually simpler to have a sample client that is separate from the sample server, it also makes integration testing easier. I haven't been using your examples (except for inspiration) for that reason. On the other hand, being able to test data flow without a human in the middle banging on a keyboard--that sounds good too. My samples have served their purpose (to be provided to other teams)--I won't be sad if you don't want to adopt their extra complexity. If you think others might want them, I could pull them into a separate repo which includes this one as a submodule. Whether you'd then want to link to that repo in your docs would be your call. Optional Arbitrary HeadersThat looks much better than what I had. |
Now that I have modernised the cmake files, I made an attempt to support MSVC in this commit that I just pushed to master: 3d18c3b |
Sorry about the close/reopen, I clicked the wrong button |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran into the same warning C4309 on Visual Studio 17 for 64 bit configuration.
I made the same fix in my copy of this file.
But without replacing 126 + 128 with neither 0xFEu nor 0xFDu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous comment is about commit
Sample Client/Server + Optional Sec-WebSocket-Protocol + No More Visual Studio Warnings
related to file client_ws.hpp.
Sample Client/Server + Optional Sec-WebSocket-Protocol + No More Visual Studio Warnings
I've been using Simple-WebSocket-Server for some time, and I've collected several changes. I will be leaving my current company in mid October, and won't be using Simple-WebSocket-Server after that. I figured it would be good to make those changes available in case you'd like them.
The changes fall in three categories:
Removing Visual Studio warnings
I understand that you don't recommend using Windows as a server. Neither do I. Most of my office develops on Windows, however, so it was necessary to get Simple-WebSocket-Server running on windows. We treat warnings as errors, so instead of silencing them, I went into your code and fixed them. All changes struck me as trivial. I also tested these changes on Linux--but haven't had the opportunity on any other OS.
Creating a sample client/server pair
In order to test your client against other servers, or your server with other clients, it was helpful to create separate test binaries so that we could mix/match. I packaged these in
./sample/
and wrote some documentation for them.Adding support for Sec-WebSocket-Protocol
We ran across a server that required that this optional field be set (see #86). I added an optional call to SocketClient which allows the user to set this field. The server in this repo ignores the field (apart from including it in the response), but it is useful to be able to set the field when communicating with libwebsockets (which, I'm told, prefers to use the protocol instead of the http resource).
While doing this, I also learned that libwebsockets likes to respond a bit differently (still an HTTP 101, but then it says "Switching Protocols" instead of "Web Socket Protocol Handshake". I changed SocketClientBase so that it just looks for the 101, and ignores the message--which prevents an error.
Testing
These changes were tested with: