HTTP communication for Quick-cocos and Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Quick-cocos client program:

Local mainscene = Class ("Mainscene", function ()
Return Display.newscene ("Mainscene")
End


Local info={
hero={
Name= "Zhanshen", level=2,hp=500
},
pet={
Name= "Wind", hp=100,attack=200
}


}
function Mainscene:ctor ()
function Onrequestfinished (event)
Local OK = (Event.name = = "Completed")
Local request = Event.request
If not OK then
--Request failed with error code and error message displayed
Print (Request:geterrorcode (), Request:geterrormessage ())
Return
End
Local code = Request:getresponsestatuscode ()
If code ~=
--end of request, but no 200 response code returned
Print (code)
Return
End
--The request succeeds, showing the content returned by the server


Local Resdata=request:getresponsedata ()
Resdata=json.decode (Resdata)
Print (Resdata)
Dump (Resdata)


End
--Create a request and send the data to the server by POST
Local URL = "Http://10.12.15.70:8080/test"
Local request = Network.createhttprequest (onrequestfinished, URL, "POST")


--For k,v in pairs (info) do
--For k,v in pairs (v) do
--Request:addpostvalue (K,V)
--End
--End
Local Js=json.encode (info)
Request:setpostdata (JS)
--Start the request. The callback () function is called when the request is complete
Request:start ()

End


function Mainscene:onenter ()
End


function Mainscene:onexit ()
End


Return Mainscene


Golang Service-side program:

Package Main


Import (
"Encoding/json"
"FMT"
"Io/ioutil"
"Net/http"
)


var info map[string]string


Func Main () {
http. Handlefunc ("/test", hand)
ERR: = http. Listenandserve ("10.12.15.70:8080", nil)
If err! = Nil {
Fmt. Println ("Err")
}
}


Func Hand (w http. Responsewriter, R *http. Request) {


info = make (map[string]string)
info["name"] = "Server"
Info["level"] = "1"
Data, err: = json. Marshal (Info)
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println ("OK")
W.write ([]byte ("Connect OK"))
W.write (data)


R.parseform ()//parse parameter, default is not resolved
Fmt. PRINTLN (R.form)//These are server-side printing information
Fmt. PRINTLN ("Path", R.url. Path)
Fmt. PRINTLN ("scheme", R.url. Scheme)
Fmt. Println (r.form["Url_long"])
For k, V: = Range R.form {
//Fmt. Println ("Key:", K)
//Fmt. The type of Println ("Val:", v)//v is []string


// }
Defer R.body.close ()
Body, err: = Ioutil. ReadAll (R.body)//Read the information returned by the server
If err! = Nil {
Fmt. PRINTLN ("Read err")
}
Fmt. Println (body)
Fmt. Println (String (body))
var tmp interface{}//decode JSON data in unknown format
Json. Unmarshal (body, &tmp)
Fmt. PRINTLN (TMP)
}


Remember to change the IP address to your own

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.