123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- #if !BESTHTTP_DISABLE_SIGNALR
- using System;
- using UnityEngine;
- using BestHTTP.SignalR;
- using BestHTTP.Examples;
- #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
- using BestHTTP.Cookies;
- #endif
- public sealed class ConnectionAPISample : MonoBehaviour
- {
- readonly Uri URI = new Uri("https://besthttpsignalr.azurewebsites.net/raw-connection/");
-
-
-
- enum MessageTypes
- {
- Send,
- Broadcast,
- Join,
- PrivateMessage,
- AddToGroup,
- RemoveFromGroup,
- SendToGroup,
- BroadcastExceptMe,
- }
- #region Private Fields
-
-
-
- Connection signalRConnection;
-
- string ToEveryBodyText = string.Empty;
- string ToMeText = string.Empty;
- string PrivateMessageText = string.Empty;
- string PrivateMessageUserOrGroupName = string.Empty;
- GUIMessageList messages = new GUIMessageList();
- #endregion
- #region Unity Events
- void Start()
- {
- #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
-
-
- if (PlayerPrefs.HasKey("userName"))
- CookieJar.Set(URI, new Cookie("user", PlayerPrefs.GetString("userName")));
- #endif
- signalRConnection = new Connection(URI);
-
- signalRConnection.JsonEncoder = new BestHTTP.SignalR.JsonEncoders.LitJsonEncoder();
-
- signalRConnection.OnStateChanged += signalRConnection_OnStateChanged;
- signalRConnection.OnNonHubMessage += signalRConnection_OnGeneralMessage;
-
- signalRConnection.Open();
- }
-
-
-
-
-
- void OnGUI()
- {
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
- GUILayout.BeginVertical();
- #region To Everybody
- GUILayout.Label("To Everybody");
- GUILayout.BeginHorizontal();
- ToEveryBodyText = GUILayout.TextField(ToEveryBodyText, GUILayout.MinWidth(100));
- if (GUILayout.Button("Broadcast"))
- Broadcast(ToEveryBodyText);
- if (GUILayout.Button("Broadcast (All Except Me)"))
- BroadcastExceptMe(ToEveryBodyText);
- if (GUILayout.Button("Enter Name"))
- EnterName(ToEveryBodyText);
- if (GUILayout.Button("Join Group"))
- JoinGroup(ToEveryBodyText);
- if (GUILayout.Button("Leave Group"))
- LeaveGroup(ToEveryBodyText);
- GUILayout.EndHorizontal();
- #endregion
- #region To Me
- GUILayout.Label("To Me");
- GUILayout.BeginHorizontal();
- ToMeText = GUILayout.TextField(ToMeText, GUILayout.MinWidth(100));
- if (GUILayout.Button("Send to me"))
- SendToMe(ToMeText);
- GUILayout.EndHorizontal();
- #endregion
- #region Private Message
- GUILayout.Label("Private Message");
- GUILayout.BeginHorizontal();
- GUILayout.Label("Message:");
- PrivateMessageText = GUILayout.TextField(PrivateMessageText, GUILayout.MinWidth(100));
- GUILayout.Label("User or Group name:");
- PrivateMessageUserOrGroupName = GUILayout.TextField(PrivateMessageUserOrGroupName, GUILayout.MinWidth(100));
- if (GUILayout.Button("Send to user"))
- SendToUser(PrivateMessageUserOrGroupName, PrivateMessageText);
- if (GUILayout.Button("Send to group"))
- SendToGroup(PrivateMessageUserOrGroupName, PrivateMessageText);
- GUILayout.EndHorizontal();
- #endregion
- GUILayout.Space(20);
- if (signalRConnection.State == ConnectionStates.Closed)
- {
- if (GUILayout.Button("Start Connection"))
- signalRConnection.Open();
- }
- else if (GUILayout.Button("Stop Connection"))
- signalRConnection.Close();
- GUILayout.Space(20);
-
- GUILayout.Label("Messages");
- GUILayout.BeginHorizontal();
- GUILayout.Space(20);
- messages.Draw(Screen.width - 20, 0);
- GUILayout.EndHorizontal();
- GUILayout.EndVertical();
- });
- }
- void OnDestroy()
- {
-
- signalRConnection.Close();
- }
- #endregion
- #region SignalR Events
-
-
-
- void signalRConnection_OnGeneralMessage(Connection manager, object data)
- {
-
- string reencoded = BestHTTP.JSON.Json.Encode(data);
-
- messages.Add("[Server Message] " + reencoded);
- }
- void signalRConnection_OnStateChanged(Connection manager, ConnectionStates oldState, ConnectionStates newState)
- {
-
- messages.Add(string.Format("[State Change] {0} => {1}", oldState.ToString(), newState.ToString()));
- }
- #endregion
- #region To EveryBody Functions
-
-
-
- private void Broadcast(string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.Broadcast, Value = text });
- }
-
-
-
- private void BroadcastExceptMe(string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.BroadcastExceptMe, Value = text });
- }
-
-
-
- private void EnterName(string name)
- {
- signalRConnection.Send(new { Type = MessageTypes.Join, Value = name });
- }
-
-
-
- private void JoinGroup(string groupName)
- {
- signalRConnection.Send(new { Type = MessageTypes.AddToGroup, Value = groupName });
- }
-
-
-
- private void LeaveGroup(string groupName)
- {
- signalRConnection.Send(new { Type = MessageTypes.RemoveFromGroup, Value = groupName });
- }
- #endregion
- #region To Me Functions
-
-
-
- void SendToMe(string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.Send, Value = text });
- }
- #endregion
- #region Private Message Functions
-
-
-
- void SendToUser(string userOrGroupName, string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.PrivateMessage, Value = string.Format("{0}|{1}", userOrGroupName, text) });
- }
-
-
-
- void SendToGroup(string userOrGroupName, string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.SendToGroup, Value = string.Format("{0}|{1}", userOrGroupName, text) });
- }
- #endregion
- }
- #endif
|