#if !BESTHTTP_DISABLE_SIGNALR_CORE using System; using UnityEngine; using BestHTTP.SignalRCore; using BestHTTP.SignalRCore.Encoders; using UnityEngine.UI; using BestHTTP.Examples.Helpers; namespace BestHTTP.Examples { public enum MyEnum : int { None, One, Two } public sealed class Metadata { public string strData; public int intData; public MyEnum myEnum; } // Server side of this example can be found here: // https://github.com/Benedicht/BestHTTP_DemoSite/blob/master/BestHTTP_DemoSite/Hubs/TestHub.cs public class TestHubSample : BestHTTP.Examples.Helpers.SampleBase { #pragma warning disable 0649 [SerializeField] private string _path = "/TestHub"; [SerializeField] private ScrollRect _scrollRect; [SerializeField] private RectTransform _contentRoot; [SerializeField] private TextListItem _listItemPrefab; [SerializeField] private int _maxListItemEntries = 100; [SerializeField] private Button _connectButton; [SerializeField] private Button _closeButton; #pragma warning restore // Instance of the HubConnection HubConnection hub; protected override void Start() { base.Start(); SetButtons(true, false); } void OnDestroy() { if (hub != null) hub.StartClose(); } /// /// GUI button callback /// public void OnConnectButton() { #if BESTHTTP_SIGNALR_CORE_ENABLE_MESSAGEPACK_CSHARP try { MessagePack.Resolvers.StaticCompositeResolver.Instance.Register( MessagePack.Resolvers.DynamicEnumAsStringResolver.Instance, MessagePack.Unity.UnityResolver.Instance, //MessagePack.Unity.Extension.UnityBlitWithPrimitiveArrayResolver.Instance, //MessagePack.Resolvers.StandardResolver.Instance, MessagePack.Resolvers.ContractlessStandardResolver.Instance ); var options = MessagePack.MessagePackSerializerOptions.Standard.WithResolver(MessagePack.Resolvers.StaticCompositeResolver.Instance); MessagePack.MessagePackSerializer.DefaultOptions = options; } catch { } #endif IProtocol protocol = null; #if BESTHTTP_SIGNALR_CORE_ENABLE_MESSAGEPACK_CSHARP protocol = new MessagePackCSharpProtocol(); #elif BESTHTTP_SIGNALR_CORE_ENABLE_GAMEDEVWARE_MESSAGEPACK protocol = new MessagePackProtocol(); #else protocol = new JsonProtocol(new LitJsonEncoder()); #endif // Crete the HubConnection hub = new HubConnection(new Uri(base.sampleSelector.BaseURL + this._path), protocol); // Optionally add an authenticator //hub.AuthenticationProvider = new BestHTTP.SignalRCore.Authentication.HeaderAuthenticator(""); // Subscribe to hub events hub.OnConnected += Hub_OnConnected; hub.OnError += Hub_OnError; hub.OnClosed += Hub_OnClosed; hub.OnTransportEvent += (hub, transport, ev) => AddText(string.Format("Transport({0}) event: {1}", transport.TransportType, ev)); // Set up server callable functions hub.On("Send", (string arg) => AddText(string.Format("On 'Send': '{0}'", arg)).AddLeftPadding(20)); hub.On("Person", (person) => AddText(string.Format("On 'Person': '{0}'", person)).AddLeftPadding(20)); hub.On("TwoPersons", (person1, person2) => AddText(string.Format("On 'TwoPersons': '{0}', '{1}'", person1, person2)).AddLeftPadding(20)); // And finally start to connect to the server hub.StartConnect(); AddText("StartConnect called"); SetButtons(false, false); } /// /// GUI button callback /// public void OnCloseButton() { if (this.hub != null) { this.hub.StartClose(); AddText("StartClose called"); SetButtons(false, false); } } /// /// This callback is called when the plugin is connected to the server successfully. Messages can be sent to the server after this point. /// private void Hub_OnConnected(HubConnection hub) { SetButtons(false, true); AddText(string.Format("Hub Connected with {0} transport using the {1} encoder.", hub.Transport.TransportType.ToString(), hub.Protocol.Name)); hub.Send("SendMetadata", new Metadata() { intData = 123, strData = "meta data", myEnum = MyEnum.One }); // Call a server function with a string param. We expect no return value. hub.Send("Send", "my message"); // Call a parameterless function. We expect a string return value. hub.Invoke("NoParam") .OnSuccess(ret => AddText(string.Format("'NoParam' returned: '{0}'", ret)).AddLeftPadding(20)) .OnError(error => AddText(string.Format("'NoParam' error: '{0}'", error)).AddLeftPadding(20)); // Call a function on the server to add two numbers. OnSuccess will be called with the result and OnError if there's an error. hub.Invoke("Add", 10, 20) .OnSuccess(result => AddText(string.Format("'Add(10, 20)' returned: '{0}'", result)).AddLeftPadding(20)) .OnError(error => AddText(string.Format("'Add(10, 20)' error: '{0}'", error)).AddLeftPadding(20)); hub.Invoke("NullableTest", 10) .OnSuccess(result => AddText(string.Format("'NullableTest(10)' returned: '{0}'", result)).AddLeftPadding(20)) .OnError(error => AddText(string.Format("'NullableTest(10)' error: '{0}'", error)).AddLeftPadding(20)); // Call a function that will return a Person object constructed from the function's parameters. hub.Invoke("GetPerson", "Mr. Smith", 26) .OnSuccess(result => AddText(string.Format("'GetPerson(\"Mr. Smith\", 26)' returned: '{0}'", result)).AddLeftPadding(20)) .OnError(error => AddText(string.Format("'GetPerson(\"Mr. Smith\", 26)' error: '{0}'", error)).AddLeftPadding(20)); // To test errors/exceptions this call always throws an exception on the server side resulting in an OnError call. // OnError expected here! hub.Invoke("SingleResultFailure", 10, 20) .OnSuccess(result => AddText(string.Format("'SingleResultFailure(10, 20)' returned: '{0}'", result)).AddLeftPadding(20)) .OnError(error => AddText(string.Format("'SingleResultFailure(10, 20)' error: '{0}'", error)).AddLeftPadding(20)); // This call demonstrates IEnumerable<> functions, result will be the yielded numbers. hub.Invoke("Batched", 10) .OnSuccess(result => AddText(string.Format("'Batched(10)' returned items: '{0}'", result.Length)).AddLeftPadding(20)) .OnError(error => AddText(string.Format("'Batched(10)' error: '{0}'", error)).AddLeftPadding(20)); // OnItem is called for a streaming request for every items returned by the server. OnSuccess will still be called with all the items. hub.GetDownStreamController("ObservableCounter", 10, 1000) .OnItem(result => AddText(string.Format("'ObservableCounter(10, 1000)' OnItem: '{0}'", result)).AddLeftPadding(20)) .OnSuccess(result => AddText("'ObservableCounter(10, 1000)' OnSuccess.").AddLeftPadding(20)) .OnError(error => AddText(string.Format("'ObservableCounter(10, 1000)' error: '{0}'", error)).AddLeftPadding(20)); // A stream request can be cancelled any time. var controller = hub.GetDownStreamController("ChannelCounter", 10, 1000); controller.OnItem(result => AddText(string.Format("'ChannelCounter(10, 1000)' OnItem: '{0}'", result)).AddLeftPadding(20)) .OnSuccess(result => AddText("'ChannelCounter(10, 1000)' OnSuccess.").AddLeftPadding(20)) .OnError(error => AddText(string.Format("'ChannelCounter(10, 1000)' error: '{0}'", error)).AddLeftPadding(20)); // a stream can be cancelled by calling the controller's Cancel method controller.Cancel(); // This call will stream strongly typed objects hub.GetDownStreamController("GetRandomPersons", 20, 2000) .OnItem(result => AddText(string.Format("'GetRandomPersons(20, 1000)' OnItem: '{0}'", result)).AddLeftPadding(20)) .OnSuccess(result => AddText("'GetRandomPersons(20, 1000)' OnSuccess.").AddLeftPadding(20)); } /// /// This is called when the hub is closed after a StartClose() call. /// private void Hub_OnClosed(HubConnection hub) { SetButtons(true, false); AddText("Hub Closed"); } /// /// Called when an unrecoverable error happen. After this event the hub will not send or receive any messages. /// private void Hub_OnError(HubConnection hub, string error) { SetButtons(true, false); AddText(string.Format("Hub Error: {0}", error)); } private void SetButtons(bool connect, bool close) { if (this._connectButton != null) this._connectButton.interactable = connect; if (this._closeButton != null) this._closeButton.interactable = close; } private TextListItem AddText(string text) { return GUIHelper.AddText(this._listItemPrefab, this._contentRoot, text, this._maxListItemEntries, this._scrollRect); } } } #endif