Unity3d access to the Go HTTP Server example via HTTP

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

Unity section:

Create a new script and hang it under a random object. The code is as follows:

using UnityEngine;using System.Collections;using System.Text;public class TestScript : MonoBehaviour{    public string url = "http://127.0.0.1:8000/test";public string test_string;// Use this for initializationIEnumerator Start (){    WWW www = new WWW (url);    yield return www;    print (www.bytes);    test_string = System.Text.Encoding.UTF8.GetString (www.bytes);    print (test_string);}// Update is called once per framevoid Update (){}}

Go section

<!--lang:cpp-->package mainimport ("FMT" "Html/template" "Net/http") func main () {FMT. Printf ("Server start\n") http. Handle ("/img/", http. Fileserver (http. Dir ("TPL"))) HTTP. Handlefunc ("/", Notfoundhandler) http. Handlefunc ("/index", Indexhandler) http. Handlefunc ("/test", Testhandler) http. Listenandserve (": 8000", nil) fmt. Printf ("Server closed\n")}func Notfoundhandler (w http. Responsewriter, R *http. Request) {if r.url. Path = = "/" {http. Redirect (W, R, "/index", http. Statusfound)} t, E: = template. Parsefiles ("tpl/html/404.html") if E! = nil {errorlog (e)} t.execute (W, nil)}func Testhandler (w http. Responsewriter, R *http. Request) {//below for Test FMT. Println ("Postform", r.postform) Test: = R.postform.get ("Test") fmt.    Println ("Test string:", test)//upside for test returnstring: = "return test value!" Returnbytes: = []byte (returnstring) I, E: = W.write (returnbytes) fmt. Println ("I,e:", I, E)}func IndexhandLer (w http. Responsewriter, R *http. Request) {T, E: = template. Parsefiles ("tpl/html/index.html") if E! = nil {errorlog (e)} t.execute (W, nil)}func errorlog (e error) {FM T.println (E)}
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.