2013-06-06 22:29:02 +08:00
local function WebSocketTestLayer ( )
2013-08-22 10:16:57 +08:00
local layer = cc.Layer : create ( )
local winSize = cc.Director : getInstance ( ) : getWinSize ( )
2013-06-06 22:29:02 +08:00
local MARGIN = 40
local SPACE = 35
local wsSendText = nil
local wsSendBinary = nil
local wsError = nil
local sendTextStatus = nil
local sendBinaryStatus = nil
local errorStatus = nil
local receiveTextTimes = 0
local receiveBinaryTimes = 0
2013-08-22 10:16:57 +08:00
local label = cc.LabelTTF : create ( " WebSocket Test " , " Arial " , 28 )
label : setPosition ( cc.p ( winSize.width / 2 , winSize.height - MARGIN ) )
2013-06-06 22:29:02 +08:00
layer : addChild ( label , 0 )
2013-08-22 10:16:57 +08:00
local menuRequest = cc.Menu : create ( )
menuRequest : setPosition ( cc.p ( 0 , 0 ) )
2013-06-06 22:29:02 +08:00
layer : addChild ( menuRequest )
--Send Text
local function onMenuSendTextClicked ( )
if nil ~= wsSendText then
2013-09-04 14:02:22 +08:00
if cc.WEBSOCKET_STATE_OPEN == wsSendText : getReadyState ( ) then
2013-06-06 22:29:02 +08:00
sendTextStatus : setString ( " Send Text WS is waiting... " )
2013-07-26 22:50:45 +08:00
wsSendText : sendString ( " Hello WebSocket中文, I'm a text message. " )
2013-06-06 22:29:02 +08:00
else
local warningStr = " send text websocket instance wasn't ready... "
print ( warningStr )
sendTextStatus : setString ( warningStr )
end
end
end
2013-08-22 10:16:57 +08:00
local labelSendText = cc.LabelTTF : create ( " Send Text " , " Arial " , 22 )
local itemSendText = cc.MenuItemLabel : create ( labelSendText )
2013-06-06 22:29:02 +08:00
itemSendText : registerScriptTapHandler ( onMenuSendTextClicked )
2013-08-22 10:16:57 +08:00
itemSendText : setPosition ( cc.p ( winSize.width / 2 , winSize.height - MARGIN - SPACE ) )
2013-06-06 22:29:02 +08:00
menuRequest : addChild ( itemSendText )
--Send Binary
local function onMenuSendBinaryClicked ( )
if nil ~= wsSendBinary then
2013-09-04 14:02:22 +08:00
if cc.WEBSOCKET_STATE_OPEN == wsSendBinary : getReadyState ( ) then
2013-06-06 22:29:02 +08:00
sendBinaryStatus : setString ( " Send Binary WS is waiting... " )
2013-07-26 22:50:45 +08:00
wsSendBinary : sendString ( " Hello WebSocket中文--, \0 I'm \0 a \0 binary \0 message \0 . " )
2013-06-06 22:29:02 +08:00
else
local warningStr = " send binary websocket instance wasn't ready... "
sendBinaryStatus : setString ( warningStr )
end
end
end
2013-08-22 10:16:57 +08:00
local labelSendBinary = cc.LabelTTF : create ( " Send Binary " , " Arial " , 22 )
local itemSendBinary = cc.MenuItemLabel : create ( labelSendBinary )
2013-06-06 22:29:02 +08:00
itemSendBinary : registerScriptTapHandler ( onMenuSendBinaryClicked )
2013-08-22 10:16:57 +08:00
itemSendBinary : setPosition ( cc.p ( winSize.width / 2 , winSize.height - MARGIN - 2 * SPACE ) )
2013-06-06 22:29:02 +08:00
menuRequest : addChild ( itemSendBinary )
--Send Text Status Label
2013-08-22 10:16:57 +08:00
sendTextStatus = cc.LabelTTF : create ( " Send Text WS is waiting... " , " Arial " , 14 , cc.size ( 160 , 100 ) , cc.VERTICAL_TEXT_ALIGNMENT_CENTER , cc.VERTICAL_TEXT_ALIGNMENT_TOP )
sendTextStatus : setAnchorPoint ( cc.p ( 0 , 0 ) )
sendTextStatus : setPosition ( cc.p ( 0 , 25 ) )
2013-06-06 22:29:02 +08:00
layer : addChild ( sendTextStatus )
--Send Binary Status Label
2013-08-22 10:16:57 +08:00
sendBinaryStatus = cc.LabelTTF : create ( " Send Binary WS is waiting... " , " Arial " , 14 , cc.size ( 160 , 100 ) , cc.VERTICAL_TEXT_ALIGNMENT_CENTER , cc.VERTICAL_TEXT_ALIGNMENT_TOP )
sendBinaryStatus : setAnchorPoint ( cc.p ( 0 , 0 ) )
sendBinaryStatus : setPosition ( cc.p ( 160 , 25 ) )
2013-06-06 22:29:02 +08:00
layer : addChild ( sendBinaryStatus )
--Error Label
2013-08-22 10:16:57 +08:00
errorStatus = cc.LabelTTF : create ( " Error WS is waiting... " , " Arial " , 14 , cc.size ( 160 , 100 ) , cc.VERTICAL_TEXT_ALIGNMENT_CENTER , cc.VERTICAL_TEXT_ALIGNMENT_TOP )
errorStatus : setAnchorPoint ( cc.p ( 0 , 0 ) )
errorStatus : setPosition ( cc.p ( 320 , 25 ) )
2013-06-06 22:29:02 +08:00
layer : addChild ( errorStatus )
2013-08-22 10:16:57 +08:00
local toMainMenu = cc.Menu : create ( )
2013-06-06 22:29:02 +08:00
CreateExtensionsBasicLayerMenu ( toMainMenu )
2013-08-22 10:16:57 +08:00
toMainMenu : setPosition ( cc.p ( 0 , 0 ) )
2013-06-06 22:29:02 +08:00
layer : addChild ( toMainMenu , 10 )
wsSendText = WebSocket : create ( " ws://echo.websocket.org " )
wsSendBinary = WebSocket : create ( " ws://echo.websocket.org " )
wsError = WebSocket : create ( " ws://invalid.url.com " )
local function wsSendTextOpen ( strData )
sendTextStatus : setString ( " Send Text WS was opened. " )
end
local function wsSendTextMessage ( strData )
receiveTextTimes = receiveTextTimes + 1
local strInfo = " response text msg: " .. strData .. " , " .. receiveTextTimes
sendTextStatus : setString ( strInfo )
end
local function wsSendTextClose ( strData )
print ( " _wsiSendText websocket instance closed. " )
sendTextStatus = nil
2013-06-07 16:51:50 +08:00
wsSendText = nil
2013-06-06 22:29:02 +08:00
end
local function wsSendTextError ( strData )
print ( " sendText Error was fired " )
end
local function wsSendBinaryOpen ( strData )
sendBinaryStatus : setString ( " Send Binary WS was opened. " )
end
2013-06-07 10:12:01 +08:00
local function wsSendBinaryMessage ( paramTable )
2013-06-06 22:29:02 +08:00
local length = table.getn ( paramTable )
local i = 1
local strInfo = " response bin msg: "
for i = 1 , length do
if 0 == paramTable [ i ] then
strInfo = strInfo .. " \' \\ 0 \' "
else
strInfo = strInfo .. string.char ( paramTable [ i ] )
end
end
receiveBinaryTimes = receiveBinaryTimes + 1
strInfo = strInfo .. receiveBinaryTimes
sendBinaryStatus : setString ( strInfo )
end
local function wsSendBinaryClose ( strData )
print ( " _wsiSendBinary websocket instance closed. " )
sendBinaryStatus = nil
2013-06-07 16:51:50 +08:00
wsSendBinary = nil
2013-06-06 22:29:02 +08:00
end
local function wsSendBinaryError ( strData )
print ( " sendBinary Error was fired " )
end
local function wsErrorOpen ( strData )
end
local function wsErrorMessage ( strData )
end
local function wsErrorError ( strData )
print ( " Error was fired " )
errorStatus : setString ( " an error was fired " )
end
local function wsErrorClose ( strData )
print ( " _wsiError websocket instance closed. " )
2013-06-07 14:57:36 +08:00
errorStatus = nil
2013-06-07 16:51:50 +08:00
wsError = nil
2013-06-06 22:29:02 +08:00
end
if nil ~= wsSendText then
2013-09-04 14:02:22 +08:00
wsSendText : registerScriptHandler ( wsSendTextOpen , cc.WEBSOCKET_OPEN )
wsSendText : registerScriptHandler ( wsSendTextMessage , cc.WEBSOCKET_MESSAGE )
wsSendText : registerScriptHandler ( wsSendTextClose , cc.WEBSOCKET_CLOSE )
wsSendText : registerScriptHandler ( wsSendTextError , cc.WEBSOCKET_ERROR )
2013-06-06 22:29:02 +08:00
end
if nil ~= wsSendBinary then
2013-09-04 14:02:22 +08:00
wsSendBinary : registerScriptHandler ( wsSendBinaryOpen , cc.WEBSOCKET_OPEN )
wsSendBinary : registerScriptHandler ( wsSendBinaryMessage , cc.WEBSOCKET_MESSAGE )
wsSendBinary : registerScriptHandler ( wsSendBinaryClose , cc.WEBSOCKET_CLOSE )
wsSendBinary : registerScriptHandler ( wsSendBinaryError , cc.WEBSOCKET_ERROR )
2013-06-06 22:29:02 +08:00
end
if nil ~= wsError then
2013-09-04 14:02:22 +08:00
wsError : registerScriptHandler ( wsErrorOpen , cc.WEBSOCKET_OPEN )
wsError : registerScriptHandler ( wsErrorMessage , cc.WEBSOCKET_MESSAGE )
wsError : registerScriptHandler ( wsErrorClose , cc.WEBSOCKET_CLOSE )
wsError : registerScriptHandler ( wsErrorError , cc.WEBSOCKET_ERROR )
2013-06-06 22:29:02 +08:00
end
local function OnExit ( strEventName )
if " exit " == strEventName then
if nil ~= wsSendText then
wsSendText : close ( )
end
if nil ~= wsSendBinary then
wsSendBinary : close ( )
end
if nil ~= wsError then
wsError : close ( )
end
end
end
layer : registerScriptHandler ( OnExit )
return layer
end
function runWebSocketTest ( )
2013-08-22 10:16:57 +08:00
local scene = cc.Scene : create ( )
2013-06-06 22:29:02 +08:00
scene : addChild ( WebSocketTestLayer ( ) )
return scene
end