当前位置: 首页>>代码示例>>C++>>正文


C++ WebSocket::sendFrame方法代码示例

本文整理汇总了C++中WebSocket::sendFrame方法的典型用法代码示例。如果您正苦于以下问题:C++ WebSocket::sendFrame方法的具体用法?C++ WebSocket::sendFrame怎么用?C++ WebSocket::sendFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WebSocket的用法示例。


在下文中一共展示了WebSocket::sendFrame方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: sendTextFrame

 void sendTextFrame(WebSocket& ws, const std::string& s)
 {
     ws.sendFrame(s.data(), s.size());
 }
开发者ID:wanweihua,项目名称:online,代码行数:4,代码来源:LoadTest.cpp

示例2: main


//.........这里部分代码省略.........

		


		// get response
		HTTPResponse res;
		istream& is = session.receiveResponse(res); // stream the request
		cout << res.getStatus() << " " << res.getReason() << endl; // get the status code of the transaction

		// convert the istream to sting for further processing
		istreambuf_iterator<char> eos;
		string s(istreambuf_iterator<char>(is), eos);

		const char * cc = s.c_str();
		// instantiate a rapidjson document and fill it up with the response of the negotiation request
		rapidjson::Document document; 
		document.Parse<0>(cc); 
		string token = document["ConnectionToken"].GetString(); // parse the response and get the connectionToken

		//=============================================

		//connect to signarR using the connectionToken got previously
		HTTPClientSession cs(signalr_service_url, signalr_service_port); // instantiate simple webclient
		string what = "/" + signalr_url_endpoint + "/connect?transport=webSockets&connectionToken=" + urlencode(token) + "&UID=" + my_mac + "&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D&tid=10"; // compose the request string
		HTTPRequest request(HTTPRequest::HTTP_GET, what, "HTTP/1.1"); // the protocol MUST be HTTP/1.1, as described in RFC6455; else the UPGRADE to websocket request will fail
		request.set("Host", signalr_service_url); // specify the Host header to be sent
		HTTPResponse response; // instantiate a http response
		cout << response.getStatus() << " " << response.getReason() << endl;
		WebSocket * ws = new WebSocket(cs, request, response); // instantiate a WebSocket transaction to the 'cs' session, sending the 'request' and storing the 'response'

		//sample of a message to be sent
		payload = "{\"H\":\"myhub\",\"M\":\"Send\",\"A\":[\"" + my_mac + "\",\"invoked from " + replaceInPlace(my_mac, std::string(":"), std::string("-")) + " client\"],\"I\":0}";
		// cout << endl << payload << endl ;
		ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT); // send the message to signalR using the payload and setting the type of frame to be sent as FRAME_TEXT

		flags = 1;
		// starting the receiving loop
		while( (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE ) // while websocket session open
		{
			n = ws->receiveFrame(buffer, sizeof(buffer), flags); // n is the received frame
			// flags store the response flags of the frame
			// 129 = single frame response
			// 1   = start of multi-frame response
			// 0   = continuation frame of a multi-frame response
			// 128 = FIN frame os a multi-frame response

			// signalR send data in JSON format, and it send empty messages (empty json document) on a regular basis

			if( (n != 2) && flags !=1 ) // filter out empty jsons and multiframe responses (multiframe will be treated further on)
			{
				cout << "RCV[" << msg_cnt << "]=>	" << buffer << " ===== " << unsigned(flags) << endl;
			}

			if(flags == 1){ // if I get a start frame of a multi-frame response means that I am getting something usefull from signalR
				string str(buffer);
				out += str;
				// due to flag == 1, we are expecting several frames, until we got flag == 128
				do{
					n = ws->receiveFrame(buffer, sizeof(buffer), flags);
					string str(buffer);
					out += str; // we add the next frame/frames to the out variable, to construct the whole JSON message
					str = "";
					memset(buffer, 0, sizeof buffer); // be sure to empty the buffer to don't end up with junk
				}while(flags != 128);
				cout << endl << "=> " << out << endl;
开发者ID:VirtualLife,项目名称:piSocket,代码行数:66,代码来源:piSocket.cpp


注:本文中的WebSocket::sendFrame方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。