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; #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 { int random = 0; private Text connect; Color[] colorArray = { Color.white, Color.yellow, Color.blue, Color.black, Color.red, Color.green, Color.grey }; string[] stringArray = { "白", "黄", "青", "黒", "赤", "緑", "グレー" }; #if UNITY_UWP private MessageWebSocket messageWebSocket; private DataWriter messageWriter; private bool busy; #endif // Use this for initialization void Start() { connect = GameObject.Find("Text").GetComponent(); #if UNITY_UWP // HoloLens実機でWebSocket接続開始 OnConnect(); #endif } // Called by GazeGestureManager when the user performs a Select gesture void OnSelect() { } // Called by SpeechManager when the user says the "Reset world" command void OnReset() { } // Called by SpeechManager when the user says the "Drop sphere" command void OnDrop() { } #if UNITY_UWP private void OnConnect() { 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://XXX.XXX.XXX.XXX: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, "Connect Start"); //文字を表示する 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(() => { AppendOutputLine("InvokeOnAppThread : iTween"); //色を変更する 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 } private void AppendOutputLine(string value) { // OutputField.Text += value + "\r\n"; Debug.Log(value); } #endif }