82 lines
2.1 KiB
C#
82 lines
2.1 KiB
C#
using System;
|
|
using Firesplash.UnityAssets.SocketIO;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ExampleScript : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
private struct ItsMeData
|
|
{
|
|
public string version;
|
|
}
|
|
|
|
[Serializable]
|
|
private struct ServerTechData
|
|
{
|
|
public string timestamp;
|
|
|
|
public string podName;
|
|
}
|
|
|
|
public SocketIOCommunicator sioCom;
|
|
|
|
public Text uiStatus;
|
|
|
|
public Text uiGreeting;
|
|
|
|
public Text uiPodName;
|
|
|
|
private void Start()
|
|
{
|
|
sioCom.Instance.On("connect", delegate
|
|
{
|
|
Debug.Log("LOCAL: Hey, we are connected!");
|
|
uiStatus.text = "Socket.IO Connected. Doing work...";
|
|
sioCom.Instance.Emit("KnockKnock");
|
|
});
|
|
sioCom.Instance.On("WhosThere", delegate(string payload)
|
|
{
|
|
if (payload == null)
|
|
{
|
|
Debug.Log("RECEIVED a WhosThere event without payload data just as expected.");
|
|
}
|
|
ItsMeData itsMeData = new ItsMeData
|
|
{
|
|
version = Application.unityVersion
|
|
};
|
|
sioCom.Instance.Emit("ItsMe", JsonUtility.ToJson(itsMeData), DataIsPlainText: false);
|
|
});
|
|
sioCom.Instance.On("Welcome", delegate(string payload)
|
|
{
|
|
Debug.Log("SERVER: " + payload);
|
|
uiGreeting.text = payload;
|
|
});
|
|
sioCom.Instance.On("TechData", delegate(string payload)
|
|
{
|
|
ServerTechData serverTechData = JsonUtility.FromJson<ServerTechData>(payload);
|
|
Debug.Log("Received the POD name from the server. Upadting UI. Oh! It's " + serverTechData.timestamp + " by the way.");
|
|
uiPodName.text = "I talked to " + serverTechData.podName;
|
|
sioCom.Instance.Emit("SendNumbers");
|
|
});
|
|
sioCom.Instance.On("RandomNumbers", delegate(string payload)
|
|
{
|
|
Debug.Log("We received the following JSON payload from the server for example 6: " + payload);
|
|
sioCom.Instance.Emit("Goodbye", "Thanks for talking to me!", DataIsPlainText: true);
|
|
});
|
|
sioCom.Instance.On("disconnect", delegate(string payload)
|
|
{
|
|
if (payload.Equals("io server disconnect"))
|
|
{
|
|
Debug.Log("Disconnected from server.");
|
|
uiStatus.text = "Finished. Server closed connection.";
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("We have been unexpecteldy disconnected. This will cause an automatic reconnect. Reason: " + payload);
|
|
}
|
|
});
|
|
sioCom.Instance.Connect();
|
|
}
|
|
}
|