PuzzleSDK
WebSocketTest类 参考

#include <WebSocketTest.h>

+ 类 WebSocketTest 继承关系图:
+ WebSocketTest 的协作图:

Public 成员函数

 CREATE_FUNC (WebSocketTest)
 
 WebSocketTest ()
 
virtual ~WebSocketTest ()
 
virtual void onExit () override
 
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
 
virtual void onError (cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::ErrorCode &error) override
 
void onMenuSendTextClicked (cocos2d::Ref *sender)
 
void onMenuSendMultipleTextClicked (cocos2d::Ref *sender)
 
void onMenuSendBinaryClicked (cocos2d::Ref *sender)
 
virtual std::string title () const override
 
void startTestCallback (cocos2d::Ref *sender)
 
- Public 成员函数 继承自 TestCase
 TestCase ()
 
 ~TestCase ()
 
virtual std::string subtitle () const
 
virtual Type getTestType () const
 
virtual float getDuration () const
 
virtual std::string getExpectedOutput () const
 
virtual std::string getActualOutput () const
 
virtual void restartTestCallback (cocos2d::Ref *sender)
 
virtual void nextTestCallback (cocos2d::Ref *sender)
 
virtual void priorTestCallback (cocos2d::Ref *sender)
 
virtual void onBackCallback (cocos2d::Ref *sender)
 
void setTestSuite (TestSuite *testSuite)
 
TestSuitegetTestSuite () const
 
float getRunTime () const
 
void setTestCaseName (const std::string &name)
 
std::string getTestCaseName () const
 
const cocos2d::Label * getSubtitleLable () const
 
const cocos2d::MenuItemImage * getRestartTestItem () const
 
virtual void onEnter () override
 

Private 属性

cocos2d::network::WebSocket * _wsiSendText
 
cocos2d::network::WebSocket * _wsiSendBinary
 
cocos2d::network::WebSocket * _wsiError
 
cocos2d::Label * _sendTextStatus
 
cocos2d::Label * _sendBinaryStatus
 
cocos2d::Label * _errorStatus
 
cocos2d::Menu * _startTestMenu
 
int _sendTextTimes
 
int _sendBinaryTimes
 

额外继承的成员函数

- Public 类型 继承自 TestCase
enum class  Type { ROBUSTNESS , UNIT , GRAPHICAL_STATIC , MANUAL }
 
- Public 属性 继承自 TestCase
CC_CONSTRUCTOR_ACCESS __pad0__: virtual bool init() override
 
- Protected 属性 继承自 TestCase
cocos2d::MenuItemImage * _priorTestItem
 
cocos2d::MenuItemImage * _restartTestItem
 
cocos2d::MenuItemImage * _nextTestItem
 
cocos2d::Label * _titleLabel
 
cocos2d::Label * _subtitleLabel
 

详细描述

在文件 WebSocketTest.h43 行定义.

构造及析构函数说明

◆ WebSocketTest()

WebSocketTest::WebSocketTest ( )

在文件 WebSocketTest.cpp41 行定义.

42: _wsiSendText(nullptr)
43, _wsiSendBinary(nullptr)
44, _wsiError(nullptr)
45, _sendTextStatus(nullptr)
46, _sendBinaryStatus(nullptr)
47, _errorStatus(nullptr)
50{
51 auto winSize = Director::getInstance()->getWinSize();
52
53 const int MARGIN = 40;
54 const int SPACE = 35;
55
56 auto menuRequest = Menu::create();
57 menuRequest->setPosition(Vec2::ZERO);
58 addChild(menuRequest);
59
60 // Send Text
61 auto labelSendText = Label::createWithTTF("Send Text", "fonts/arial.ttf", 20);
62 auto itemSendText = MenuItemLabel::create(labelSendText, CC_CALLBACK_1(WebSocketTest::onMenuSendTextClicked, this));
63 itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - SPACE));
64 menuRequest->addChild(itemSendText);
65
66 labelSendText = Label::createWithTTF("Send Multiple Text", "fonts/arial.ttf", 20);
67 itemSendText = MenuItemLabel::create(labelSendText, CC_CALLBACK_1(WebSocketTest::onMenuSendMultipleTextClicked, this));
68 itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
69 menuRequest->addChild(itemSendText);
70
71 // Send Binary
72 auto labelSendBinary = Label::createWithTTF("Send Binary", "fonts/arial.ttf", 20);
73 auto itemSendBinary = MenuItemLabel::create(labelSendBinary, CC_CALLBACK_1(WebSocketTest::onMenuSendBinaryClicked, this));
74 itemSendBinary->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
75 menuRequest->addChild(itemSendBinary);
76
77
78 // Send Text Status Label
79 _sendTextStatus = Label::createWithTTF("Send Text WS is waiting...", "fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
80 _sendTextStatus->setAnchorPoint(Vec2(0, 0));
81 _sendTextStatus->setPosition(Vec2(VisibleRect::left().x, VisibleRect::rightBottom().y + 25));
82 this->addChild(_sendTextStatus);
83
84 // Send Binary Status Label
85 _sendBinaryStatus = Label::createWithTTF("Send Binary WS is waiting...", "fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
86 _sendBinaryStatus->setAnchorPoint(Vec2(0, 0));
87 _sendBinaryStatus->setPosition(Vec2(VisibleRect::left().x + 160, VisibleRect::rightBottom().y + 25));
88 this->addChild(_sendBinaryStatus);
89
90 // Error Label
91 _errorStatus = Label::createWithTTF("Error WS is waiting...", "fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
92 _errorStatus->setAnchorPoint(Vec2(0, 0));
93 _errorStatus->setPosition(Vec2(VisibleRect::left().x + 320, VisibleRect::rightBottom().y + 25));
94 this->addChild(_errorStatus);
95
96 auto startTestLabel = Label::createWithTTF("Start Test WebSocket", "fonts/arial.ttf", 16);
97 auto startTestItem = MenuItemLabel::create(startTestLabel, CC_CALLBACK_1(WebSocketTest::startTestCallback, this));
98 startTestItem->setPosition(Vec2(VisibleRect::center().x - 150, VisibleRect::bottom().y + 150));
99 _startTestMenu = Menu::create(startTestItem, nullptr);
100 _startTestMenu->setPosition(Vec2::ZERO);
101 this->addChild(_startTestMenu, 1);
102}
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45
static cocos2d::Vec2 rightBottom()
Definition: VisibleRect.cpp:93
cocos2d::network::WebSocket * _wsiError
Definition: WebSocketTest.h:70
cocos2d::Label * _sendBinaryStatus
Definition: WebSocketTest.h:73
void onMenuSendBinaryClicked(cocos2d::Ref *sender)
void onMenuSendTextClicked(cocos2d::Ref *sender)
cocos2d::network::WebSocket * _wsiSendText
Definition: WebSocketTest.h:68
cocos2d::Label * _errorStatus
Definition: WebSocketTest.h:74
cocos2d::network::WebSocket * _wsiSendBinary
Definition: WebSocketTest.h:69
cocos2d::Label * _sendTextStatus
Definition: WebSocketTest.h:72
void startTestCallback(cocos2d::Ref *sender)
void onMenuSendMultipleTextClicked(cocos2d::Ref *sender)
cocos2d::Menu * _startTestMenu
Definition: WebSocketTest.h:75

引用了 _errorStatus, _sendBinaryStatus, _sendTextStatus, _startTestMenu, VisibleRect::bottom(), VisibleRect::center(), VisibleRect::left(), onMenuSendBinaryClicked(), onMenuSendMultipleTextClicked(), onMenuSendTextClicked(), VisibleRect::rightBottom() , 以及 startTestCallback().

+ 函数调用图:

◆ ~WebSocketTest()

WebSocketTest::~WebSocketTest ( )
virtual

在文件 WebSocketTest.cpp104 行定义.

105{
106
107}

成员函数说明

◆ CREATE_FUNC()

WebSocketTest::CREATE_FUNC ( WebSocketTest  )

◆ onClose()

void WebSocketTest::onClose ( cocos2d::network::WebSocket *  ws)
overridevirtual

在文件 WebSocketTest.cpp227 行定义.

228{
229 log("onClose: websocket instance (%p) closed.", ws);
230 if (ws == _wsiSendText)
231 {
232 _wsiSendText = nullptr;
233 _sendTextStatus->setString("Send Text WS was closed");
234 }
235 else if (ws == _wsiSendBinary)
236 {
237 _wsiSendBinary = nullptr;
238 _sendBinaryStatus->setString("Send Binary WS was closed");
239 }
240 else if (ws == _wsiError)
241 {
242 _wsiError = nullptr;
243 _errorStatus->setString("Test invalid URL WS was closed");
244 }
245 // Delete websocket instance.
246 CC_SAFE_DELETE(ws);
247 log("WebSocketTest ref: %u", _referenceCount);
248 release();
249}

引用了 _errorStatus, _sendBinaryStatus, _sendTextStatus, _wsiError, _wsiSendBinary , 以及 _wsiSendText.

◆ onError()

void WebSocketTest::onError ( cocos2d::network::WebSocket *  ws,
const cocos2d::network::WebSocket::ErrorCode &  error 
)
overridevirtual

在文件 WebSocketTest.cpp251 行定义.

252{
253 log("Error was fired, error code: %d", static_cast<int>(error));
254 char buf[100] = {0};
255 sprintf(buf, "An error was fired, code: %d", static_cast<int>(error));
256
257 if (ws == _wsiSendText)
258 {
259 _sendTextStatus->setString(buf);
260 }
261 else if (ws == _wsiSendBinary)
262 {
263 _sendBinaryStatus->setString(buf);
264 }
265 else if (ws == _wsiError)
266 {
267 _errorStatus->setString(buf);
268 }
269}

引用了 _errorStatus, _sendBinaryStatus, _sendTextStatus, _wsiError, _wsiSendBinary , 以及 _wsiSendText.

◆ onExit()

void WebSocketTest::onExit ( )
overridevirtual

在文件 WebSocketTest.cpp109 行定义.

110{
111 if (_wsiSendText)
112 {
113 _wsiSendText->closeAsync();
114 }
115
116 if (_wsiSendBinary)
117 {
118 _wsiSendBinary->closeAsync();
119 }
120
121 if (_wsiError)
122 {
123 _wsiError->closeAsync();
124 }
125 Node::onExit();
126}

引用了 _wsiError, _wsiSendBinary , 以及 _wsiSendText.

◆ onMenuSendBinaryClicked()

void WebSocketTest::onMenuSendBinaryClicked ( cocos2d::Ref *  sender)

在文件 WebSocketTest.cpp314 行定义.

315{
316 if (! _wsiSendBinary) {
317 return;
318 }
319
320 if (_wsiSendBinary->getReadyState() == network::WebSocket::State::OPEN)
321 {
322 _sendBinaryStatus->setString("Send Binary WS is waiting...");
323 char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";
324 _wsiSendBinary->send((unsigned char*)buf, sizeof(buf));
325 }
326 else
327 {
328 std::string warningStr = "send binary websocket instance wasn't ready...";
329 log("%s", warningStr.c_str());
330 _sendBinaryStatus->setString(warningStr.c_str());
331 }
332}

引用了 _sendBinaryStatus , 以及 _wsiSendBinary.

被这些函数引用 WebSocketTest().

+ 这是这个函数的调用关系图:

◆ onMenuSendMultipleTextClicked()

void WebSocketTest::onMenuSendMultipleTextClicked ( cocos2d::Ref *  sender)

在文件 WebSocketTest.cpp292 行定义.

293{
294 if (! _wsiSendText)
295 {
296 return;
297 }
298
299 if (_wsiSendText->getReadyState() == network::WebSocket::State::OPEN)
300 {
301 _sendTextStatus->setString("Send Multiple Text WS is waiting...");
302 for (int index = 0; index < 15; ++index) {
303 _wsiSendText->send(StringUtils::format("Hello WebSocket, text message index:%d", index));
304 }
305 }
306 else
307 {
308 std::string warningStr = "send text websocket instance wasn't ready...";
309 log("%s", warningStr.c_str());
310 _sendTextStatus->setString(warningStr.c_str());
311 }
312}

引用了 _sendTextStatus , 以及 _wsiSendText.

被这些函数引用 WebSocketTest().

+ 这是这个函数的调用关系图:

◆ onMenuSendTextClicked()

void WebSocketTest::onMenuSendTextClicked ( cocos2d::Ref *  sender)

在文件 WebSocketTest.cpp272 行定义.

273{
274 if (! _wsiSendText)
275 {
276 return;
277 }
278
279 if (_wsiSendText->getReadyState() == network::WebSocket::State::OPEN)
280 {
281 _sendTextStatus->setString("Send Text WS is waiting...");
282 _wsiSendText->send("Hello WebSocket, I'm a text message.");
283 }
284 else
285 {
286 std::string warningStr = "send text websocket instance wasn't ready...";
287 log("%s", warningStr.c_str());
288 _sendTextStatus->setString(warningStr.c_str());
289 }
290}

引用了 _sendTextStatus , 以及 _wsiSendText.

被这些函数引用 WebSocketTest().

+ 这是这个函数的调用关系图:

◆ onMessage()

void WebSocketTest::onMessage ( cocos2d::network::WebSocket *  ws,
const cocos2d::network::WebSocket::Data &  data 
)
overridevirtual

在文件 WebSocketTest.cpp190 行定义.

191{
192 if (!data.isBinary)
193 {
195 char times[100] = {0};
196 sprintf(times, "%d", _sendTextTimes);
197 std::string textStr = std::string("response text msg: ")+data.bytes+", "+times;
198 log("%s", textStr.c_str());
199
200 _sendTextStatus->setString(textStr.c_str());
201 }
202 else
203 {
205 char times[100] = {0};
206 sprintf(times, "%d", _sendBinaryTimes);
207
208 std::string binaryStr = "response bin msg: ";
209
210 for (int i = 0; i < data.len; ++i) {
211 if (data.bytes[i] != '\0')
212 {
213 binaryStr += data.bytes[i];
214 }
215 else
216 {
217 binaryStr += "\'\\0\'";
218 }
219 }
220
221 binaryStr += std::string(", ")+times;
222 log("%s", binaryStr.c_str());
223 _sendBinaryStatus->setString(binaryStr.c_str());
224 }
225}

引用了 _sendBinaryStatus, _sendBinaryTimes, _sendTextStatus , 以及 _sendTextTimes.

◆ onOpen()

void WebSocketTest::onOpen ( cocos2d::network::WebSocket *  ws)
overridevirtual

在文件 WebSocketTest.cpp170 行定义.

171{
172 char status[256] = {0};
173 sprintf(status, "Opened, url: %s, protocol: %s", ws->getUrl().c_str(), ws->getProtocol().c_str());
174
175 log("Websocket (%p) was opened, url: %s, protocol: %s", ws, ws->getUrl().c_str(), ws->getProtocol().c_str());
176 if (ws == _wsiSendText)
177 {
178 _sendTextStatus->setString(status);
179 }
180 else if (ws == _wsiSendBinary)
181 {
182 _sendBinaryStatus->setString(status);
183 }
184 else if (ws == _wsiError)
185 {
186 CCASSERT(0, "error test will never go here.");
187 }
188}

引用了 _sendBinaryStatus, _sendTextStatus, _wsiError, _wsiSendBinary , 以及 _wsiSendText.

◆ startTestCallback()

void WebSocketTest::startTestCallback ( cocos2d::Ref *  sender)

在文件 WebSocketTest.cpp128 行定义.

129{
130 removeChild(_startTestMenu);
131 _startTestMenu = nullptr;
132
133 _wsiSendText = new network::WebSocket();
134 _wsiSendBinary = new network::WebSocket();
135 _wsiError = new network::WebSocket();
136
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"))
141 {
142 CC_SAFE_DELETE(_wsiSendText);
143 }
144 else
145 {
146 retain(); // Retain self to avoid WebSocketTest instance be deleted immediately, it will be released in WebSocketTest::onClose.
147 }
148
149 protocols.erase(protocols.begin());
150 if (!_wsiSendBinary->init(*this, "wss://echo.websocket.org", &protocols, "cacert.pem"))
151 {
152 CC_SAFE_DELETE(_wsiSendBinary);
153 }
154 else
155 {
156 retain(); // Retain self to avoid WebSocketTest instance be deleted immediately, it will be released in WebSocketTest::onClose.
157 }
158
159 if (!_wsiError->init(*this, "ws://invalid.urlxxxxxxxx.com"))
160 {
161 CC_SAFE_DELETE(_wsiError);
162 }
163 else
164 {
165 retain(); // Retain self to avoid WebSocketTest instance be deleted immediately, it will be released in WebSocketTest::onClose.
166 }
167}

引用了 _startTestMenu, _wsiError, _wsiSendBinary , 以及 _wsiSendText.

被这些函数引用 WebSocketTest().

+ 这是这个函数的调用关系图:

◆ title()

virtual std::string WebSocketTest::title ( ) const
inlineoverridevirtual

重载 TestCase .

在文件 WebSocketTest.h64 行定义.

64{ return "WebSocket Test"; }

类成员变量说明

◆ _errorStatus

cocos2d::Label* WebSocketTest::_errorStatus
private

在文件 WebSocketTest.h74 行定义.

被这些函数引用 onClose(), onError() , 以及 WebSocketTest().

◆ _sendBinaryStatus

cocos2d::Label* WebSocketTest::_sendBinaryStatus
private

在文件 WebSocketTest.h73 行定义.

被这些函数引用 onClose(), onError(), onMenuSendBinaryClicked(), onMessage(), onOpen() , 以及 WebSocketTest().

◆ _sendBinaryTimes

int WebSocketTest::_sendBinaryTimes
private

在文件 WebSocketTest.h78 行定义.

被这些函数引用 onMessage().

◆ _sendTextStatus

cocos2d::Label* WebSocketTest::_sendTextStatus
private

◆ _sendTextTimes

int WebSocketTest::_sendTextTimes
private

在文件 WebSocketTest.h77 行定义.

被这些函数引用 onMessage().

◆ _startTestMenu

cocos2d::Menu* WebSocketTest::_startTestMenu
private

在文件 WebSocketTest.h75 行定义.

被这些函数引用 startTestCallback() , 以及 WebSocketTest().

◆ _wsiError

cocos2d::network::WebSocket* WebSocketTest::_wsiError
private

在文件 WebSocketTest.h70 行定义.

被这些函数引用 onClose(), onError(), onExit(), onOpen() , 以及 startTestCallback().

◆ _wsiSendBinary

cocos2d::network::WebSocket* WebSocketTest::_wsiSendBinary
private

在文件 WebSocketTest.h69 行定义.

被这些函数引用 onClose(), onError(), onExit(), onMenuSendBinaryClicked(), onOpen() , 以及 startTestCallback().

◆ _wsiSendText

cocos2d::network::WebSocket* WebSocketTest::_wsiSendText
private

该类的文档由以下文件生成: