示例#1
0
	void ApiRouter::handleSocketRequest(const string& aRequestBody, WebSocketPtr& aSocket, bool aIsSecure) noexcept {

		dcdebug("Received socket request: %s\n", aRequestBody.c_str());
		bool authenticated = aSocket->getSession() != nullptr;

		json responseJsonData, errorJson;
		websocketpp::http::status_code::value code;
		int callbackId = -1;

		try {
			json requestJson = json::parse(aRequestBody);
			auto cb = requestJson.find("callback_id");
			if (cb != requestJson.end()) {
				callbackId = cb.value();
			}

			ApiRequest apiRequest(requestJson["path"], requestJson["method"], responseJsonData, errorJson);
			apiRequest.parseSocketRequestJson(requestJson);
			apiRequest.setSession(aSocket->getSession());

			apiRequest.validate();

			code = handleRequest(apiRequest, aIsSecure, aSocket, aSocket->getIp());
		} catch (const std::exception& e) {
			errorJson = { "message", "Parsing failed: " + string(e.what()) };
			code = websocketpp::http::status_code::bad_request;
		}

		if (callbackId > 0 || !errorJson.is_null()) {
			aSocket->sendApiResponse(responseJsonData, errorJson, code, callbackId);
		}
	}
示例#2
0
	void WebServerManager::on_close_socket(websocketpp::connection_hdl hdl) {
		WebSocketPtr socket = nullptr;

		{
			WLock l(cs);
			auto s = sockets.find(hdl);
			dcassert(s != sockets.end());
			if (s == sockets.end()) {
				return;
			}

			socket = s->second;
			sockets.erase(s);
		}

		if (socket->getSession()) {
			socket->getSession()->onSocketDisconnected();
		}
	}