PuzzleSDK
WebSocketDelayTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "WebSocketDelayTest.h"
26#include "../ExtensionsTest.h"
27#include "testResource.h"
28
31
32#define SEND_TEXT_TIMES 100
33
35: _wsiSendText(nullptr)
36, _sendTextStatus(nullptr)
37, _progressStatus(nullptr)
38, _sendTextTimes(0)
39{
40 auto winSize = Director::getInstance()->getWinSize();
41
42 const int MARGIN = 40;
43 const int SPACE = 35;
44
45 auto menuRequest = Menu::create();
46 menuRequest->setPosition(Vec2::ZERO);
47 addChild(menuRequest);
48
49 // Send Text
50 char cmdLabel[60] = { 0 };
51 snprintf(cmdLabel, 60, "Send %d Text", SEND_TEXT_TIMES);
52 auto labelSendText = Label::createWithTTF(cmdLabel, "fonts/arial.ttf", 20);
53 auto itemSendText = MenuItemLabel::create(labelSendText, CC_CALLBACK_1(WebSocketDelayTest::onMenuSendTextClicked, this));
54 itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - SPACE));
55 menuRequest->addChild(itemSendText);
56
57 // Send Text Status Label
58 _sendTextStatus = Label::createWithTTF("Waiting connection...", "fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
59 _sendTextStatus->setAnchorPoint(Vec2(0, 0));
60 _sendTextStatus->setPosition(Vec2(VisibleRect::left().x, VisibleRect::rightBottom().y + 25));
61 this->addChild(_sendTextStatus);
62
63
64 // Error Label
65 _progressStatus = Label::createWithTTF(".", "fonts/arial.ttf", 16, Size(160, 100), TextHAlignment::CENTER, TextVAlignment::TOP);
66 _progressStatus->setAnchorPoint(Vec2(0, 0));
67 _progressStatus->setPosition(Vec2(VisibleRect::left().x + 320, VisibleRect::rightBottom().y + 25));
68 this->addChild(_progressStatus);
69
70 auto startTestLabel = Label::createWithTTF("DO Connect!", "fonts/arial.ttf", 16);
71 auto startTestItem = MenuItemLabel::create(startTestLabel, CC_CALLBACK_1(WebSocketDelayTest::startTestCallback, this));
72 startTestItem->setPosition(Vec2(VisibleRect::center().x - 150, VisibleRect::bottom().y + 150));
73 _startTestMenu = Menu::create(startTestItem, nullptr);
74 _startTestMenu->setPosition(Vec2::ZERO);
75 this->addChild(_startTestMenu, 1);
76}
77
79{
80
81}
82
84{
85 if (_wsiSendText)
86 {
87 _wsiSendText->closeAsync();
88 }
89
90 Node::onExit();
91}
92
94{
95 removeChild(_startTestMenu);
96 _startTestMenu = nullptr;
97
98 _wsiSendText = new network::WebSocket();
99
100 std::vector<std::string> protocols;
101 protocols.push_back("myprotocol_1");
102 protocols.push_back("myprotocol_2");
103 if (!_wsiSendText->init(*this, "wss://echo.websocket.org", &protocols, "cacert.pem"))
104 {
105 CC_SAFE_DELETE(_wsiSendText);
106 }
107 else
108 {
109 retain(); // Retain self to avoid WebSocketDelayTest instance be deleted immediately, it will be released in WebSocketDelayTest::onClose.
110
111 }
112
113}
114
116{
117 _sendTextTimes += 1;
119 {
120 _sendTextStatus->setString("Test Done!");
121 return;
122 }
123
124 char statueBuffer[80] = { 0 };
125 snprintf(statueBuffer, 80, "Sending #%d/%d text", _sendTextTimes, SEND_TEXT_TIMES);
126 _sendTextStatus->setString(statueBuffer);
128 _wsiSendText->send("Hello WebSocket, I'm a text message.");
129}
130
132{
134 if(_sendTimeMircoSec > 0)
136 doSendText(); //send next
137}
138
139// Delegate methods
140void WebSocketDelayTest::onOpen(network::WebSocket* ws)
141{
142 char status[256] = {0};
143 sprintf(status, "Opened, url: %s, protocol: %s", ws->getUrl().c_str(), ws->getProtocol().c_str());
144
145 log("Websocket (%p) was opened, url: %s, protocol: %s", ws, ws->getUrl().c_str(), ws->getProtocol().c_str());
146 if (ws == _wsiSendText)
147 {
148 _sendTextStatus->setString(status);
149
150 }
151}
152
153void WebSocketDelayTest::onMessage(network::WebSocket* ws, const network::WebSocket::Data& data)
154{
155 if (!data.isBinary)
156 {
158 char times[100] = {0};
159 sprintf(times, "%d", _receiveTextTimes);
160 std::string textStr = std::string("response text msg: ")+data.bytes+", "+times;
161 log("%s", textStr.c_str());
163 memset(times, 0, 100);
164 snprintf(times, 100, "total delay %f seconds", (float)(_totalDelayMircoSec/ 1000000.0));
165 _progressStatus->setString(times);
166 }
167}
168
169void WebSocketDelayTest::onClose(network::WebSocket* ws)
170{
171 log("onClose: websocket instance (%p) closed.", ws);
172 if (ws == _wsiSendText)
173 {
174 _wsiSendText = nullptr;
175 _sendTextStatus->setString("Send Text WS was closed");
176 }
177 // Delete websocket instance.
178 CC_SAFE_DELETE(ws);
179 log("WebSocketDelayTest ref: %u", _referenceCount);
180 release();
181}
182
183void WebSocketDelayTest::onError(network::WebSocket* ws, const network::WebSocket::ErrorCode& error)
184{
185 log("Error was fired, error code: %d", static_cast<int>(error));
186 char buf[100] = {0};
187 sprintf(buf, "An error was fired, code: %d", static_cast<int>(error));
188
189 if (ws == _wsiSendText)
190 {
191 _sendTextStatus->setString(buf);
192 }
193
195}
196
197// Menu Callbacks
199{
200 if (! _wsiSendText)
201 {
202 return;
203 }
204
205 if (_wsiSendText->getReadyState() == network::WebSocket::State::OPEN)
206 {
207
208 _sendTextTimes = 0;
210 doSendText();
211 }
212 else
213 {
214 std::string warningStr = "send text websocket instance wasn't ready...";
215 log("%s", warningStr.c_str());
216 _sendTextStatus->setString(warningStr.c_str());
217 }
218}
USING_NS_CC_EXT
#define SEND_TEXT_TIMES
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::Menu * _startTestMenu
virtual void onOpen(cocos2d::network::WebSocket *ws) override
virtual void onClose(cocos2d::network::WebSocket *ws) override
cocos2d::Label * _progressStatus
virtual void onExit() override
cocos2d::Label * _sendTextStatus
cocos2d::network::WebSocket * _wsiSendText
virtual void onMessage(cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::Data &data) override
void onMenuSendTextClicked(cocos2d::Ref *sender)
void startTestCallback(cocos2d::Ref *sender)
virtual void onError(cocos2d::network::WebSocket *ws, const cocos2d::network::WebSocket::ErrorCode &error) override