Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.AspNetCore.SignalR.Client;
- using Newtonsoft.Json;
- using UnityEngine;
- using Task = System.Threading.Tasks.Task;
- public class SocketServerCommunication : MonoBehaviour
- {
- public static Action<Guid, BetData> OnConfirmedBet;
- public static Action<decimal> OnConfirmedCashOut;
- public static Action<DateTime> OnReceivedNextGameDate;
- public static Action<decimal> OnReceivedEndMultiplayer;
- public static Action<decimal> OnReceivedCurrentMultiplayer;
- public static SocketServerCommunication Instance;
- private const string SERVER_URI = "https://api.qoomonquest.com/hubs/game";
- private HubConnection connection;
- private string authKey;
- private Action<bool> initCallBack;
- private void Awake()
- {
- if (Instance==null)
- {
- Instance = this;
- }
- else
- {
- Destroy(gameObject);
- }
- }
- public void SetAuthToken(string _authKey)
- {
- authKey = _authKey;
- }
- public async void Init(Action<bool> _callBack)
- {
- initCallBack = _callBack;
- if (connection is { State: HubConnectionState.Connected })
- {
- initCallBack?.Invoke(true);
- return;
- }
- connection = new HubConnectionBuilder()
- .WithUrl(SERVER_URI
- , _options =>
- {
- _options.AccessTokenProvider = () => Task.FromResult(authKey);
- _options.SkipNegotiation = true;
- _options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
- }
- )
- .WithAutomaticReconnect()
- .Build();
- connection.On<decimal>(nameof(ReceiveCurrentMultiplier),ReceiveCurrentMultiplier);
- connection.On<decimal>(nameof(ReceiveEndMultiplier),ReceiveEndMultiplier);
- connection.On<DateTime>(nameof(ReceiveNextGameStart),ReceiveNextGameStart);
- connection.On<decimal>(nameof(ReceiveConfirmCashOut),ReceiveConfirmCashOut);
- connection.On<Guid, BetData>(nameof(ReceiveConfirmBet),ReceiveConfirmBet);
- connection.On<string>(nameof(ReceiveError),ReceiveError);
- try
- {
- await connection.StartAsync();
- initCallBack?.Invoke(true);
- Debug.Log("Finished with connecting to the server");
- }
- catch (Exception _error)
- {
- Debug.Log($"Trying to connect with: {SERVER_URI} , authToken: {authKey}\nFailed with error: {_error}");
- initCallBack?.Invoke(false);
- }
- }
- #region Receive messages
- private void ReceiveCurrentMultiplier(decimal _multiplayer)
- {
- Debug.Log("Received current multiplayer: "+_multiplayer);
- OnReceivedCurrentMultiplayer?.Invoke(_multiplayer);
- }
- private void ReceiveEndMultiplier(decimal _multiplayer)
- {
- Debug.Log("Received end multiplayer: "+_multiplayer);
- OnReceivedEndMultiplayer?.Invoke(_multiplayer);
- }
- private void ReceiveNextGameStart(DateTime _gameStartAtUtc)
- {
- Debug.Log("Received next game start: "+_gameStartAtUtc);
- OnReceivedNextGameDate?.Invoke(_gameStartAtUtc);
- }
- private void ReceiveConfirmCashOut(decimal _multiplayer)
- {
- Debug.Log("Confirmed cash out: "+_multiplayer);
- OnConfirmedCashOut?.Invoke(_multiplayer);
- }
- private void ReceiveConfirmBet(Guid _gameId, BetData _betData)
- {
- Debug.Log("received confirm bet: "+JsonConvert.SerializeObject(_betData));
- OnConfirmedBet?.Invoke(_gameId,_betData);
- }
- private void ReceiveError(string _error)
- {
- Debug.LogError("Received error from server: "+_error);
- }
- #endregion
- #region Send messages
- public void CashOut(Guid _guid)
- {
- connection.SendAsync("CashOut", _guid);
- }
- public void PlaceBet(BetData _bet)
- {
- connection.SendAsync("PlaceBet", _bet);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement