如果您想從回顯服務器獲得回覆,您還必須確保使用Http 1.1請求。 Poco默認爲Http 1.0。
HTTPRequest request(HTTPRequest::HTTP_GET, "/",HTTPMessage::HTTP_1_1);
這是一個完整的例子,
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPClientSession.h"
#include <iostream>
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::Net::WebSocket;
int main(int args,char **argv)
{
HTTPClientSession cs("echo.websocket.org",80);
HTTPRequest request(HTTPRequest::HTTP_GET, "/?encoding=text",HTTPMessage::HTTP_1_1);
request.set("origin", "http://www.websocket.org");
HTTPResponse response;
try {
WebSocket* m_psock = new WebSocket(cs, request, response);
char *testStr="Hello echo websocket!";
char receiveBuff[256];
int len=m_psock->sendFrame(testStr,strlen(testStr),WebSocket::FRAME_TEXT);
std::cout << "Sent bytes " << len << std::endl;
int flags=0;
int rlen=m_psock->receiveFrame(receiveBuff,256,flags);
std::cout << "Received bytes " << rlen << std::endl;
std::cout << receiveBuff << std::endl;
m_psock->close();
} catch (std::exception &e) {
std::cout << "Exception " << e.what();
}
}
我會問,關於[波蘇論壇](http://pocoproject.org/forum/) –
我這樣做,但沒有幫助:/ – Moonlit