Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Matt Godbolt |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 2 | // All rights reserved. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 5 | // modification, are permitted provided that the following conditions are met: |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 8 | // list of conditions and the following disclaimer. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 12 | // and/or other materials provided with the distribution. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | // An extraordinarily simple test which presents a web page with some buttons. |
| 27 | // Clicking on the numbered button increments the number, which is visible to |
| 28 | // other connected clients. WebSockets are used to do this: by the rather |
| 29 | // suspicious means of sending raw JavaScript commands to be executed on other |
| 30 | // clients. |
| 31 | |
| 32 | #include "seasocks/PrintfLogger.h" |
| 33 | #include "seasocks/Server.h" |
| 34 | #include "seasocks/StringUtil.h" |
| 35 | #include "seasocks/WebSocket.h" |
| 36 | #include "seasocks/util/Json.h" |
| 37 | |
| 38 | #include <cstring> |
| 39 | #include <iostream> |
| 40 | #include <memory> |
| 41 | #include <set> |
| 42 | #include <sstream> |
| 43 | #include <string> |
| 44 | |
| 45 | using namespace seasocks; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 46 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 47 | class MyHandler : public WebSocket::Handler { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 48 | public: |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 49 | explicit MyHandler(Server* server) |
| 50 | : _server(server), _currentValue(0) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 51 | setValue(1); |
| 52 | } |
| 53 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 54 | virtual void onConnect(WebSocket* connection) override { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 55 | _connections.insert(connection); |
| 56 | connection->send(_currentSetValue.c_str()); |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 57 | std::cout << "Connected: " << connection->getRequestUri() |
| 58 | << " : " << formatAddress(connection->getRemoteAddress()) |
| 59 | << "\nCredentials: " << *(connection->credentials()) << "\n"; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 62 | virtual void onData(WebSocket* connection, const char* data) override { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 63 | if (0 == strcmp("die", data)) { |
| 64 | _server->terminate(); |
| 65 | return; |
| 66 | } |
| 67 | if (0 == strcmp("close", data)) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 68 | std::cout << "Closing..\n"; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 69 | connection->close(); |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 70 | std::cout << "Closed.\n"; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 74 | const int value = std::stoi(data) + 1; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 75 | if (value > _currentValue) { |
| 76 | setValue(value); |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 77 | for (auto c : _connections) { |
| 78 | c->send(_currentSetValue.c_str()); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 83 | virtual void onDisconnect(WebSocket* connection) override { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 84 | _connections.erase(connection); |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 85 | std::cout << "Disconnected: " << connection->getRequestUri() |
| 86 | << " : " << formatAddress(connection->getRemoteAddress()) << "\n"; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private: |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 90 | std::set<WebSocket*> _connections; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 91 | Server* _server; |
| 92 | int _currentValue; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 93 | std::string _currentSetValue; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 94 | |
| 95 | void setValue(int value) { |
| 96 | _currentValue = value; |
| 97 | _currentSetValue = makeExecString("set", _currentValue); |
| 98 | } |
| 99 | }; |
| 100 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 101 | int main(int /*argc*/, const char* /*argv*/[]) { |
| 102 | auto logger = std::make_shared<PrintfLogger>(Logger::Level::Debug); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 103 | |
| 104 | Server server(logger); |
| 105 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 106 | auto handler = std::make_shared<MyHandler>(&server); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 107 | server.addWebSocketHandler("/ws", handler); |
| 108 | server.serve("src/ws_test_web", 9090); |
| 109 | return 0; |
| 110 | } |