2013-05-31 23:13:03 +08:00
|
|
|
//
|
|
|
|
// WebSocketTest.cpp
|
|
|
|
// TestCpp
|
|
|
|
//
|
|
|
|
// Created by James Chen on 5/31/13.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "WebSocketTest.h"
|
|
|
|
#include "../ExtensionsTest.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
|
|
|
WebSocketTestLayer::WebSocketTestLayer()
|
|
|
|
: _wsiSendText(NULL)
|
|
|
|
, _wsiSendBinary(NULL)
|
|
|
|
, _wsiError(NULL)
|
|
|
|
, _sendTextStatus(NULL)
|
|
|
|
, _sendBinaryStatus(NULL)
|
|
|
|
, _errorStatus(NULL)
|
|
|
|
, _sendTextTimes(0)
|
|
|
|
, _sendBinaryTimes(0)
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Size winSize = Director::getInstance()->getWinSize();
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
const int MARGIN = 40;
|
|
|
|
const int SPACE = 35;
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *label = LabelTTF::create("WebSocket Test", "Arial", 28);
|
2013-07-12 14:11:55 +08:00
|
|
|
label->setPosition(Point(winSize.width / 2, winSize.height - MARGIN));
|
2013-05-31 23:13:03 +08:00
|
|
|
addChild(label, 0);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menuRequest = Menu::create();
|
2013-07-12 14:47:36 +08:00
|
|
|
menuRequest->setPosition(Point::ZERO);
|
2013-05-31 23:13:03 +08:00
|
|
|
addChild(menuRequest);
|
|
|
|
|
|
|
|
// Send Text
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelSendText = LabelTTF::create("Send Text", "Arial", 22);
|
2013-06-21 14:15:20 +08:00
|
|
|
MenuItemLabel *itemSendText = MenuItemLabel::create(labelSendText, CC_CALLBACK_1(WebSocketTestLayer::onMenuSendTextClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemSendText->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - SPACE));
|
2013-05-31 23:13:03 +08:00
|
|
|
menuRequest->addChild(itemSendText);
|
|
|
|
|
|
|
|
// Send Binary
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelSendBinary = LabelTTF::create("Send Binary", "Arial", 22);
|
2013-06-21 14:15:20 +08:00
|
|
|
MenuItemLabel *itemSendBinary = MenuItemLabel::create(labelSendBinary, CC_CALLBACK_1(WebSocketTestLayer::onMenuSendBinaryClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemSendBinary->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
|
2013-05-31 23:13:03 +08:00
|
|
|
menuRequest->addChild(itemSendBinary);
|
|
|
|
|
|
|
|
|
|
|
|
// Send Text Status Label
|
2013-07-12 14:30:26 +08:00
|
|
|
_sendTextStatus = LabelTTF::create("Send Text WS is waiting...", "Arial", 14, Size(160, 100), kTextAlignmentCenter, kVerticalTextAlignmentTop);
|
2013-07-12 14:11:55 +08:00
|
|
|
_sendTextStatus->setAnchorPoint(Point(0, 0));
|
|
|
|
_sendTextStatus->setPosition(Point(VisibleRect::left().x, VisibleRect::rightBottom().y + 25));
|
2013-05-31 23:13:03 +08:00
|
|
|
this->addChild(_sendTextStatus);
|
|
|
|
|
|
|
|
// Send Binary Status Label
|
2013-07-12 14:30:26 +08:00
|
|
|
_sendBinaryStatus = LabelTTF::create("Send Binary WS is waiting...", "Arial", 14, Size(160, 100), kTextAlignmentCenter, kVerticalTextAlignmentTop);
|
2013-07-12 14:11:55 +08:00
|
|
|
_sendBinaryStatus->setAnchorPoint(Point(0, 0));
|
|
|
|
_sendBinaryStatus->setPosition(Point(VisibleRect::left().x + 160, VisibleRect::rightBottom().y + 25));
|
2013-05-31 23:13:03 +08:00
|
|
|
this->addChild(_sendBinaryStatus);
|
|
|
|
|
|
|
|
// Error Label
|
2013-07-12 14:30:26 +08:00
|
|
|
_errorStatus = LabelTTF::create("Error WS is waiting...", "Arial", 14, Size(160, 100), kTextAlignmentCenter, kVerticalTextAlignmentTop);
|
2013-07-12 14:11:55 +08:00
|
|
|
_errorStatus->setAnchorPoint(Point(0, 0));
|
|
|
|
_errorStatus->setPosition(Point(VisibleRect::left().x + 320, VisibleRect::rightBottom().y + 25));
|
2013-05-31 23:13:03 +08:00
|
|
|
this->addChild(_errorStatus);
|
|
|
|
|
|
|
|
// Back Menu
|
2013-06-21 14:15:20 +08:00
|
|
|
MenuItemFont *itemBack = MenuItemFont::create("Back", CC_CALLBACK_1(WebSocketTestLayer::toExtensionsMainLayer, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemBack->setPosition(Point(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25));
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menuBack = Menu::create(itemBack, NULL);
|
2013-07-12 14:47:36 +08:00
|
|
|
menuBack->setPosition(Point::ZERO);
|
2013-05-31 23:13:03 +08:00
|
|
|
addChild(menuBack);
|
|
|
|
|
|
|
|
_wsiSendText = new WebSocket();
|
|
|
|
_wsiSendBinary = new WebSocket();
|
|
|
|
_wsiError = new WebSocket();
|
|
|
|
|
|
|
|
if (!_wsiSendText->init(*this, "ws://echo.websocket.org"))
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_wsiSendText);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_wsiSendBinary->init(*this, "ws://echo.websocket.org"))
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_wsiSendBinary);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_wsiError->init(*this, "ws://invalid.url.com"))
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_wsiError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WebSocketTestLayer::~WebSocketTestLayer()
|
|
|
|
{
|
|
|
|
if (_wsiSendText)
|
|
|
|
_wsiSendText->close();
|
|
|
|
|
|
|
|
if (_wsiSendBinary)
|
|
|
|
_wsiSendBinary->close();
|
|
|
|
|
|
|
|
if (_wsiError)
|
|
|
|
_wsiError->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delegate methods
|
|
|
|
void WebSocketTestLayer::onOpen(cocos2d::extension::WebSocket* ws)
|
|
|
|
{
|
|
|
|
CCLog("Websocket (%p) opened", ws);
|
|
|
|
if (ws == _wsiSendText)
|
|
|
|
{
|
|
|
|
_sendTextStatus->setString("Send Text WS was opened.");
|
|
|
|
}
|
|
|
|
else if (ws == _wsiSendBinary)
|
|
|
|
{
|
|
|
|
_sendBinaryStatus->setString("Send Binary WS was opened.");
|
|
|
|
}
|
|
|
|
else if (ws == _wsiError)
|
|
|
|
{
|
|
|
|
CCAssert(0, "error test will never go here.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketTestLayer::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data)
|
|
|
|
{
|
|
|
|
if (!data.isBinary)
|
|
|
|
{
|
|
|
|
_sendTextTimes++;
|
|
|
|
char times[100] = {0};
|
|
|
|
sprintf(times, "%d", _sendTextTimes);
|
|
|
|
std::string textStr = std::string("response text msg: ")+data.bytes+", "+times;
|
|
|
|
CCLog("%s", textStr.c_str());
|
|
|
|
|
|
|
|
_sendTextStatus->setString(textStr.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_sendBinaryTimes++;
|
|
|
|
char times[100] = {0};
|
|
|
|
sprintf(times, "%d", _sendBinaryTimes);
|
|
|
|
|
|
|
|
std::string binaryStr = "response bin msg: ";
|
|
|
|
|
|
|
|
for (int i = 0; i < data.len; ++i) {
|
|
|
|
if (data.bytes[i] != '\0')
|
|
|
|
{
|
|
|
|
binaryStr += data.bytes[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
binaryStr += "\'\\0\'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
binaryStr += std::string(", ")+times;
|
|
|
|
CCLog("%s", binaryStr.c_str());
|
|
|
|
_sendBinaryStatus->setString(binaryStr.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketTestLayer::onClose(cocos2d::extension::WebSocket* ws)
|
|
|
|
{
|
|
|
|
CCLog("websocket instance (%p) closed.", ws);
|
|
|
|
if (ws == _wsiSendText)
|
|
|
|
{
|
|
|
|
_wsiSendText = NULL;
|
|
|
|
}
|
|
|
|
else if (ws == _wsiSendBinary)
|
|
|
|
{
|
|
|
|
_wsiSendBinary = NULL;
|
|
|
|
}
|
|
|
|
else if (ws == _wsiError)
|
|
|
|
{
|
|
|
|
_wsiError = NULL;
|
|
|
|
}
|
|
|
|
// Delete websocket instance.
|
|
|
|
CC_SAFE_DELETE(ws);
|
|
|
|
}
|
|
|
|
|
2013-06-03 09:55:43 +08:00
|
|
|
void WebSocketTestLayer::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
CCLog("Error was fired, error code: %d", error);
|
|
|
|
if (ws == _wsiError)
|
|
|
|
{
|
|
|
|
char buf[100] = {0};
|
|
|
|
sprintf(buf, "an error was fired, code: %d", error);
|
|
|
|
_errorStatus->setString(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::Object *sender)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
ExtensionsTestScene *pScene = new ExtensionsTestScene();
|
|
|
|
pScene->runThisTest();
|
|
|
|
pScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Menu Callbacks
|
2013-06-20 14:17:10 +08:00
|
|
|
void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::Object *sender)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2013-06-03 09:55:43 +08:00
|
|
|
if (_wsiSendText->getReadyState() == WebSocket::kStateOpen)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
_sendTextStatus->setString("Send Text WS is waiting...");
|
|
|
|
_wsiSendText->send("Hello WebSocket, I'm a text message.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string warningStr = "send text websocket instance wasn't ready...";
|
|
|
|
CCLog("%s", warningStr.c_str());
|
|
|
|
_sendTextStatus->setString(warningStr.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::Object *sender)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2013-06-03 09:55:43 +08:00
|
|
|
if (_wsiSendBinary->getReadyState() == WebSocket::kStateOpen)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
_sendBinaryStatus->setString("Send Binary WS is waiting...");
|
|
|
|
char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";
|
|
|
|
_wsiSendBinary->send((unsigned char*)buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string warningStr = "send binary websocket instance wasn't ready...";
|
|
|
|
CCLog("%s", warningStr.c_str());
|
|
|
|
_sendBinaryStatus->setString(warningStr.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void runWebSocketTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene *pScene = Scene::create();
|
2013-05-31 23:13:03 +08:00
|
|
|
WebSocketTestLayer *pLayer = new WebSocketTestLayer();
|
|
|
|
pScene->addChild(pLayer);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(pScene);
|
2013-05-31 23:13:03 +08:00
|
|
|
pLayer->release();
|
|
|
|
}
|