Quick-cocos do the client, Golang do the server, to implement HTTP communication

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

The client Quick-cocos program is as follows:

Local info={hero={
Heroname= "Zhanshen", herolevel=1,herohp=200
},
[1]={
Name= "Fengbao", level=2,att=100
}
}

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 response = Request:getresponsestring ()
Print (response)
End
--Create a request and send the data to the server by POST
Local URL = "Http://192.168.1.101:8080/test"
Local request = Network.createhttprequest (onrequestfinished, URL, "POST")


For key, value in pairs (info) does
For key,value in pairs (value) does
Print (Key,value)
Request:addpostvalue (key, value)
End
End
--Start the request. The callback () function is called when the request is complete
Request:start ()


Service-Side Golang program:

Package Main


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


Type person struct {
NAME string
Age int
}


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


Func Hand (w http. Responsewriter, R *http. Request) {
Fmt. Println ("OK")
W.write ([]byte ("Connect OK"))


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
}



}


Remember to replace the IP address with your own

Related Article

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.