26#include "../ExtensionsTest.h"
34WebSocketTests::WebSocketTests()
42: _wsiSendText(nullptr)
43, _wsiSendBinary(nullptr)
45, _sendTextStatus(nullptr)
46, _sendBinaryStatus(nullptr)
47, _errorStatus(nullptr)
51 auto winSize = Director::getInstance()->getWinSize();
53 const int MARGIN = 40;
56 auto menuRequest = Menu::create();
57 menuRequest->setPosition(Vec2::ZERO);
58 addChild(menuRequest);
61 auto labelSendText = Label::createWithTTF(
"Send Text",
"fonts/arial.ttf", 20);
63 itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - SPACE));
64 menuRequest->addChild(itemSendText);
66 labelSendText = Label::createWithTTF(
"Send Multiple Text",
"fonts/arial.ttf", 20);
68 itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
69 menuRequest->addChild(itemSendText);
72 auto labelSendBinary = Label::createWithTTF(
"Send Binary",
"fonts/arial.ttf", 20);
74 itemSendBinary->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
75 menuRequest->addChild(itemSendBinary);
79 _sendTextStatus = Label::createWithTTF(
"Send Text WS is waiting...",
"fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
85 _sendBinaryStatus = Label::createWithTTF(
"Send Binary WS is waiting...",
"fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
91 _errorStatus = Label::createWithTTF(
"Error WS is waiting...",
"fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
96 auto startTestLabel = Label::createWithTTF(
"Start Test WebSocket",
"fonts/arial.ttf", 16);
137 std::vector<std::string> protocols;
138 protocols.push_back(
"myprotocol_1");
139 protocols.push_back(
"myprotocol_2");
140 if (!
_wsiSendText->init(*
this,
"wss://echo.websocket.org", &protocols,
"cacert.pem"))
149 protocols.erase(protocols.begin());
150 if (!
_wsiSendBinary->init(*
this,
"wss://echo.websocket.org", &protocols,
"cacert.pem"))
159 if (!
_wsiError->init(*
this,
"ws://invalid.urlxxxxxxxx.com"))
172 char status[256] = {0};
173 sprintf(status,
"Opened, url: %s, protocol: %s", ws->getUrl().c_str(), ws->getProtocol().c_str());
175 log(
"Websocket (%p) was opened, url: %s, protocol: %s", ws, ws->getUrl().c_str(), ws->getProtocol().c_str());
186 CCASSERT(0,
"error test will never go here.");
195 char times[100] = {0};
197 std::string textStr = std::string(
"response text msg: ")+data.bytes+
", "+times;
198 log(
"%s", textStr.c_str());
205 char times[100] = {0};
208 std::string binaryStr =
"response bin msg: ";
210 for (
int i = 0; i < data.len; ++i) {
211 if (data.bytes[i] !=
'\0')
213 binaryStr += data.bytes[i];
217 binaryStr +=
"\'\\0\'";
221 binaryStr += std::string(
", ")+times;
222 log(
"%s", binaryStr.c_str());
229 log(
"onClose: websocket instance (%p) closed.", ws);
243 _errorStatus->setString(
"Test invalid URL WS was closed");
247 log(
"WebSocketTest ref: %u", _referenceCount);
253 log(
"Error was fired, error code: %d",
static_cast<int>(error));
255 sprintf(buf,
"An error was fired, code: %d",
static_cast<int>(error));
279 if (
_wsiSendText->getReadyState() == network::WebSocket::State::OPEN)
282 _wsiSendText->send(
"Hello WebSocket, I'm a text message.");
286 std::string warningStr =
"send text websocket instance wasn't ready...";
287 log(
"%s", warningStr.c_str());
299 if (
_wsiSendText->getReadyState() == network::WebSocket::State::OPEN)
302 for (
int index = 0; index < 15; ++index) {
303 _wsiSendText->send(StringUtils::format(
"Hello WebSocket, text message index:%d", index));
308 std::string warningStr =
"send text websocket instance wasn't ready...";
309 log(
"%s", warningStr.c_str());
320 if (
_wsiSendBinary->getReadyState() == network::WebSocket::State::OPEN)
323 char buf[] =
"Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";
328 std::string warningStr =
"send binary websocket instance wasn't ready...";
329 log(
"%s", warningStr.c_str());
337 auto winSize = Director::getInstance()->getWinSize();
339 _wsiTest =
new network::WebSocket();
341 if (!
_wsiTest->init(*
this,
"ws://echo.websocket.org"))
348 Director::getInstance()->end();
352 auto menu = Menu::create(closeItem,
nullptr);
353 menu->setPosition(Vec2::ZERO);
356 auto notifyLabel = Label::createWithTTF(
"See log window, when enter there's should have\n'Websocket opened' log,\nwhen close there's should have'websocket closed' log",
"fonts/arial.ttf", 20);
358 notifyLabel->setAlignment(TextHAlignment::CENTER);
359 addChild(notifyLabel, 1);
373 log(
"Websocket (%p) opened", ws);
378 log(
"Websocket get message from %p", ws);
383 log(
"websocket (%p) closed.", ws);
392 log(
"Error was fired, error code: %d",
static_cast<int>(error));
#define ADD_TEST_CASE(__className__)
static cocos2d::Vec2 top()
static cocos2d::Vec2 center()
static cocos2d::Vec2 bottom()
static cocos2d::Vec2 left()
static cocos2d::Vec2 rightBottom()
static cocos2d::Vec2 right()
virtual void onClose(cocos2d::network::WebSocket *ws) override
virtual void onError(cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::ErrorCode &error) override
virtual void onMessage(cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::Data &data) override
cocos2d::network::WebSocket * _wsiTest
virtual void onOpen(cocos2d::network::WebSocket *ws) override
virtual ~WebSocketCloseTest()
cocos2d::network::WebSocket * _wsiError
cocos2d::Label * _sendBinaryStatus
void onMenuSendBinaryClicked(cocos2d::Ref *sender)
void onMenuSendTextClicked(cocos2d::Ref *sender)
cocos2d::network::WebSocket * _wsiSendText
cocos2d::Label * _errorStatus
cocos2d::network::WebSocket * _wsiSendBinary
cocos2d::Label * _sendTextStatus
virtual void onError(cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::ErrorCode &error) override
void startTestCallback(cocos2d::Ref *sender)
void onMenuSendMultipleTextClicked(cocos2d::Ref *sender)
virtual void onExit() override
cocos2d::Menu * _startTestMenu
virtual void onOpen(cocos2d::network::WebSocket *ws) override
virtual void onMessage(cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::Data &data) override
virtual void onClose(cocos2d::network::WebSocket *ws) override
static const char s_pathClose[]