2013-05-31 23:13:03 +08:00
# include "WebSocketTest.h"
# include "../ExtensionsTest.h"
2016-03-10 12:04:10 +08:00
# include "testResource.h"
2013-05-31 23:13:03 +08:00
USING_NS_CC ;
USING_NS_CC_EXT ;
2015-04-03 14:31:03 +08:00
WebSocketTests : : WebSocketTests ( )
{
ADD_TEST_CASE ( WebSocketTest ) ;
2016-03-10 12:04:10 +08:00
ADD_TEST_CASE ( WebSocketCloseTest ) ;
2015-04-03 14:31:03 +08:00
}
WebSocketTest : : WebSocketTest ( )
2014-04-09 22:53:59 +08:00
: _wsiSendText ( nullptr )
, _wsiSendBinary ( nullptr )
, _wsiError ( nullptr )
, _sendTextStatus ( nullptr )
, _sendBinaryStatus ( nullptr )
, _errorStatus ( nullptr )
2013-05-31 23:13:03 +08:00
, _sendTextTimes ( 0 )
, _sendBinaryTimes ( 0 )
{
2013-08-16 16:05:27 +08:00
auto winSize = Director : : getInstance ( ) - > getWinSize ( ) ;
2013-05-31 23:13:03 +08:00
const int MARGIN = 40 ;
const int SPACE = 35 ;
2013-08-16 16:05:27 +08:00
auto menuRequest = Menu : : create ( ) ;
2014-05-15 01:07:09 +08:00
menuRequest - > setPosition ( Vec2 : : ZERO ) ;
2013-05-31 23:13:03 +08:00
addChild ( menuRequest ) ;
// Send Text
2015-04-03 14:31:03 +08:00
auto labelSendText = Label : : createWithTTF ( " Send Text " , " fonts/arial.ttf " , 20 ) ;
auto itemSendText = MenuItemLabel : : create ( labelSendText , CC_CALLBACK_1 ( WebSocketTest : : onMenuSendTextClicked , this ) ) ;
2014-05-15 01:07:09 +08:00
itemSendText - > setPosition ( Vec2 ( winSize . width / 2 , winSize . height - MARGIN - SPACE ) ) ;
2013-05-31 23:13:03 +08:00
menuRequest - > addChild ( itemSendText ) ;
2015-12-31 10:42:06 +08:00
labelSendText = Label : : createWithTTF ( " Send Multiple Text " , " fonts/arial.ttf " , 20 ) ;
itemSendText = MenuItemLabel : : create ( labelSendText , CC_CALLBACK_1 ( WebSocketTest : : onMenuSendMultipleTextClicked , this ) ) ;
itemSendText - > setPosition ( Vec2 ( winSize . width / 2 , winSize . height - MARGIN - 2 * SPACE ) ) ;
menuRequest - > addChild ( itemSendText ) ;
2013-05-31 23:13:03 +08:00
// Send Binary
2015-04-03 14:31:03 +08:00
auto labelSendBinary = Label : : createWithTTF ( " Send Binary " , " fonts/arial.ttf " , 20 ) ;
auto itemSendBinary = MenuItemLabel : : create ( labelSendBinary , CC_CALLBACK_1 ( WebSocketTest : : onMenuSendBinaryClicked , this ) ) ;
2015-12-31 10:42:06 +08:00
itemSendBinary - > setPosition ( Vec2 ( winSize . width / 2 , winSize . height - MARGIN - 3 * SPACE ) ) ;
2013-05-31 23:13:03 +08:00
menuRequest - > addChild ( itemSendBinary ) ;
// Send Text Status Label
2015-04-03 14:31:03 +08:00
_sendTextStatus = Label : : createWithTTF ( " Send Text WS is waiting... " , " fonts/arial.ttf " , 16 , Size ( 160 , 100 ) , TextHAlignment : : CENTER , TextVAlignment : : TOP ) ;
2014-05-15 01:07:09 +08:00
_sendTextStatus - > setAnchorPoint ( Vec2 ( 0 , 0 ) ) ;
_sendTextStatus - > setPosition ( Vec2 ( VisibleRect : : left ( ) . x , VisibleRect : : rightBottom ( ) . y + 25 ) ) ;
2013-05-31 23:13:03 +08:00
this - > addChild ( _sendTextStatus ) ;
// Send Binary Status Label
2015-04-03 14:31:03 +08:00
_sendBinaryStatus = Label : : createWithTTF ( " Send Binary WS is waiting... " , " fonts/arial.ttf " , 16 , Size ( 160 , 100 ) , TextHAlignment : : CENTER , TextVAlignment : : TOP ) ;
2014-05-15 01:07:09 +08:00
_sendBinaryStatus - > setAnchorPoint ( Vec2 ( 0 , 0 ) ) ;
_sendBinaryStatus - > setPosition ( Vec2 ( VisibleRect : : left ( ) . x + 160 , VisibleRect : : rightBottom ( ) . y + 25 ) ) ;
2013-05-31 23:13:03 +08:00
this - > addChild ( _sendBinaryStatus ) ;
// Error Label
2015-04-03 14:31:03 +08:00
_errorStatus = Label : : createWithTTF ( " Error WS is waiting... " , " fonts/arial.ttf " , 16 , Size ( 160 , 100 ) , TextHAlignment : : CENTER , TextVAlignment : : TOP ) ;
2014-05-15 01:07:09 +08:00
_errorStatus - > setAnchorPoint ( Vec2 ( 0 , 0 ) ) ;
_errorStatus - > setPosition ( Vec2 ( VisibleRect : : left ( ) . x + 320 , VisibleRect : : rightBottom ( ) . y + 25 ) ) ;
2013-05-31 23:13:03 +08:00
this - > addChild ( _errorStatus ) ;
2015-04-03 14:31:03 +08:00
auto startTestLabel = Label : : createWithTTF ( " Start Test WebSocket " , " fonts/arial.ttf " , 16 ) ;
auto startTestItem = MenuItemLabel : : create ( startTestLabel , CC_CALLBACK_1 ( WebSocketTest : : startTestCallback , this ) ) ;
startTestItem - > setPosition ( Vec2 ( VisibleRect : : center ( ) . x , VisibleRect : : bottom ( ) . y + 150 ) ) ;
_startTestMenu = Menu : : create ( startTestItem , nullptr ) ;
_startTestMenu - > setPosition ( Vec2 : : ZERO ) ;
this - > addChild ( _startTestMenu , 1 ) ;
}
WebSocketTest : : ~ WebSocketTest ( )
{
if ( _wsiSendText )
_wsiSendText - > close ( ) ;
if ( _wsiSendBinary )
_wsiSendBinary - > close ( ) ;
2013-05-31 23:13:03 +08:00
2015-04-03 14:31:03 +08:00
if ( _wsiError )
_wsiError - > close ( ) ;
}
void WebSocketTest : : startTestCallback ( Ref * sender )
{
removeChild ( _startTestMenu ) ;
_startTestMenu = nullptr ;
2014-01-02 16:25:35 +08:00
_wsiSendText = new network : : WebSocket ( ) ;
_wsiSendBinary = new network : : WebSocket ( ) ;
_wsiError = new network : : WebSocket ( ) ;
2015-04-03 14:31:03 +08:00
2013-05-31 23:13:03 +08:00
if ( ! _wsiSendText - > init ( * this , " ws://echo.websocket.org " ) )
{
CC_SAFE_DELETE ( _wsiSendText ) ;
}
2015-04-03 14:31:03 +08:00
2013-05-31 23:13:03 +08:00
if ( ! _wsiSendBinary - > init ( * this , " ws://echo.websocket.org " ) )
{
CC_SAFE_DELETE ( _wsiSendBinary ) ;
}
2015-04-03 14:31:03 +08:00
2013-05-31 23:13:03 +08:00
if ( ! _wsiError - > init ( * this , " ws://invalid.url.com " ) )
{
CC_SAFE_DELETE ( _wsiError ) ;
}
}
// Delegate methods
2015-04-03 14:31:03 +08:00
void WebSocketTest : : onOpen ( network : : WebSocket * ws )
2013-05-31 23:13:03 +08:00
{
2013-07-24 06:20:22 +08:00
log ( " Websocket (%p) opened " , ws ) ;
2013-05-31 23:13:03 +08:00
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 )
{
2013-07-20 13:01:27 +08:00
CCASSERT ( 0 , " error test will never go here. " ) ;
2013-05-31 23:13:03 +08:00
}
}
2015-04-03 14:31:03 +08:00
void WebSocketTest : : onMessage ( network : : WebSocket * ws , const network : : WebSocket : : Data & data )
2013-05-31 23:13:03 +08:00
{
if ( ! data . isBinary )
{
_sendTextTimes + + ;
char times [ 100 ] = { 0 } ;
sprintf ( times , " %d " , _sendTextTimes ) ;
std : : string textStr = std : : string ( " response text msg: " ) + data . bytes + " , " + times ;
2013-07-24 06:20:22 +08:00
log ( " %s " , textStr . c_str ( ) ) ;
2013-05-31 23:13:03 +08:00
_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 ;
2013-07-24 06:20:22 +08:00
log ( " %s " , binaryStr . c_str ( ) ) ;
2013-05-31 23:13:03 +08:00
_sendBinaryStatus - > setString ( binaryStr . c_str ( ) ) ;
}
}
2015-04-03 14:31:03 +08:00
void WebSocketTest : : onClose ( network : : WebSocket * ws )
2013-05-31 23:13:03 +08:00
{
2013-07-24 06:20:22 +08:00
log ( " websocket instance (%p) closed. " , ws ) ;
2013-05-31 23:13:03 +08:00
if ( ws = = _wsiSendText )
{
2014-07-10 00:45:27 +08:00
_wsiSendText = nullptr ;
2013-05-31 23:13:03 +08:00
}
else if ( ws = = _wsiSendBinary )
{
2014-07-10 00:45:27 +08:00
_wsiSendBinary = nullptr ;
2013-05-31 23:13:03 +08:00
}
else if ( ws = = _wsiError )
{
2014-07-10 00:45:27 +08:00
_wsiError = nullptr ;
2013-05-31 23:13:03 +08:00
}
// Delete websocket instance.
CC_SAFE_DELETE ( ws ) ;
}
2015-04-03 14:31:03 +08:00
void WebSocketTest : : onError ( network : : WebSocket * ws , const network : : WebSocket : : ErrorCode & error )
2013-05-31 23:13:03 +08:00
{
2016-04-18 17:52:58 +08:00
log ( " Error was fired, error code: %d " , static_cast < int > ( error ) ) ;
2013-05-31 23:13:03 +08:00
if ( ws = = _wsiError )
{
char buf [ 100 ] = { 0 } ;
2016-04-18 17:52:58 +08:00
sprintf ( buf , " an error was fired, code: %d " , static_cast < int > ( error ) ) ;
2013-05-31 23:13:03 +08:00
_errorStatus - > setString ( buf ) ;
}
}
// Menu Callbacks
2015-04-03 14:31:03 +08:00
void WebSocketTest : : onMenuSendTextClicked ( cocos2d : : Ref * sender )
2013-05-31 23:13:03 +08:00
{
2014-03-06 16:49:51 +08:00
if ( ! _wsiSendText )
{
return ;
}
2014-01-02 16:25:35 +08:00
if ( _wsiSendText - > getReadyState ( ) = = network : : WebSocket : : State : : OPEN )
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... " ;
2013-07-24 06:20:22 +08:00
log ( " %s " , warningStr . c_str ( ) ) ;
2013-05-31 23:13:03 +08:00
_sendTextStatus - > setString ( warningStr . c_str ( ) ) ;
}
}
2015-12-31 10:42:06 +08:00
void WebSocketTest : : onMenuSendMultipleTextClicked ( cocos2d : : Ref * sender )
{
if ( ! _wsiSendText )
{
return ;
}
if ( _wsiSendText - > getReadyState ( ) = = network : : WebSocket : : State : : OPEN )
{
_sendTextStatus - > setString ( " Send Multiple Text WS is waiting... " ) ;
for ( int index = 0 ; index < 15 ; + + index ) {
_wsiSendText - > send ( StringUtils : : format ( " Hello WebSocket, text message index:%d " , index ) ) ;
}
}
else
{
std : : string warningStr = " send text websocket instance wasn't ready... " ;
log ( " %s " , warningStr . c_str ( ) ) ;
_sendTextStatus - > setString ( warningStr . c_str ( ) ) ;
}
}
2015-04-03 14:31:03 +08:00
void WebSocketTest : : onMenuSendBinaryClicked ( cocos2d : : Ref * sender )
2013-05-31 23:13:03 +08:00
{
2014-03-06 16:49:51 +08:00
if ( ! _wsiSendBinary ) {
return ;
}
2014-01-02 16:25:35 +08:00
if ( _wsiSendBinary - > getReadyState ( ) = = network : : WebSocket : : State : : OPEN )
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... " ;
2013-07-24 06:20:22 +08:00
log ( " %s " , warningStr . c_str ( ) ) ;
2013-05-31 23:13:03 +08:00
_sendBinaryStatus - > setString ( warningStr . c_str ( ) ) ;
}
}
2016-03-10 12:04:10 +08:00
WebSocketCloseTest : : WebSocketCloseTest ( )
2016-04-25 16:22:46 +08:00
: _wsiTest ( nullptr )
2016-03-10 12:04:10 +08:00
{
auto winSize = Director : : getInstance ( ) - > getWinSize ( ) ;
_wsiTest = new network : : WebSocket ( ) ;
if ( ! _wsiTest - > init ( * this , " ws://echo.websocket.org " ) )
{
2016-05-10 17:55:36 +08:00
delete _wsiTest ;
_wsiTest = nullptr ;
2016-03-10 12:04:10 +08:00
}
auto closeItem = MenuItemImage : : create ( s_pathClose , s_pathClose , [ ] ( Ref * sender ) {
Director : : getInstance ( ) - > end ( ) ;
# if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit ( 0 ) ;
# endif
} ) ;
closeItem - > setPosition ( VisibleRect : : right ( ) . x / 2 , VisibleRect : : top ( ) . y * 2 / 3 ) ;
auto menu = Menu : : create ( closeItem , nullptr ) ;
menu - > setPosition ( Vec2 : : ZERO ) ;
addChild ( menu , 1 ) ;
auto notifyLabel = Label : : createWithTTF ( " See log window, when enter there's should have \n 'Websocket opened' log, \n when close there's should have'websocket closed' log " , " fonts/arial.ttf " , 20 ) ;
notifyLabel - > setPosition ( VisibleRect : : right ( ) . x / 2 , VisibleRect : : top ( ) . y / 3 ) ;
notifyLabel - > setAlignment ( TextHAlignment : : CENTER ) ;
addChild ( notifyLabel , 1 ) ;
}
2016-05-10 17:55:36 +08:00
WebSocketCloseTest : : ~ WebSocketCloseTest ( )
{
2016-04-25 16:22:46 +08:00
if ( _wsiTest ! = nullptr )
{
2016-05-10 17:55:36 +08:00
_wsiTest - > close ( ) ;
2016-04-25 16:22:46 +08:00
}
2016-05-10 17:55:36 +08:00
}
2016-03-10 12:04:10 +08:00
// Delegate methods
void WebSocketCloseTest : : onOpen ( network : : WebSocket * ws )
{
log ( " Websocket (%p) opened " , ws ) ;
}
void WebSocketCloseTest : : onMessage ( network : : WebSocket * ws , const network : : WebSocket : : Data & data )
{
log ( " Websocket get message from %p " , ws ) ;
}
void WebSocketCloseTest : : onClose ( network : : WebSocket * ws )
{
log ( " websocket (%p) closed. " , ws ) ;
2016-04-25 16:22:46 +08:00
if ( ws = = _wsiTest ) {
_wsiTest = nullptr ;
}
2016-03-10 12:04:10 +08:00
CC_SAFE_DELETE ( ws ) ;
}
void WebSocketCloseTest : : onError ( network : : WebSocket * ws , const network : : WebSocket : : ErrorCode & error )
{
2016-04-18 17:52:58 +08:00
log ( " Error was fired, error code: %d " , static_cast < int > ( error ) ) ;
2016-03-10 12:04:10 +08:00
}