Skip to content

Commit

Permalink
simplify websocket interface
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 30, 2018
1 parent d32054e commit acd945c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,24 @@ cinatra目前支持了multipart和octet-stream格式的上传。
server.set_http_handler<GET, POST>("/ws", [](request& req, response& res) {
assert(req.get_content_type() == content_type::websocket);

req.on(data_proc_state::data_begin, [](request& req){
req.on(ws_open, [](request& req){
std::cout << "websocket start" << std::endl;
});

req.on(data_proc_state::data_continue, [](request& req) {
req.on(ws_message, [](request& req) {
auto part_data = req.get_part_data();
//echo
std::string str = std::string(part_data.data(), part_data.length());
req.get_conn()->send_ws_string(std::move(str));
std::cout << part_data.data() << std::endl;
});

req.on(data_proc_state::data_close, [](request& req) {
req.on(ws_close, [](request& req) {
std::cout << "websocket close" << std::endl;
});

req.on(data_proc_state::data_error, [](request& req) {
std::cout << "network error" << std::endl;
req.on(ws_error, [](request& req) {
std::cout << "websocket error" << std::endl;
});
});

Expand Down
5 changes: 5 additions & 0 deletions connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,4 +1075,9 @@ namespace cinatra {
const http_handler& http_handler_;
std::any tag_;
};

inline constexpr data_proc_state ws_open = data_proc_state::data_begin;
inline constexpr data_proc_state ws_message = data_proc_state::data_continue;
inline constexpr data_proc_state ws_close = data_proc_state::data_close;
inline constexpr data_proc_state ws_error = data_proc_state::data_error;
}
10 changes: 5 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,24 +188,24 @@ int main() {
server.set_http_handler<GET, POST>("/ws", [](request& req, response& res) {
assert(req.get_content_type() == content_type::websocket);

req.on(data_proc_state::data_begin, [](request& req){
req.on(ws_open, [](request& req){
std::cout << "websocket start" << std::endl;
});

req.on(data_proc_state::data_continue, [](request& req) {
req.on(ws_message, [](request& req) {
auto part_data = req.get_part_data();
//echo
std::string str = std::string(part_data.data(), part_data.length());
req.get_conn()->send_ws_string(std::move(str));
std::cout << part_data.data() << std::endl;
});

req.on(data_proc_state::data_close, [](request& req) {
req.on(ws_close, [](request& req) {
std::cout << "websocket close" << std::endl;
});

req.on(data_proc_state::data_error, [](request& req) {
std::cout << "network error" << std::endl;
req.on(ws_error, [](request& req) {
std::cout << "websocket error" << std::endl;
});
});

Expand Down

0 comments on commit acd945c

Please sign in to comment.