Skip to content

Instantly share code, notes, and snippets.

@giraffesyo
Created September 9, 2018 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giraffesyo/496ce27ee51fc819d72781362a739ec8 to your computer and use it in GitHub Desktop.
Save giraffesyo/496ce27ee51fc819d72781362a739ec8 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Internet : MonoBehaviour
{
static private string baseURL = "http://localhost:3000/";
static private string getallpath = baseURL + "/getall";
static private string adduserpath = baseURL + "/adduser";
public static API_Data api_data = new API_Data();
public static int api_user;
//public int API_UserCount;
void Start()
{
StartCoroutine("addUser");
StartCoroutine("RefreshData");
StartCoroutine("SetController");
}
void Update()
{
}
IEnumerator addUser()
{
{
using(WWW www = new WWW(adduserpath))
{
yield return www;
if (www.text == "")
{
Debug.Log("Api call to " + www.url + " failed");
}
api_user = int.Parse(www.text);
Debug.Log("user id: " + api_user);
}
}
}
IEnumerator RefreshData()
{
for (; ; )
{
yield return new WaitForSeconds(1.0f);
using (WWW www = new WWW(getallpath))
{
yield return www;
if (www.text == "")
{
Debug.Log("Api call to " + www.url + " failed");
}
api_data = JsonUtility.FromJson<API_Data>(www.text);
Debug.Log("Total users: " + api_data.totalSystemUsers);
Debug.Log("Current Operator: " + api_data.currentControllerID);
}
}
}
IEnumerator SetController()
{
{
yield return new WaitForSeconds(2.0f);
using(WWW www = new WWW(baseURL + "/setcontroller/" + api_user.ToString()))
{
yield return www;
Debug.Log( www.url + " returned: " + www.text);
}
}
}
}
[System.Serializable]
public struct API_Data
{ public int totalSystemUsers; public int currentControllerID; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment