cocos2dx 网络连接

cocos2dx 网络连接,第1张

概述XMLHttpRequest ------------------------------------------------ 数据转换,将请求数据 由table转化为stringfunction dataParse(data) if type(data) ~= "table" then return nil end local tmp = {} for key, value


XMLhttpRequest

------------------------------------------------ 数据转换,将请求数据 由table转化为stringfunction dataParse(data)		if type(data) ~= "table" then		return nil	end	local tmp = {}	for key,value in pairs(data) do		table.insert(tmp,key .. "=" .. value)	end	local newData = ""	for i = 1,#tmp do		newData = newData .. tostring(tmp[i])		if i < #tmp then			newData = newData .. "&"		end	end	-- cclog("data:" .. newData)	return newDataend----------------------------------- POST 型,发送数据function sendInfoToSync()		local url = "www.baIDu.com" 				-- 网址	local xhr = cc.XMLhttpRequest:new()	xhr.responseType = cc.XMLhttpREQUEST_RESPONSE_STRING	xhr.timeout = 30			-- 判断连接超时时间,秒	-- 响应函数	local onReadyFunc = function()		if xhr.status ~= -1 then		-- 连接成功			local result = xhr.response 			-- string			local cJson = require("cJson")			local info = cJson.decode(result)		-- Json 解析成table		else  		-- 连接失败		end	end	xhr:registerScriptHandler(onReadyFunc)	xhr:open("POST",url)	local data = {}	data.account = 1	local newData = dataParse(data)	xhr:send(newData)end---------------------------------------- GET型,获取数据function loadInfoFromSync()		local url = "www.baIDu.com"	local xhr = cc.XMLhttpRequest:new()	xhr.responseType = cc.XMLhttpREQUEST_RESPONSE_JsON	xhr.timeout = 30	local onReadyFunc = function()		if xhr.status ~= -1 then		-- 连接成功			local result = xhr.response 			-- string			local cJson = require("cJson")			local info = cJson.decode(result)		-- Json 解析成table		else  		-- 连接失败		end	end	xhr:registerScriptHandler(onReadyFunc)	local data = {}	data.account = 1	local newData = url .. "?" .. dataParse(data)	xhr:open("GET",newData)	xhr:send()end
总结

以上是内存溢出为你收集整理的cocos2dx 网络连接全部内容,希望文章能够帮你解决cocos2dx 网络连接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1066798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存