blob: 9a6440aca0784a9f4325e446c56df02981352e6b [file] [log] [blame]
Comran Morshedfe7f9ea2015-02-19 23:52:57 +00001// Copyright (c) 2013, Matt Godbolt
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
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
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#include "embedded.h"
46
47using namespace seasocks;
48using namespace std;
49
50class MyHandler: public WebSocket::Handler {
51public:
52 MyHandler(Server* server) : _server(server), _currentValue(0) {
53 setValue(1);
54 }
55
56 virtual void onConnect(WebSocket* connection) {
57 _connections.insert(connection);
58 connection->send(_currentSetValue.c_str());
59 cout << "Connected: " << connection->getRequestUri()
60 << " : " << formatAddress(connection->getRemoteAddress())
61 << endl;
62 cout << "Credentials: " << *(connection->credentials()) << endl;
63 }
64
65 virtual void onData(WebSocket* connection, const char* data) {
66 if (0 == strcmp("die", data)) {
67 _server->terminate();
68 return;
69 }
70 if (0 == strcmp("close", data)) {
71 cout << "Closing.." << endl;
72 connection->close();
73 cout << "Closed." << endl;
74 return;
75 }
76
77 int value = atoi(data) + 1;
78 if (value > _currentValue) {
79 setValue(value);
80 for (auto connection : _connections) {
81 connection->send(_currentSetValue.c_str());
82 }
83 }
84 }
85
86 virtual void onDisconnect(WebSocket* connection) {
87 _connections.erase(connection);
88 cout << "Disconnected: " << connection->getRequestUri()
89 << " : " << formatAddress(connection->getRemoteAddress())
90 << endl;
91 }
92
93private:
94 set<WebSocket*> _connections;
95 Server* _server;
96 int _currentValue;
97 string _currentSetValue;
98
99 void setValue(int value) {
100 _currentValue = value;
101 _currentSetValue = makeExecString("set", _currentValue);
102 }
103};
104
105int main(int argc, const char* argv[]) {
106 shared_ptr<Logger> logger(new PrintfLogger(Logger::DEBUG));
107
108 Server server(logger);
109
110 shared_ptr<MyHandler> handler(new MyHandler(&server));
111 server.addWebSocketHandler("/ws", handler);
112 server.serve("web_test", 8080);
113 return 0;
114}