123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Collections.Generic;
- using SocketIOClient;
- using SocketIOClient.Newtonsoft.Json;
- using UnityEngine;
- using UnityEngine.UI;
- using Newtonsoft.Json.Linq;
- using Debug = System.Diagnostics.Debug;
- public class SocketManager : MonoBehaviour
- {
- public SocketIOUnity socket;
- public InputField EventNameTxt;
- public InputField DataTxt;
- public Text ReceivedText;
- public GameObject objectToSpin;
- // Start is called before the first frame update
- void Start()
- {
- //TODO: check the Uri if Valid.
- var uri = new Uri("http://127.0.0.1:11100");
- socket = new SocketIOUnity(uri, new SocketIOOptions
- {
- Query = new Dictionary<string, string>
- {
- {"token", "UNITY" }
- }
- ,
- EIO = 4
- ,
- Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
- });
- socket.JsonSerializer = new NewtonsoftJsonSerializer();
- ///// reserved socketio events
- socket.OnConnected += (sender, e) =>
- {
- UnityEngine.Debug.Log("socket.OnConnected");
- };
- socket.OnPing += (sender, e) =>
- {
- UnityEngine.Debug.Log("Ping");
- };
- socket.OnPong += (sender, e) =>
- {
- UnityEngine.Debug.Log("Pong: " + e.TotalMilliseconds);
- };
- socket.OnDisconnected += (sender, e) =>
- {
- UnityEngine.Debug.Log("disconnect: " + e);
- };
- socket.OnReconnectAttempt += (sender, e) =>
- {
- UnityEngine.Debug.Log($"{DateTime.Now} Reconnecting: attempt = {e}");
- };
- ////
- UnityEngine.Debug.Log("Connecting...");
- socket.Connect();
- socket.OnUnityThread("spin", (data) =>
- {
- rotateAngle = 0;
- });
- ReceivedText.text = "";
- socket.OnAnyInUnityThread((name, response) =>
- {
- ReceivedText.text += "Received On " + name + " : " + response.GetValue().GetRawText() + "\n";
- });
- }
- public void EmitTest()
- {
- string eventName = EventNameTxt.text.Trim().Length < 1 ? "hello" : EventNameTxt.text;
- string txt = DataTxt.text;
- if (!IsJSON(txt))
- {
- socket.Emit(eventName, txt);
- }
- else
- {
- socket.EmitStringAsJSON(eventName, txt);
- }
- }
- public static bool IsJSON(string str)
- {
- if (string.IsNullOrWhiteSpace(str)) { return false; }
- str = str.Trim();
- if ((str.StartsWith("{") && str.EndsWith("}")) || //For object
- (str.StartsWith("[") && str.EndsWith("]"))) //For array
- {
- try
- {
- var obj = JToken.Parse(str);
- return true;
- }catch (Exception ex) //some other exception
- {
- Console.WriteLine(ex.ToString());
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- public void EmitSpin()
- {
- socket.Emit("spin");
- }
- public void EmitClass()
- {
- TestClass testClass = new TestClass(new string[] { "foo", "bar", "baz", "qux" });
- TestClass2 testClass2 = new TestClass2("lorem ipsum");
- socket.Emit("class", testClass2);
- }
- // our test class
- [System.Serializable]
- class TestClass
- {
- public string[] arr;
- public TestClass(string[] arr)
- {
- this.arr = arr;
- }
- }
- [System.Serializable]
- class TestClass2
- {
- public string text;
- public TestClass2(string text)
- {
- this.text = text;
- }
- }
- //
- float rotateAngle = 45;
- readonly float MaxRotateAngle = 45;
- void Update()
- {
- if(rotateAngle < MaxRotateAngle)
- {
- rotateAngle++;
- objectToSpin.transform.Rotate(0, 1, 0);
- }
- }
- }
|