void OnDisable()
 {
     Debug.Log("Closing");
     if (webSocket != null && webSocket.IsOpened())
     {
         webSocket.Close();
     }
 }
Exemplo n.º 2
0
    void OnGUI()
    {
        // Make a background box
        GUI.Box(new Rect(10, 10, 400, 400), "Actions");

        // Check if the websocket has an opened connection
        if (webSocket == null || !webSocket.IsOpened())
        {
            // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
            if (GUI.Button(new Rect(30, 40, 320, 80), "Connect"))
            {
                // Warning : some libraries (for example on ios) don't support reconnection
                //         so you need to create a new websocketunity before each connect (destroy your object when receive a disconnect or an error)
                webSocket = new WebSocketUnity("ws://127.0.0.1:5566", this);                 // <= public server
                //webSocket = new WebSocketUnity("ws://localhost:8080", this); // <= local server

                // Second Step : we open the connection
                webSocket.Open();
            }
        }
        else
        {
            // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
            if (GUI.Button(new Rect(30, 40, 320, 80), "Disconnect"))
            {
                // Third Step : we need to close the connection when we finish
                webSocket.Close();
            }

            if (GUI.Button(new Rect(30, 130, 320, 80), "Send message data"))
            {
                // create or modify json object
                JsonData newFriend = new JsonData();
                newFriend["Hello"]         = new JsonData();
                newFriend["Hello"]["Name"] = "terry";
                string strFriends = newFriend.ToJson();
                Debug.Log(strFriends);

                webSocket.Send(strFriends);
                sendingMessage = true;
            }

            if (sendingMessage)
            {
                GUI.Label(new Rect(30, 350, 400, 60), "Waiting answer from server...");
            }
            else if (receivedMessage)
            {
                GUI.Label(new Rect(30, 350, 400, 60), "Server sent an echo answer !");
            }
        }
    }
Exemplo n.º 3
0
    void OnGUI()
    {
        // Make a background box
        GUI.Box(new Rect(10, 10, 400, 400), "Actions");

        // Check if the websocket has an opened connection
        if (webSocket == null || !webSocket.IsOpened())
        {
            // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
            if (GUI.Button(new Rect(30, 40, 320, 80), "Connect"))
            {
                // first step : we need to create the WebSocket
                //      You can use a websocketURL (ws://XXXXXXX or wss:XXXXXXXX)
                //		You need to give a MonoBehaviour object who will receive events
                //		This object must extend WebSocketUnityDelegate interface
                //      For example : TestWebSocketController inherits of WebSocketUnityDelegate
                //		You can create your own websocket server
                //		for example : https://github.com/willryan/em-websocket
                // Warning : some libraries (for example on ios) don't support reconnection
                //         so you need to create a new websocketunity before each connect (destroy your object when receive a disconnect or an error)
                //webSocket = new WebSocketUnity("ws://echo.websocket.org", this); // <= public server
                webSocket = new WebSocketUnity("ws://" + address, this);                 // <= local server

                // Second Step : we open the connection
                webSocket.Open();
            }
        }
        else
        {
            // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
            if (GUI.Button(new Rect(30, 40, 320, 80), "Disconnect"))
            {
                // Third Step : we need to close the connection when we finish
                webSocket.Close();
            }

            if (GUI.Button(new Rect(30, 130, 320, 80), "Send message"))
            {
                // Fourth Step : we can send message
                //    In this sample, we are waiting a "echo" message from the server
                //    when we will receive this message, we will be able to change the display
                webSocket.Send("Hello World");
                sendingMessage = true;
            }
            if (GUI.Button(new Rect(30, 220, 320, 80), "Send data"))
            {
                // Fifth Step : we can send Data
                //    In this sample, we are waiting a "echo" message from the server
                //    when we will receive this message, we will be able to change the display
                // You need a server which manages bytes message

                int    test1 = 42;
                int    test2 = 33;
                byte[] data  = new byte[8];

                byte[] testB1 = System.BitConverter.GetBytes(test1);
                byte[] testB2 = System.BitConverter.GetBytes(test2);

                testB1.CopyTo(data, 0);
                testB2.CopyTo(data, 4);

                webSocket.Send(data);
                sendingMessage = true;
            }

            if (sendingMessage)
            {
                GUI.Label(new Rect(30, 350, 400, 60), "Waiting answer from server...");
            }
            else if (receivedMessage)
            {
                GUI.Label(new Rect(30, 350, 400, 60), "Server sent an echo answer !");
                DisplayMessage(_message);
            }
        }
    }