Advertisement
Learning000001

Untitled

Nov 22nd, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. using System;
  2. using Microsoft.AspNetCore.SignalR.Client;
  3. using Newtonsoft.Json;
  4. using UnityEngine;
  5. using Task = System.Threading.Tasks.Task;
  6.  
  7. public class SocketServerCommunication : MonoBehaviour
  8. {
  9. public static Action<Guid, BetData> OnConfirmedBet;
  10. public static Action<decimal> OnConfirmedCashOut;
  11. public static Action<DateTime> OnReceivedNextGameDate;
  12. public static Action<decimal> OnReceivedEndMultiplayer;
  13. public static Action<decimal> OnReceivedCurrentMultiplayer;
  14.  
  15. public static SocketServerCommunication Instance;
  16. private const string SERVER_URI = "https://api.qoomonquest.com/hubs/game";
  17. private HubConnection connection;
  18. private string authKey;
  19. private Action<bool> initCallBack;
  20.  
  21. private void Awake()
  22. {
  23. if (Instance==null)
  24. {
  25. Instance = this;
  26. }
  27. else
  28. {
  29. Destroy(gameObject);
  30. }
  31. }
  32.  
  33. public void SetAuthToken(string _authKey)
  34. {
  35. authKey = _authKey;
  36. }
  37.  
  38. public async void Init(Action<bool> _callBack)
  39. {
  40. initCallBack = _callBack;
  41. if (connection is { State: HubConnectionState.Connected })
  42. {
  43. initCallBack?.Invoke(true);
  44. return;
  45. }
  46.  
  47. connection = new HubConnectionBuilder()
  48. .WithUrl(SERVER_URI
  49. , _options =>
  50. {
  51. _options.AccessTokenProvider = () => Task.FromResult(authKey);
  52. _options.SkipNegotiation = true;
  53. _options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
  54. }
  55. )
  56. .WithAutomaticReconnect()
  57. .Build();
  58.  
  59. connection.On<decimal>(nameof(ReceiveCurrentMultiplier),ReceiveCurrentMultiplier);
  60. connection.On<decimal>(nameof(ReceiveEndMultiplier),ReceiveEndMultiplier);
  61. connection.On<DateTime>(nameof(ReceiveNextGameStart),ReceiveNextGameStart);
  62. connection.On<decimal>(nameof(ReceiveConfirmCashOut),ReceiveConfirmCashOut);
  63. connection.On<Guid, BetData>(nameof(ReceiveConfirmBet),ReceiveConfirmBet);
  64. connection.On<string>(nameof(ReceiveError),ReceiveError);
  65.  
  66. try
  67. {
  68. await connection.StartAsync();
  69. initCallBack?.Invoke(true);
  70. Debug.Log("Finished with connecting to the server");
  71. }
  72. catch (Exception _error)
  73. {
  74. Debug.Log($"Trying to connect with: {SERVER_URI} , authToken: {authKey}\nFailed with error: {_error}");
  75. initCallBack?.Invoke(false);
  76. }
  77. }
  78.  
  79. #region Receive messages
  80.  
  81. private void ReceiveCurrentMultiplier(decimal _multiplayer)
  82. {
  83. Debug.Log("Received current multiplayer: "+_multiplayer);
  84. OnReceivedCurrentMultiplayer?.Invoke(_multiplayer);
  85. }
  86.  
  87. private void ReceiveEndMultiplier(decimal _multiplayer)
  88. {
  89. Debug.Log("Received end multiplayer: "+_multiplayer);
  90. OnReceivedEndMultiplayer?.Invoke(_multiplayer);
  91. }
  92.  
  93. private void ReceiveNextGameStart(DateTime _gameStartAtUtc)
  94. {
  95. Debug.Log("Received next game start: "+_gameStartAtUtc);
  96. OnReceivedNextGameDate?.Invoke(_gameStartAtUtc);
  97. }
  98.  
  99. private void ReceiveConfirmCashOut(decimal _multiplayer)
  100. {
  101. Debug.Log("Confirmed cash out: "+_multiplayer);
  102. OnConfirmedCashOut?.Invoke(_multiplayer);
  103. }
  104. private void ReceiveConfirmBet(Guid _gameId, BetData _betData)
  105. {
  106. Debug.Log("received confirm bet: "+JsonConvert.SerializeObject(_betData));
  107. OnConfirmedBet?.Invoke(_gameId,_betData);
  108. }
  109.  
  110. private void ReceiveError(string _error)
  111. {
  112. Debug.LogError("Received error from server: "+_error);
  113. }
  114.  
  115. #endregion
  116.  
  117. #region Send messages
  118.  
  119. public void CashOut(Guid _guid)
  120. {
  121. connection.SendAsync("CashOut", _guid);
  122. }
  123.  
  124. public void PlaceBet(BetData _bet)
  125. {
  126. connection.SendAsync("PlaceBet", _bet);
  127. }
  128.  
  129. #endregion
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement