PuzzleSDK
SocketIOTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2015 Chris Hannon http://www.channon.us
3 Copyright (c) 2013-2016 Chukong Technologies Inc.
4 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
5
6 http://www.cocos2d-x.org
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25
26 ****************************************************************************/
27
28#include "SocketIOTest.h"
29#include "../ExtensionsTest.h"
30
33using namespace cocos2d::network;
34
35SocketIOTests::SocketIOTests()
36{
38}
39
41 : _sioClient(nullptr)
42 , _sioEndpoint(nullptr)
43 , _sioClientStatus(nullptr)
44{//set the clients to nullptr until we are ready to connect
45
46 Size winSize = Director::getInstance()->getWinSize();
47
48 const int MARGIN = 40;
49 const int SPACE = 35;
50
51 auto menuRequest = Menu::create();
52 menuRequest->setPosition(Vec2::ZERO);
53 addChild(menuRequest);
54
55 // Test to create basic client in the default namespace
56 auto labelSIOClient = Label::createWithTTF("Open SocketIO Client", "fonts/arial.ttf", 22);
57 auto itemSIOClient = MenuItemLabel::create(labelSIOClient, CC_CALLBACK_1(SocketIOTest::onMenuSIOClientClicked, this));
58 itemSIOClient->setPosition(Vec2(VisibleRect::left().x + labelSIOClient->getContentSize().width / 2 + 5, winSize.height - MARGIN - SPACE));
59 menuRequest->addChild(itemSIOClient);
60
61 // Test to create a client at the endpoint '/testpoint'
62 auto labelSIOEndpoint = Label::createWithTTF("Open SocketIO Endpoint", "fonts/arial.ttf", 22);
63 auto itemSIOEndpoint = MenuItemLabel::create(labelSIOEndpoint, CC_CALLBACK_1(SocketIOTest::onMenuSIOEndpointClicked, this));
64 itemSIOEndpoint->setPosition(Vec2(VisibleRect::right().x - labelSIOEndpoint->getContentSize().width / 2 - 5, winSize.height - MARGIN - SPACE));
65 menuRequest->addChild(itemSIOEndpoint);
66
67 // Test sending message to default namespace
68 auto labelTestMessage = Label::createWithTTF("Send Test Message", "fonts/arial.ttf", 22);
69 auto itemTestMessage = MenuItemLabel::create(labelTestMessage, CC_CALLBACK_1(SocketIOTest::onMenuTestMessageClicked, this));
70 itemTestMessage->setPosition(Vec2(VisibleRect::left().x + labelTestMessage->getContentSize().width / 2 + 5, winSize.height - MARGIN - 2 * SPACE));
71 menuRequest->addChild(itemTestMessage);
72
73 // Test sending message to the endpoint '/testpoint'
74 auto labelTestMessageEndpoint = Label::createWithTTF("Test Endpoint Message", "fonts/arial.ttf", 22);
75 auto itemTestMessageEndpoint = MenuItemLabel::create(labelTestMessageEndpoint, CC_CALLBACK_1(SocketIOTest::onMenuTestMessageEndpointClicked, this));
76 itemTestMessageEndpoint->setPosition(Vec2(VisibleRect::right().x - labelTestMessageEndpoint->getContentSize().width / 2 - 5, winSize.height - MARGIN - 2 * SPACE));
77 menuRequest->addChild(itemTestMessageEndpoint);
78
79 // Test sending event 'echotest' to default namespace
80 auto labelTestEvent = Label::createWithTTF("Send Test Event", "fonts/arial.ttf", 22);
81 auto itemTestEvent = MenuItemLabel::create(labelTestEvent, CC_CALLBACK_1(SocketIOTest::onMenuTestEventClicked, this));
82 itemTestEvent->setPosition(Vec2(VisibleRect::left().x + labelTestEvent->getContentSize().width / 2 + 5, winSize.height - MARGIN - 3 * SPACE));
83 menuRequest->addChild(itemTestEvent);
84
85 // Test sending event 'echotest' to the endpoint '/testpoint'
86 auto labelTestEventEndpoint = Label::createWithTTF("Test Endpoint Event", "fonts/arial.ttf", 22);
87 auto itemTestEventEndpoint = MenuItemLabel::create(labelTestEventEndpoint, CC_CALLBACK_1(SocketIOTest::onMenuTestEventEndpointClicked, this));
88 itemTestEventEndpoint->setPosition(Vec2(VisibleRect::right().x - labelTestEventEndpoint->getContentSize().width / 2 - 5, winSize.height - MARGIN - 3 * SPACE));
89 menuRequest->addChild(itemTestEventEndpoint);
90
91 // Test disconnecting basic client
92 auto labelTestClientDisconnect = Label::createWithTTF("Disconnect Socket", "fonts/arial.ttf", 22);
93 auto itemClientDisconnect = MenuItemLabel::create(labelTestClientDisconnect, CC_CALLBACK_1(SocketIOTest::onMenuTestClientDisconnectClicked, this));
94 itemClientDisconnect->setPosition(Vec2(VisibleRect::left().x + labelTestClientDisconnect->getContentSize().width / 2 + 5, winSize.height - MARGIN - 4 * SPACE));
95 menuRequest->addChild(itemClientDisconnect);
96
97 // Test disconnecting the endpoint '/testpoint'
98 auto labelTestEndpointDisconnect = Label::createWithTTF("Disconnect Endpoint", "fonts/arial.ttf", 22);
99 auto itemTestEndpointDisconnect = MenuItemLabel::create(labelTestEndpointDisconnect, CC_CALLBACK_1(SocketIOTest::onMenuTestEndpointDisconnectClicked, this));
100 itemTestEndpointDisconnect->setPosition(Vec2(VisibleRect::right().x - labelTestEndpointDisconnect->getContentSize().width / 2 - 5, winSize.height - MARGIN - 4 * SPACE));
101 menuRequest->addChild(itemTestEndpointDisconnect);
102
103 // Shared Status Label
104 _sioClientStatus = Label::createWithTTF("Not connected...", "fonts/arial.ttf", 14, Size(320, 100), TextHAlignment::LEFT);
105 _sioClientStatus->setAnchorPoint(Vec2(0, 0));
107 this->addChild(_sioClientStatus);
108}
109
110
112{
113}
114
115//test event callback handlers, these will be registered with socket.io
116void SocketIOTest::testevent(SIOClient *client, const std::string& data) {
117
118 CCLOGINFO("SocketIOTest::testevent called with data: %s", data.c_str());
119
120 std::stringstream s;
121 s << client->getTag() << " received event testevent with data: " << data.c_str();
122
123 _sioClientStatus->setString(s.str().c_str());
124
125}
126
127void SocketIOTest::echotest(SIOClient *client, const std::string& data) {
128
129 CCLOGINFO("SocketIOTest::echotest called with data: %s", data.c_str());
130
131 std::stringstream s;
132 s << client->getTag() << " received event echotest with data: " << data.c_str();
133
134 _sioClientStatus->setString(s.str().c_str());
135
136}
137
138// onMessage is no longer a required override from the delegate class
139// 'message' events and handlers are now registered in the same way that other events are
140void SocketIOTest::message(network::SIOClient* client, const std::string& data)
141{
142 CCLOGINFO("SocketIOTest::message received: %s", data.c_str());
143
144 std::stringstream s;
145 s << client->getTag() << " received message with content: " << data.c_str();
146 _sioClientStatus->setString(s.str().c_str());
147
148}
149
150void SocketIOTest::json(network::SIOClient* client, const std::string& data)
151{
152 CCLOGINFO("SocketIOTest::json received: %s", data.c_str());
153
154 std::stringstream s;
155 s << client->getTag() << " received json message with content: " << data.c_str();
156 _sioClientStatus->setString(s.str().c_str());
157
158}
159
160void SocketIOTest::connect(network::SIOClient* client, const std::string& data)
161{
162 CCLOGINFO("SocketIOTest::connect called");
163
164 std::stringstream s;
165 s << client->getTag() << " connected!";
166 _sioClientStatus->setString(s.str().c_str());
167
168}
169
170void SocketIOTest::disconnect(network::SIOClient* client, const std::string& data)
171{
172 CCLOGINFO("SocketIOTest::disconnect called");
173
174 std::stringstream s;
175 s << client->getTag() << " disconnected by server!";
176 _sioClientStatus->setString(s.str().c_str());
177
178 this->closedSocketAction(client);
179
180}
181
182void SocketIOTest::closedSocketAction(network::SIOClient* client)
183{
184 //set the local pointer to nullptr or connect to another client
185 //the client object will be released on its own after this method completes
186 if (client == _sioClient) {
187
188 _sioClient = nullptr;
189 }
190 else if (client == _sioEndpoint) {
191
192 _sioEndpoint = nullptr;
193 }
194}
195
196void SocketIOTest::onMenuSIOClientClicked(cocos2d::Ref *sender)
197{
198 //create a client by using this static method, url does not need to contain the protocol
199 _sioClient = SocketIO::connect("ws://tools.itharbors.com:4000", *this);
200 //you may set a tag for the client for reference in callbacks
201 _sioClient->setTag("Test Client");
202
203 //register event callbacks using the CC_CALLBACK_2() macro and passing the instance of the target class
204 _sioClient->on("testevent", CC_CALLBACK_2(SocketIOTest::testevent, this));
205 _sioClient->on("echotest", CC_CALLBACK_2(SocketIOTest::echotest, this));
206 _sioClient->on("message", CC_CALLBACK_2(SocketIOTest::message, this));
207 _sioClient->on("json", CC_CALLBACK_2(SocketIOTest::json, this));
208 _sioClient->on("connect", CC_CALLBACK_2(SocketIOTest::connect, this));
209 _sioClient->on("disconnect", CC_CALLBACK_2(SocketIOTest::disconnect, this));
210
211}
212
214{
215 //repeat the same connection steps for the namespace "testpoint"
216 _sioEndpoint = SocketIO::connect("ws://tools.itharbors.com:4000/testpoint", *this);
217 //a tag to differentiate in shared callbacks
218 _sioEndpoint->setTag("Test Endpoint");
219
220 //demonstrating how callbacks can be shared within a delegate
221 _sioEndpoint->on("testevent", CC_CALLBACK_2(SocketIOTest::testevent, this));
222 _sioEndpoint->on("echotest", CC_CALLBACK_2(SocketIOTest::echotest, this));
223 _sioEndpoint->on("message", CC_CALLBACK_2(SocketIOTest::message, this));
224 _sioEndpoint->on("json", CC_CALLBACK_2(SocketIOTest::json, this));
225 _sioEndpoint->on("connect", CC_CALLBACK_2(SocketIOTest::connect, this));
226 _sioEndpoint->on("disconnect", CC_CALLBACK_2(SocketIOTest::disconnect, this));
227
228}
229
231{
232 //check that the socket is != nullptr before sending or emitting events
233 //the client should be nullptr either before initialization and connection or after disconnect
234 if (_sioClient != nullptr) _sioClient->send("Hello Socket.IO!");
235
236}
237
239{
240
241 if (_sioEndpoint != nullptr) _sioEndpoint->send("Hello Socket.IO!");
242
243}
244
245void SocketIOTest::onMenuTestEventClicked(cocos2d::Ref *sender)
246{
247 //check that the socket is != nullptr before sending or emitting events
248 //the client should be nullptr either before initialization and connection or after disconnect
249 if (_sioClient != nullptr) _sioClient->emit("echotest", "{\"name\":\"myname\",\"type\":\"mytype\"}");
250
251}
252
254{
255
256 if (_sioEndpoint != nullptr) _sioEndpoint->emit("echotest", "{\"name\":\"myname\",\"type\":\"mytype\"}");
257
258}
259
261{
262 // Disconnecting from the default namespace "" or "/" will also disconnect all other endpoints
263 std::stringstream s;
264
265 if (_sioClient != nullptr) {
266 s << _sioClient->getTag() << " manually closed!";
267 _sioClient->disconnect();
268 _sioClient = nullptr;
269 }
270 else {
271 s << "Socket.io Test Client not initialized!";
272 }
273
274 _sioClientStatus->setString(s.str().c_str());
275
276}
277
279{
280 std::stringstream s;
281
282 if (_sioEndpoint != nullptr) {
283 s << _sioEndpoint->getTag() << " manually closed!";
284 _sioEndpoint->disconnect();
285 _sioEndpoint = nullptr;
286 }
287 else {
288 s << "Socket.io Test Endpoint not initialized!";
289 }
290
291 _sioClientStatus->setString(s.str().c_str());
292
293}
294
295// SIODelegate methods to catch network/socket level events outside of the socket.io events
296
297void SocketIOTest::onClose(network::SIOClient* client)
298{
299 CCLOGINFO("SocketIOTest::onClose called");
300
301 std::stringstream s;
302 s << client->getTag() << " closed!";
303 _sioClientStatus->setString(s.str().c_str());
304
305 this->closedSocketAction(client);
306
307}
308
309void SocketIOTest::onError(network::SIOClient* client, const std::string& data)
310{
311 CCLOGERROR("SocketIOTest::onError received: %s", data.c_str());
312
313 std::stringstream s;
314 s << client->getTag() << " received error with content: " << data.c_str();
315 _sioClientStatus->setString(s.str().c_str());
316}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC_EXT
USING_NS_CC
void echotest(cocos2d::network::SIOClient *client, const std::string &data)
Socket.io event handler for custom event "echoevent"
void onMenuSIOEndpointClicked(cocos2d::Ref *sender)
cocos2d::network::SIOClient * _sioClient
Definition: SocketIOTest.h:104
void onMenuTestEndpointDisconnectClicked(cocos2d::Ref *sender)
cocos2d::Label * _sioClientStatus
Definition: SocketIOTest.h:106
void connect(cocos2d::network::SIOClient *client, const std::string &data)
Socket.io event handler for event "connect"
void testevent(cocos2d::network::SIOClient *client, const std::string &data)
Socket.io event handler for custom event "testevent"
void onMenuTestEventClicked(cocos2d::Ref *sender)
void message(cocos2d::network::SIOClient *client, const std::string &data)
Socket.io event handler for event "message"
void closedSocketAction(cocos2d::network::SIOClient *client)
Common function to call on both socket.io disconnect and websocket close
void onMenuTestEventEndpointClicked(cocos2d::Ref *sender)
void onMenuTestClientDisconnectClicked(cocos2d::Ref *sender)
virtual void onError(cocos2d::network::SIOClient *client, const std::string &data) override
Used for network level socket error (not for disconnect from the socket.io server)
void onMenuTestMessageEndpointClicked(cocos2d::Ref *sender)
void json(cocos2d::network::SIOClient *client, const std::string &data)
Socket.io event handler for event "json" This is only used in v 0.9.x, in 1.x this is handled as a "m...
void onMenuSIOClientClicked(cocos2d::Ref *sender)
cocos2d::network::SIOClient * _sioEndpoint
Definition: SocketIOTest.h:104
virtual void onClose(cocos2d::network::SIOClient *client) override
Used for network level socket close (not for disconnect from the socket.io server)
virtual ~SocketIOTest()
void onMenuTestMessageClicked(cocos2d::Ref *sender)
void disconnect(cocos2d::network::SIOClient *client, const std::string &data)
Socket.io event handler for event "disconnect"
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45
static cocos2d::Vec2 rightBottom()
Definition: VisibleRect.cpp:93
static cocos2d::Vec2 right()
Definition: VisibleRect.cpp:51