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/");
- /// <summary>
- /// Possible message types that the client can send to the server
- /// </summary>
- enum MessageTypes
- {
- Send, // 0
- Broadcast, // 1
- Join, // 2
- PrivateMessage, // 3
- AddToGroup, // 4
- RemoveFromGroup, // 5
- SendToGroup, // 6
- BroadcastExceptMe, // 7
- }
- #region Private Fields
- /// <summary>
- /// Reference to the SignalR Connection
- /// </summary>
- Connection signalRConnection;
- // Input strings
- 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)
- // SaveLocal a "user" cookie if we previously used the 'Enter Name' button.
- // The server will set this username to the new connection.
- if (PlayerPrefs.HasKey("userName"))
- CookieJar.Set(URI, new Cookie("user", PlayerPrefs.GetString("userName")));
- #endif
- signalRConnection = new Connection(URI);
- // to serialize the Message class, set a more advanced json encoder
- signalRConnection.JsonEncoder = new BestHTTP.SignalR.JsonEncoders.LitJsonEncoder();
- // set up event handlers
- signalRConnection.OnStateChanged += signalRConnection_OnStateChanged;
- signalRConnection.OnNonHubMessage += signalRConnection_OnGeneralMessage;
- // Start to connect to the server.
- signalRConnection.Open();
- }
- /// <summary>
- /// Draw the gui.
- /// Get input strings.
- /// Handle function calls.
- /// </summary>
- 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);
- // Draw the messages
- GUILayout.Label("Messages");
- GUILayout.BeginHorizontal();
- GUILayout.Space(20);
- messages.Draw(Screen.width - 20, 0);
- GUILayout.EndHorizontal();
- GUILayout.EndVertical();
- });
- }
- void OnDestroy()
- {
- // Close the connection when the sample is closed
- signalRConnection.Close();
- }
- #endregion
- #region SignalR Events
- /// <summary>
- /// Handle non-hub messages
- /// </summary>
- void signalRConnection_OnGeneralMessage(Connection manager, object data)
- {
- // For now, just create a Json string from the sent data again
- string reencoded = BestHTTP.JSON.Json.Encode(data);
- // and display it
- messages.Add("[Server Message] " + reencoded);
- }
- void signalRConnection_OnStateChanged(Connection manager, ConnectionStates oldState, ConnectionStates newState)
- {
- // display state changes
- messages.Add(string.Format("[State Change] {0} => {1}", oldState.ToString(), newState.ToString()));
- }
- #endregion
- #region To EveryBody Functions
- /// <summary>
- /// Broadcast a message to all connected clients
- /// </summary>
- private void Broadcast(string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.Broadcast, Value = text });
- }
- /// <summary>
- /// Broadcast a message to all connected clients, except this client
- /// </summary>
- private void BroadcastExceptMe(string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.BroadcastExceptMe, Value = text });
- }
- /// <summary>
- /// SaveLocal a name for this connection.
- /// </summary>
- private void EnterName(string name)
- {
- signalRConnection.Send(new { Type = MessageTypes.Join, Value = name });
- }
- /// <summary>
- /// Join to a group
- /// </summary>
- private void JoinGroup(string groupName)
- {
- signalRConnection.Send(new { Type = MessageTypes.AddToGroup, Value = groupName });
- }
- /// <summary>
- /// Leave a group
- /// </summary>
- private void LeaveGroup(string groupName)
- {
- signalRConnection.Send(new { Type = MessageTypes.RemoveFromGroup, Value = groupName });
- }
- #endregion
- #region To Me Functions
- /// <summary>
- /// Send a message to the very same client through the server
- /// </summary>
- void SendToMe(string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.Send, Value = text });
- }
- #endregion
- #region Private Message Functions
- /// <summary>
- /// Send a private message to a user
- /// </summary>
- void SendToUser(string userOrGroupName, string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.PrivateMessage, Value = string.Format("{0}|{1}", userOrGroupName, text) });
- }
- /// <summary>
- /// Send a message to a group
- /// </summary>
- void SendToGroup(string userOrGroupName, string text)
- {
- signalRConnection.Send(new { Type = MessageTypes.SendToGroup, Value = string.Format("{0}|{1}", userOrGroupName, text) });
- }
- #endregion
- }
- #endif
|