Code fragment: local ws = cc.WebSocket:create("url here")
local function onOpen()
print("ws open")
end
local function onClose()
print("ws close")
end
local function onMessage(msg)
print("ws msg is " .. msg)
end
local function onError()
print("ws error occurs")
end
ws:registerScriptHandler(onOpen, cc.WEBSOCKET_OPEN)
ws:registerScriptHandler(onClose, cc.WEBSOCKET_CLOSE)
ws:registerScriptHandler(onMessage, cc.WEBSOCKET_MESSAGE)
ws:registerScriptHandler(onError, cc.WEBSOCKET_ERROR)
if ws:getReadyState() == cc.WEBSOCKET_STATE_CONNECTING then
ws:close()
end
The program will be blocked, and the following code will not be executed!!! (cocos2d-x 3.2 lua)
Code fragment:
local ws = cc.WebSocket:create("url here")
local function onOpen()
print("ws open")
end
local function onClose()
print("ws close")
end
local function onMessage(msg)
print("ws msg is " .. msg)
end
local function onError()
print("ws error occurs")
end
ws:registerScriptHandler(onOpen, cc.WEBSOCKET_OPEN)
ws:registerScriptHandler(onClose, cc.WEBSOCKET_CLOSE)
ws:registerScriptHandler(onMessage, cc.WEBSOCKET_MESSAGE)
ws:registerScriptHandler(onError, cc.WEBSOCKET_ERROR)
if ws:getReadyState() == cc.WEBSOCKET_STATE_CONNECTING then
ws:close()
end
The program will be blocked, and the following code will not be executed!!! (cocos2d-x 3.2 lua)