Skip to content

Commit c9b63a7

Browse files
committed
fix(lua-tests): address several non-critical issues
1. Use `require 'cjson'` instead of deprecated global variable usage 2. Replace `TMXTiledMap` (removed from axmol years ago) with `FastTMXTiledMap` 3. Update lua-websocket test case URL and note that it uses a public WebSocket echo service, which is not guaranteed to be available long-term and may require VPN access from mainland China 4. Remove the awkward native-to-Lua WebSocket binary data transfer; pass as string instead and add an `isBinary` flag to indicate binary data 5. Remove meaningless touch event registration in `ExtensionTest.lua` 6. On Win32, set console encoding to UTF-8 to comply with axmol engine standards
1 parent 179f0f5 commit c9b63a7

File tree

10 files changed

+178
-258
lines changed

10 files changed

+178
-258
lines changed

extensions/scripting/lua-bindings/manual/network/lua_websocket.cpp

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,6 @@
3737

3838
using namespace ax;
3939

40-
static int SendBinaryMessageToLua(int nHandler,const unsigned char* pTable,int nLength)
41-
{
42-
if (NULL == pTable || nHandler <= 0) {
43-
return 0;
44-
}
45-
46-
if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) {
47-
return 0;
48-
}
49-
50-
LuaStack *pStack = LuaEngine::getInstance()->getLuaStack();
51-
if (NULL == pStack) {
52-
return 0;
53-
}
54-
55-
lua_State *tolua_s = pStack->getLuaState();
56-
if (NULL == tolua_s) {
57-
return 0;
58-
}
59-
60-
int nRet = 0;
61-
LuaValueArray array;
62-
for (int i = 0 ; i < nLength; i++) {
63-
LuaValue value = LuaValue::intValue(pTable[i]);
64-
array.push_back(value);
65-
}
66-
67-
pStack->pushLuaValueArray(array);
68-
nRet = pStack->executeFunctionByHandler(nHandler, 1);
69-
pStack->clean();
70-
return nRet;
71-
}
72-
73-
74-
7540
LuaWebSocket::~LuaWebSocket()
7641
{
7742
ScriptHandlerMgr::getInstance()->removeObjectAllHandlers((void*)this);
@@ -80,7 +45,7 @@ LuaWebSocket::~LuaWebSocket()
8045
void LuaWebSocket::onOpen(WebSocket* ws)
8146
{
8247
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
83-
if (NULL != luaWs) {
48+
if (luaWs) {
8449
int nHandler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN);
8550
if (0 != nHandler) {
8651
CommonScriptData data(nHandler,"");
@@ -93,24 +58,18 @@ void LuaWebSocket::onOpen(WebSocket* ws)
9358
void LuaWebSocket::onMessage(WebSocket* ws, const WebSocket::Data& data)
9459
{
9560
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
96-
if (NULL != luaWs) {
97-
if (data.isBinary) {
98-
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_MESSAGE);
99-
if (0 != handler) {
100-
SendBinaryMessageToLua(handler, (const unsigned char*)data.bytes, (int)data.len);
101-
}
102-
}
103-
else{
104-
105-
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_MESSAGE);
106-
if (0 != handler)
61+
if (luaWs)
62+
{
63+
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(
64+
(void*)this, ScriptHandlerMgr::HandlerType::WEBSOCKET_MESSAGE);
65+
if (0 != handler)
66+
{
67+
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
68+
if (nullptr != stack)
10769
{
108-
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
109-
if (nullptr != stack)
110-
{
111-
stack->pushString(data.bytes,(int)data.len);
112-
stack->executeFunctionByHandler(handler, 1);
113-
}
70+
stack->pushString(data.bytes, (int)data.len);
71+
stack->pushBoolean(data.isBinary);
72+
stack->executeFunctionByHandler(handler, 2);
11473
}
11574
}
11675
}
@@ -119,7 +78,7 @@ void LuaWebSocket::onMessage(WebSocket* ws, const WebSocket::Data& data)
11978
void LuaWebSocket::onClose(WebSocket* ws, uint16_t code, std::string_view reason)
12079
{
12180
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
122-
if (NULL != luaWs) {
81+
if (luaWs) {
12382
int nHandler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_CLOSE);
12483
if (0 != nHandler)
12584
{
@@ -133,7 +92,7 @@ void LuaWebSocket::onClose(WebSocket* ws, uint16_t code, std::string_view reason
13392
void LuaWebSocket::onError(WebSocket* ws, const WebSocket::ErrorCode& error)
13493
{
13594
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
136-
if (NULL != luaWs) {
95+
if (luaWs) {
13796
int nHandler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_ERROR);
13897
if (0 != nHandler)
13998
{
@@ -172,7 +131,7 @@ static int axlua_WebSocket_create00(lua_State* tolua_S)
172131
if (argumentCount >= 2)
173132
{
174133
std::string_view url;
175-
const char* protocols{nullptr};
134+
std::string_view protocols = ""sv;
176135
std::string_view caCertPath;
177136

178137
#ifndef TOLUA_RELEASE
@@ -196,7 +155,7 @@ static int axlua_WebSocket_create00(lua_State* tolua_S)
196155
else if (argumentCount == 4)
197156
{
198157
luaval_to_std_string_view(tolua_S, 3, &caCertPath);
199-
protocols = lua_tostring(tolua_S, 4);
158+
protocols = axlua_tosv(tolua_S, 4);
200159
}
201160

202161
luaval_to_std_string_view(tolua_S, 2, &url);
@@ -231,7 +190,7 @@ static int axlua_WebSocket_getReadyState00(lua_State* tolua_S)
231190
{
232191
LuaWebSocket *self = (LuaWebSocket*)tolua_tousertype(tolua_S,1,0);
233192
int tolua_ret = -1;
234-
if (NULL != self) {
193+
if (self) {
235194
tolua_ret = (int)self->getReadyState();
236195
}
237196
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
@@ -261,7 +220,7 @@ static int axlua_WebSocket_close00(lua_State* tolua_S)
261220
#endif
262221
{
263222
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
264-
if (NULL != self ) {
223+
if (self) {
265224
self->closeAsync();
266225
}
267226
}
@@ -292,7 +251,7 @@ static int axlua_WebSocket_sendString00(lua_State* tolua_S)
292251
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
293252
size_t size = 0;
294253
const char* data = (const char*) lua_tolstring(tolua_S, 2, &size);
295-
if ( NULL == data)
254+
if (!data)
296255
return 0;
297256

298257
if (strlen(data) != size)
@@ -361,7 +320,7 @@ TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){
361320
#ifdef __cplusplus
362321
tolua_cclass(tolua_S,"WebSocket","ax.WebSocket","",tolua_collect_WebSocket);
363322
#else
364-
tolua_cclass(tolua_S,"WebSocket","ax.WebSocket","",NULL);
323+
tolua_cclass(tolua_S,"WebSocket","ax.WebSocket","", nullptr);
365324
#endif
366325
tolua_beginmodule(tolua_S,"WebSocket");
367326
tolua_function(tolua_S, "create", axlua_WebSocket_create00);
@@ -390,7 +349,7 @@ int axlua_WebSocket_registerScriptHandler00(lua_State* tolua_S)
390349
#endif
391350
{
392351
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
393-
if (NULL != self ) {
352+
if (self) {
394353
int handler = ( toluafix_ref_function(tolua_S,2,0));
395354
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)((int)tolua_tonumber(tolua_S,3,0) + (int)ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN);
396355
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, handlerType);
@@ -418,7 +377,7 @@ int axlua_WebSocket_unregisterScriptHandler00(lua_State* tolua_S)
418377
#endif
419378
{
420379
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
421-
if (NULL != self ) {
380+
if (self) {
422381
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)((int)tolua_tonumber(tolua_S,2,0) + (int)ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN);
423382

424383
ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerType);

tests/cpp-tests/Source/NetworkTest/WebSocketTest/WebSocketTest.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@
2929

3030
#include "base/format.h"
3131

32-
/* "ws://echo.websocket.org no longer avaiable: https://www.lob.com/blog/websocket-org-is-down-here-is-an-alternative
33-
list of public test servers:
34-
- wss://echo.websocket.events/.ws
35-
- wss://ws.postman-echo.com/raw
36-
- wss://ws.postman-echo.com/socketio handshake path: random, handling
37-
- wss://socketsbay.com/wss/v2/1/demo/
32+
/* https://websocket.org/
33+
list of public test servers: (Note, on china mainland, may need VPN):
34+
- wss://ws.ifelse.io (available, test on Sep.5 2025)
35+
- wss://echo.websocket.org (available, test on Sep.5 2025)
36+
- wss://echo.websocket.events/.ws (unavailable, test on Sep.5 2025)
37+
- wss://ws.postman-echo.com/raw (not test)
38+
- wss://ws.postman-echo.com/socketio handshake path: random, handling (not test)
39+
- wss://socketsbay.com/wss/v2/1/demo/ (not test)
3840
- https://blog.postman.com/introducing-postman-websocket-echo-service/
41+
- other https://www.lob.com/blog/websocket-org-is-down-here-is-an-alternative
3942
*/
40-
#define ECHO_SERVER_URL "wss://echo.websocket.events/.ws" //"wss://ws.postman-echo.com/raw"
43+
#define ECHO_SERVER_URL "wss://ws.ifelse.io"
4144
#define SOCKETIO_SERVICE "wss://ws.postman-echo.com/socketio"
4245

4346
using namespace ax;

tests/cpp-tests/Source/TileMapTest/TileMapTest2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ FastTileMapTests::FastTileMapTests()
7373
TileDemoNew::TileDemoNew()
7474
{
7575
// fix bug #486, #419.
76-
// "test" is the default value in Director::setGLDefaultValues()
76+
// "test" is the default value in Director::setRenderDefaults()
7777
// but TransitionTest may setDepthTest(false), we should revert it here
7878
Director::getInstance()->getRenderer()->setDepthTest(true);
7979
Director::getInstance()->getRenderer()->setDepthWrite(true);

tests/lua-tests/Content/src/DownloaderTest/DownloaderTest.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local json = cjson
1+
local json = require 'cjson'
22

33

44
local imgURI = "https://forum.cocos.com/images/logo.png"

tests/lua-tests/Content/src/ExtensionTest/ExtensionTest.lua

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -193,40 +193,6 @@ local function ExtensionsMainLayer()
193193

194194
layer:addChild(menu)
195195

196-
-- handling touch events
197-
local beginPos = {x = 0, y = 0}
198-
local function onTouchesBegan(touches, event)
199-
beginPos = touches[1]:getLocation()
200-
end
201-
202-
local function onTouchesMoved(touches, event)
203-
local location = touches[1]:getLocation()
204-
205-
local nMoveY = location.y - beginPos.y
206-
local curPosx, curPosy = menu:getPosition()
207-
local nextPosy = curPosy + nMoveY
208-
local winSize = ax.Director:getInstance():getWinSize()
209-
if nextPosy < 0 then
210-
menu:setPosition(0, 0)
211-
return
212-
end
213-
214-
if nextPosy > ((ExtensionTestEnum.TEST_MAX_COUNT + 1) * LINE_SPACE - winSize.height) then
215-
menu:setPosition(0, ((ExtensionTestEnum.TEST_MAX_COUNT + 1) * LINE_SPACE - winSize.height))
216-
return
217-
end
218-
219-
menu:setPosition(curPosx, nextPosy)
220-
beginPos = {x = location.x, y = location.y}
221-
end
222-
223-
local listener = ax.EventListenerTouchAllAtOnce:create()
224-
listener:registerScriptHandler(onTouchesBegan,ax.Handler.EVENT_TOUCHES_BEGAN )
225-
listener:registerScriptHandler(onTouchesMoved,ax.Handler.EVENT_TOUCHES_MOVED )
226-
227-
local eventDispatcher = layer:getEventDispatcher()
228-
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)
229-
230196
return layer
231197
end
232198

tests/lua-tests/Content/src/ExtensionTest/WebProxyTest.lua

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
--[[ https://websocket.org/
2+
list of public test servers (Note, on china mainland, may need VPN):
3+
- wss://ws.ifelse.io (available, test on Sep.5 2025)
4+
- wss://echo.websocket.org (available, test on Sep.5 2025)
5+
- wss://echo.websocket.events/.ws (unavailable, test on Sep.5 2025)
6+
- wss://ws.postman-echo.com/raw (not test)
7+
- wss://ws.postman-echo.com/socketio handshake path: random, handling (not test)
8+
- wss://socketsbay.com/wss/v2/1/demo/ (not test)
9+
- https://blog.postman.com/introducing-postman-websocket-echo-service/
10+
- other https://www.lob.com/blog/websocket-org-is-down-here-is-an-alternative
11+
]]
12+
local ECHO_SERVER_URL = "wss://ws.ifelse.io"
13+
14+
local function bin2hex(str)
15+
return (str:gsub('.', function(c)
16+
return string.format('%02X', string.byte(c))
17+
end))
18+
end
19+
120
local function WebSocketTestLayer()
221
local layer = ax.Layer:create()
322
local winSize = ax.Director:getInstance():getWinSize()
@@ -85,17 +104,17 @@ local function WebSocketTestLayer()
85104
toMainMenu:setPosition(ax.p(0, 0))
86105
layer:addChild(toMainMenu,10)
87106

88-
wsSendText = ax.WebSocket:create("wss://echo.websocket.org", {"myprotocol_1", "myprotocol_2"}, "cacert.pem")
89-
wsSendBinary = ax.WebSocket:create("ws://echo.websocket.org", {"hello"}, "cacert.pem")
90-
wsError = ax.WebSocket:create("ws://invalid.url.com", {"invalid_protocol"})
107+
wsSendText = ax.WebSocket:create(ECHO_SERVER_URL, "cacert.pem")
108+
wsSendBinary = ax.WebSocket:create(ECHO_SERVER_URL, "cacert.pem")
109+
wsError = ax.WebSocket:create("ws://invalid.url.com")
91110

92111
local function wsSendTextOpen(strData)
93112
if sendTextStatus ~= nil then
94113
sendTextStatus:setString("Send Text WS was opened, url:" .. wsSendText.url .. ", protocol: " .. wsSendText.protocol)
95114
end
96115
end
97116

98-
local function wsSendTextMessage(strData)
117+
local function wsSendTextMessage(strData, isBinary)
99118
receiveTextTimes= receiveTextTimes + 1
100119
local strInfo= "response text msg: "..strData..", "..receiveTextTimes
101120
if sendTextStatus ~= nil then
@@ -122,19 +141,17 @@ local function WebSocketTestLayer()
122141
end
123142
end
124143

125-
local function wsSendBinaryMessage(paramTable)
126-
local length = #(paramTable)
127-
local i = 1
128-
local strInfo = "response bin msg: "
129-
for i = 1,length do
130-
if 0 == paramTable[i] then
131-
strInfo = strInfo.."\'\\0\'"
132-
else
133-
strInfo = strInfo..string.char(paramTable[i])
134-
end
144+
local function wsSendBinaryMessage(reply, isBinary)
145+
local strInfo = nil
146+
if not isBinary then
147+
strInfo = reply
148+
else
149+
local length = #(reply)
150+
local i = 1
151+
receiveBinaryTimes = receiveBinaryTimes + 1
152+
strInfo = '#' .. receiveBinaryTimes .. " response bin msg: "
153+
strInfo = strInfo .. bin2hex(reply)
135154
end
136-
receiveBinaryTimes = receiveBinaryTimes + 1
137-
strInfo = strInfo..receiveBinaryTimes
138155
if sendBinaryStatus ~= nil then
139156
sendBinaryStatus:setString(strInfo)
140157
end

0 commit comments

Comments
 (0)