using UnityEngine; using System.Collections; using UnityEngine.Networking; using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Collections.Generic; using MiniJSON; using UnityEngine.UI; using HoloToolkit.Unity.InputModule; #if UNITY_UWP using Windows.Foundation; using Windows.Networking.Sockets; using Windows.Security.Cryptography.Certificates; using Windows.Storage.Streams; using System.Threading.Tasks; #else using System.Text; #endif public class WebSocket : MonoBehaviour,IInputClickHandler { int random = 0; private Text connect; private bool check = false; int count = 0; Color[] colorArray = { Color.white, Color.yellow, Color.blue, Color.black, Color.red, Color.green, Color.grey }; string[] stringArray = { "white", "yellow", "blue", "black", "red", "green", "grey" }; #if UNITY_UWP private MessageWebSocket messageWebSocket; private DataWriter messageWriter; #endif // Use this for initialization void Awake() { connect = GameObject.Find("Text").GetComponent(); #if UNITY_UWP // HoloLens実機でWebSocket接続開始 OnConnect("Start"); #endif } public void OnInputClicked(InputClickedEventData eventData) { // ここにAirTapを検出したときの処理を書く OnSelect1(); } void OnSelect1() { var dic = Json.Deserialize("{\"value1\":\"\",\"value2\":2,\"value3\":3}") as Dictionary; dic["value1"] = System.DateTime.Now.ToString(); dic["value2"] = this.gameObject.name; dic["value3"] = stringArray[random].ToString();//色の名前を送信する string json = Json.Serialize(dic); Debug.Log(json); connect.text = stringArray[random]; #if UNITY_UWP OnConnect(json); #endif } #if UNITY_UWP private void OnConnect(string json) { AppendOutputLine("OnConnect"); messageWebSocket = new MessageWebSocket(); //In this case we will be sending/receiving a string so we need to set the MessageType to Utf8. messageWebSocket.Control.MessageType = SocketMessageType.Utf8; //Add the MessageReceived event handler. messageWebSocket.MessageReceived += WebSock_MessageReceived; //Add the Closed event handler. messageWebSocket.Closed += WebSock_Closed; Uri serverUri = new Uri("ws://192.168.0.24:1880"); // 別PCのNode-REDのWebSocketにつながる try { Task.Run(async () => { //Connect to the server. AppendOutputLine("Connect to the server...." + serverUri.ToString()); await Task.Run(async () => { await messageWebSocket.ConnectAsync(serverUri); AppendOutputLine("ConnectAsync OK"); await WebSock_SendMessage(messageWebSocket, json); //文字を表示する connect.text = "start"; }); }); } catch (System.Exception ex) { AppendOutputLine("error : " + ex.ToString()); connect.text = "error"; //Add code here to handle any exceptions } } private async Task WebSock_SendMessage(MessageWebSocket webSock, string message) { AppendOutputLine("WebSock_SendMessage : " + message); DataWriter messageWriter = new DataWriter(webSock.OutputStream); messageWriter.WriteString(message); await messageWriter.StoreAsync(); } private void WebSock_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) { DataReader messageReader = args.GetDataReader(); messageReader.UnicodeEncoding = UnicodeEncoding.Utf8; string messageString = messageReader.ReadString(messageReader.UnconsumedBufferLength); AppendOutputLine("messageString : " + messageString); //Add code here to do something with the string that is received. Task.Run(async () => { UnityEngine.WSA.Application.InvokeOnAppThread(() => { //色を変更する random = UnityEngine.Random.Range(0, 6); this.GetComponent().material.color = colorArray[random]; }, true); await Task.Delay(100); }); } private void WebSock_Closed(IWebSocket sender, WebSocketClosedEventArgs args) { //Add code here to do something when the connection is closed locally or by the server connect.text = "close"; } private void AppendOutputLine(string value) { // OutputField.Text += value + "\r\n"; Debug.Log(value); } #endif }