123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #if !BESTHTTP_DISABLE_WEBSOCKET
- using System;
- using UnityEngine;
- using BestHTTP;
- using BestHTTP.WebSocket;
- using BestHTTP.Examples;
- using System.Text;
- public class WebSocketSample : MonoBehaviour
- {
- #region Private Fields
-
-
-
- string address = "wss://rtc.shadowcreator.com:3300";
-
-
-
- string msgToSend = "Hello World!";
-
-
-
- string Text = string.Empty;
-
-
-
- WebSocket webSocket;
-
-
-
- Vector2 scrollPos;
- #endregion
- #region Unity Events
- void OnDestroy()
- {
- if (webSocket != null)
- webSocket.Close();
- }
- void OnGUI()
- {
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
- scrollPos = GUILayout.BeginScrollView(scrollPos);
- GUILayout.Label(Text);
- GUILayout.EndScrollView();
- GUILayout.Space(5);
- GUILayout.FlexibleSpace();
- address = GUILayout.TextField(address);
- if (webSocket == null && GUILayout.Button("Open Web Socket"))
- {
-
- webSocket = new WebSocket(new Uri(address));
- #if !UNITY_WEBGL
- webSocket.StartPingThread = true;
- #if !BESTHTTP_DISABLE_PROXY
- if (HTTPManager.Proxy != null)
- webSocket.InternalRequest.Proxy = new HTTPProxy(HTTPManager.Proxy.Address, HTTPManager.Proxy.Credentials, false);
- #endif
- #endif
-
- webSocket.OnOpen += OnOpen;
- webSocket.OnMessage += OnMessageReceived;
- webSocket.OnBinary += OnBinaryReceived;
- webSocket.OnClosed += OnClosed;
- webSocket.OnError += OnError;
-
- webSocket.Open();
- Text += "Opening Web Socket...\n";
- }
- if (webSocket != null && webSocket.IsOpen)
- {
- GUILayout.Space(10);
- GUILayout.BeginHorizontal();
- msgToSend = GUILayout.TextField(msgToSend);
- if (GUILayout.Button("Send", GUILayout.MaxWidth(70)))
- {
- Text += "Sending message...\n";
-
- webSocket.Send(msgToSend);
- }
- GUILayout.EndHorizontal();
- GUILayout.Space(10);
- if (GUILayout.Button("Close"))
- {
-
- webSocket.Close(1000, "Bye!");
- }
- }
- });
- }
- #endregion
- #region WebSocket Event Handlers
-
-
-
- void OnOpen(WebSocket ws)
- {
- Text += string.Format("-WebSocket Open!\n");
- }
-
-
-
- void OnMessageReceived(WebSocket ws, string message)
- {
- Text += string.Format("-Message received: {0}\n", message);
- }
-
-
-
- void OnBinaryReceived(WebSocket webSocket, byte[] data)
- {
- Debug.Log (string.Format("-Binary received: {0}\n", data.Length));
- }
-
-
-
- void OnClosed(WebSocket ws, UInt16 code, string message)
- {
- Text += string.Format("-WebSocket closed! Code: {0} Message: {1}\n", code, message);
- webSocket = null;
- }
-
-
-
- void OnError(WebSocket ws, Exception ex)
- {
- string errorMsg = string.Empty;
- #if !UNITY_WEBGL || UNITY_EDITOR
- if (ws.InternalRequest.Response != null)
- errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
- #endif
- Text += string.Format("-An error occured: {0}\n", (ex != null ? ex.Message : "Unknown Error " + errorMsg));
- webSocket = null;
- }
- #endregion
- }
- #endif
|