-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws.cpp
More file actions
132 lines (122 loc) · 3.59 KB
/
ws.cpp
File metadata and controls
132 lines (122 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerRequestImpl.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/HTTPClientSession.h"
#include <iostream>
#include <string>
#include <vector>
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerRequestImpl;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerParams;
using Poco::Net::HTTPServer;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
using Poco::Net::ServerSocket;
using Poco::Util::ServerApplication;
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using std::vector;
using std::string;
using std::cout;
using std::stoi;
char message[1024];
int size = 0;
int flag;
bool wait = true;
class WebSocketRequestHandler: public Poco::Net::HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
try
{
WebSocket ws(request, response);
char buffer[1024];
int flags = 0;
int n,len;
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
if(n == 2){
do
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
cout<<"Frame recieved"<<"\n"<<buffer;
strcpy(message,buffer);
size = n;
flag = flags;
wait = false;
}while (n > 0 || (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
}
else if(n == 4){
do
{
if(size != 0 && !wait){
ws.sendFrame(message, size, flags);
wait = true;
}
}while (true);
}
}
catch (WebSocketException& exc)
{
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
};
class WebSocketRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
{
public:
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
return new WebSocketRequestHandler;
}
};
class MyServerApp : public ServerApplication {
protected:
int main(const vector<string> &args) {
ServerSocket ss(stoi(args[0]), 4000);
HTTPServer server(new WebSocketRequestHandlerFactory, ss, new HTTPServerParams);
cout<<"starting up the server"<<"\n";
server.start();
waitForTerminationRequest();
server.stop();
return Application::EXIT_OK;
}
};
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " port " << std::endl;
return 1;
}
MyServerApp app;
return app.run(argc, argv);
}