aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2016-08-28 14:46:31 +0900
committeripknHama <ipknhama@gmail.com>2016-08-28 14:46:31 +0900
commit967adf0de55afcb52881cdb1a7b16788c7c283db (patch)
treedbe4fe620a136bdb462a4ad29e83d6d699b3b447 /examples
parent45f6d12fd382662675000fb1c60909287733127c (diff)
downloadcrow-967adf0de55afcb52881cdb1a7b16788c7c283db.tar.gz
crow-967adf0de55afcb52881cdb1a7b16788c7c283db.zip
Add websocket feature
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt4
-rw-r--r--examples/websocket/example_ws.cpp45
-rw-r--r--examples/websocket/templates/ws.html41
3 files changed, 90 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 22139ad..1e96dea 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -15,6 +15,10 @@ add_executable(example_ssl ssl/example_ssl.cpp)
target_link_libraries(example_ssl ${Boost_LIBRARIES})
target_link_libraries(example_ssl ${CMAKE_THREAD_LIBS_INIT} ssl crypto)
+add_executable(example_websocket websocket/example_ws.cpp)
+target_link_libraries(example_websocket ${Boost_LIBRARIES})
+target_link_libraries(example_websocket ${CMAKE_THREAD_LIBS_INIT} ssl crypto)
+
add_executable(example example.cpp)
#target_link_libraries(example crow)
target_link_libraries(example ${Boost_LIBRARIES})
diff --git a/examples/websocket/example_ws.cpp b/examples/websocket/example_ws.cpp
new file mode 100644
index 0000000..7fbd5ef
--- /dev/null
+++ b/examples/websocket/example_ws.cpp
@@ -0,0 +1,45 @@
+#include "crow.h"
+#include "mustache.h"
+#include "websocket.h"
+#include <unordered_set>
+#include <mutex>
+
+
+int main()
+{
+ crow::SimpleApp app;
+
+ std::mutex mtx;;
+ std::unordered_set<crow::websocket::connection*> users;
+
+ CROW_ROUTE(app, "/ws")
+ .websocket()
+ .onopen([&](crow::websocket::connection& conn){
+ CROW_LOG_INFO << "new websocket connection";
+ std::lock_guard<std::mutex> _(mtx);
+ users.insert(&conn);
+ })
+ .onclose([&](crow::websocket::connection& conn, const std::string& reason){
+ CROW_LOG_INFO << "websocket connection closed: " << reason;
+ std::lock_guard<std::mutex> _(mtx);
+ users.erase(&conn);
+ })
+ .onmessage([&](crow::websocket::connection& /*conn*/, const std::string& data, bool is_binary){
+ std::lock_guard<std::mutex> _(mtx);
+ for(auto u:users)
+ if (is_binary)
+ u->send_binary(data);
+ else
+ u->send_text(data);
+ });
+
+ CROW_ROUTE(app, "/")
+ ([]{
+ auto page = crow::mustache::load("ws.html");
+ return page.render();
+ });
+
+ app.port(40080)
+ .multithreaded()
+ .run();
+}
diff --git a/examples/websocket/templates/ws.html b/examples/websocket/templates/ws.html
new file mode 100644
index 0000000..f6e7281
--- /dev/null
+++ b/examples/websocket/templates/ws.html
@@ -0,0 +1,41 @@
+<!doctype html>
+<html>
+<head>
+ <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
+</head>
+<body>
+ <input id="msg" type="text"></input>
+ <button id="send">
+ Send
+ </button><BR>
+ <textarea id="log" cols=100 rows=50>
+ </textarea>
+ <script>
+var sock = new WebSocket("ws://i.ipkn.me:40080/ws");
+sock.onopen = ()=>{
+ console.log('open')
+}
+sock.onerror = (e)=>{
+ console.log('error',e)
+}
+sock.onclose = ()=>{
+ console.log('close')
+}
+sock.onmessage = (e)=>{
+ $("#log").val(
+ e.data +"\n" + $("#log").val());
+}
+$("#msg").keypress(function(e){
+ if (e.which == 13)
+ {
+ sock.send($("#msg").val());
+ $("#msg").val("");
+ }
+});
+$("#send").click(()=>{
+ sock.send($("#msg").val());
+ $("#msg").val("");
+});
+ </script>
+</body>
+</html>