node.js + mysql + Unity 연동
Sever/Node.js 2018. 10. 19. 17:46반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | var express = require('express'); var app = express(); var mysql_dbc = require('./db_con')(); var body_parser = require('body-parser'); //app.use(body_parser()); app.use(express.json()); app.get('/', (req, res)=>{ res.send("connected."); }); app.get('/personList', (req, res)=>{ var connection = mysql_dbc.init(); mysql_dbc.test_poen(connection); var query = "select * from persons"; connection.query(query, (err, result, fields)=>{ if(err) throw err; console.log(result); connection.end(); res.send(result); }); }); app.post('/addPerson', (req, res)=>{ console.log(req.body); var firstName = req.body.firstName; var lastName = req.body.lastName; var age = req.body.age; console.log(firstName, lastName, age); var connection = mysql_dbc.init(); mysql_dbc.test_poen(connection); var query = 'insert into persons (firstname, lastname, age) values (?, ?, ?)'; var params = [firstName, lastName, age]; connection.query(query, params, (err, result, fields)=>{ if(err) throw err; console.log(result); }); }); app.post('/updateAge', (req, res)=>{ console.log(req.body); var id = req.body.id; var age = req.body.age; var connection = mysql_dbc.init(); mysql_dbc.test_poen(connection); var query = 'update persons set age = ? where id = ?'; var params = [age, id]; connection.query(query, params, (err, result, fields)=>{ if(err) throw err; console.log(result); }); }); app.post app.listen(3000, ()=>{ console.log("server started!"); }); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | var mysql = require('mysql'); var conf = require('./db_info').local; module.exports = function(){ return { init : function (){ return mysql.createConnection({ host: conf.host, port: conf.port, user: conf.user, password: conf.password, database: conf.database }); }, test_poen : function(con){ con.connect(function(err){ if(err){ console.error('mysql connection failed' + err); }else{ console.info('mysql is connected'); } }) } } }; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | module.exports =( function(){ return { local: { host : 'localhost', port : '3306', user : 'root', password : '000000', database : 'testdb' } } } )(); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using Newtonsoft.Json; //[System.Serializable] public class add_person { public string firstName; public string lastName; public int age; public add_person(string firstName, string lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } } public struct update_age { public int id; public int age; } public class Test : MonoBehaviour { public object[] Encording { get; private set; } private List<PersonData> personDataList; // Use this for initialization void Start () { } IEnumerator AddPerson(PersonData personData) { var add_person = new add_person(personData.firstName, personData.lastName, personData.age); var json = JsonConvert.SerializeObject(add_person); Debug.LogFormat("{0}", json); List<IMultipartFormSection> formData = new List<IMultipartFormSection>(); formData.Add(new MultipartFormDataSection("data", json)); UnityWebRequest www = UnityWebRequest.Post("localhost:3000/addPerson", formData); yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.LogFormat("{0}", www.error); } else { Debug.LogFormat("{0}", www.downloadHandler.text); } } IEnumerator GetPersons() { using (UnityWebRequest www = UnityWebRequest.Get("localhost:3000/personList")) { yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log(www.downloadHandler.text); byte[] results = www.downloadHandler.data; var json = System.Text.Encoding.Default.GetString(results); Debug.LogFormat("{0} {1}", results.Length, json); this.personDataList = JsonConvert.DeserializeObject<List<PersonData>>(json); foreach (var data in this.personDataList) { Debug.LogFormat("{0}, {1}, {2}, {3}", data.id, data.firstName, data.lastName, data.age); } } } } // server side post data is null ? //https://stackoverflow.com/questions/43292700/unitywebrequest-downloadhandler-returning-null-while-getting-response-from-node?rq=1 IEnumerator Post(string uri , string b) { byte[] bytes = System.Text.Encoding.ASCII.GetBytes(b); using (UnityWebRequest www = new UnityWebRequest(uri, UnityWebRequest.kHttpVerbPOST)) { UploadHandlerRaw uH = new UploadHandlerRaw(bytes); DownloadHandlerBuffer dH = new DownloadHandlerBuffer(); www.uploadHandler = uH; www.downloadHandler = dH; www.SetRequestHeader("Content-Type", "application/json"); yield return www.SendWebRequest(); if (www.isHttpError || www.isNetworkError) { Debug.Log(www.error); } else { Debug.Log(www.ToString()); Debug.Log(www.downloadHandler.text); } } } IEnumerator UpdateAge(string uri, string data) { var bytes = System.Text.Encoding.UTF8.GetBytes(data); using (UnityWebRequest www = new UnityWebRequest(uri, UnityWebRequest.kHttpVerbPOST)) { UploadHandlerRaw uh = new UploadHandlerRaw(bytes); DownloadHandlerBuffer dh = new DownloadHandlerBuffer(); www.uploadHandler = uh; www.downloadHandler = dh; www.SetRequestHeader("Content-Type", "application/json"); yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.LogFormat("{0}", www.isHttpError); } else { Debug.LogFormat("{0}", www.downloadHandler.text); } } } private void OnGUI() { if (GUILayout.Button("GET", GUILayout.Width(100), GUILayout.Height(60))) { StartCoroutine(GetPersons()); } if (GUILayout.Button("POST", GUILayout.Width(100), GUILayout.Height(60))) { var personData = new PersonData("aaaaaaa", "bbbbbb", 1234); //StartCoroutine(AddPerson(personData)); var json = JsonConvert.SerializeObject(personData); StartCoroutine(this.Post("localhost:3000/addPerson", json)); } if (GUILayout.Button("UPDATE", GUILayout.Width(100), GUILayout.Height(60))) { var update_age = new update_age(); update_age.id = 7; update_age.age = 11; var json = JsonConvert.SerializeObject(update_age); StartCoroutine(this.UpdateAge("localhost:3000/updateAge", json)); } if (GUILayout.Button("DELETE", GUILayout.Width(100), GUILayout.Height(60))) { } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class PersonData { public int id; public string firstName; public string lastName; public int age; public PersonData(string firstName, string lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } } | cs |
반응형
'Sever > Node.js' 카테고리의 다른 글
jwt 스터디 1차 (0) | 2019.01.02 |
---|---|
jwt (0) | 2019.01.01 |
Node.js Passport login (0) | 2018.12.20 |
Study : Express, RESTful API (0) | 2018.10.24 |
카페24 node.js + mysql + unity 연동 (CRUD, RestfulAPI) (4) | 2018.10.22 |