diff --git a/Chtn.CSharp.SDK.csproj b/Chtn.CSharp.SDK.csproj index dbdcea4..07e569b 100644 --- a/Chtn.CSharp.SDK.csproj +++ b/Chtn.CSharp.SDK.csproj @@ -4,4 +4,9 @@ netstandard2.0 + + + + + diff --git a/Chtn.CSharp.SDK.sln b/Chtn.CSharp.SDK.sln new file mode 100644 index 0000000..be76aee --- /dev/null +++ b/Chtn.CSharp.SDK.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 18 +VisualStudioVersion = 18.4.11626.88 stable +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chtn.CSharp.SDK", "Chtn.CSharp.SDK.csproj", "{CE1ED0A8-C1D4-A4F2-A1F7-46A18938BB73}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CE1ED0A8-C1D4-A4F2-A1F7-46A18938BB73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CE1ED0A8-C1D4-A4F2-A1F7-46A18938BB73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CE1ED0A8-C1D4-A4F2-A1F7-46A18938BB73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CE1ED0A8-C1D4-A4F2-A1F7-46A18938BB73}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DC9BA064-42F5-45F7-BC3F-89938B9904F8} + EndGlobalSection +EndGlobal diff --git a/Core/ChateniumClient.cs b/Core/ChateniumClient.cs index 0d60ea3..9d4d2c6 100644 --- a/Core/ChateniumClient.cs +++ b/Core/ChateniumClient.cs @@ -1,6 +1,9 @@ -using Chtn.CSharpSDK.Interfaces; -using System.Net.Http; -using System; +using System; +using System.Globalization; +using System.Net.WebSockets; +using System.Threading.Tasks; +using Chtn.CSharpSDK.Interfaces; + namespace Chtn.CSharpSDK.Core { @@ -8,15 +11,17 @@ namespace Chtn.CSharpSDK.Core { private readonly IKeyringAPI _keyring; private readonly IDatabaseAPI _database; - private readonly HttpClient _httpClient; + private readonly WebSocketHandler _wsHandler; public ChateniumClient(IKeyringAPI keyring, IDatabaseAPI database) { _keyring = keyring ?? throw new ArgumentNullException(nameof(keyring)); _database = database ?? throw new ArgumentNullException(nameof(database)); - - _httpClient = new HttpClient(); - _httpClient.BaseAddress = new Uri("https://api.chatenium.hu"); + _wsHandler = WebSocketHandler.GetInstance(); + } + public async Task ConnectAsync(string userId, string token) + { + await _wsHandler.ConnectAsync(userId, token); } public void initialize() { diff --git a/Core/EnvironmentConfig.cs b/Core/EnvironmentConfig.cs new file mode 100644 index 0000000..25bd23c --- /dev/null +++ b/Core/EnvironmentConfig.cs @@ -0,0 +1,15 @@ +namespace Chtn.CSharp.SDK.Core +{ + public class SDKConfig + { + public string ApiUrl { get; set; } = "https://api.chatenium.hu"; + public string CdnUrl { get; set; } = "https://cdn.chatenium.hu"; + public string WsUrl { get; set; } = "wss://api.chatenium.hu"; + } + + public static class EnvironmentConfig + { + private static SDKConfig _currentConfig = new SDKConfig(); + public static SDKConfig Get() => _currentConfig; + } +} \ No newline at end of file diff --git a/Core/HttpClientFactory.cs b/Core/HttpClientFactory.cs new file mode 100644 index 0000000..bcebd3b --- /dev/null +++ b/Core/HttpClientFactory.cs @@ -0,0 +1,19 @@ +using System; +using System.Net.Http; + +namespace Chtn.CSharp.SDK.Core +{ + public static class HttpClientFactory + { + public static HttpClient CreateClient(bool isCdn = false) + { + var env = EnvironmentConfig.Get(); + return new HttpClient + { + BaseAddress = new Uri(isCdn ? env.CdnUrl : env.ApiUrl), + Timeout = TimeSpan.FromSeconds(5) + }; + + } + } +} \ No newline at end of file diff --git a/Core/WebSocketHandler.cs b/Core/WebSocketHandler.cs new file mode 100644 index 0000000..e425405 --- /dev/null +++ b/Core/WebSocketHandler.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using Chtn.CSharpSDK.Interfaces; +using Chtn.CSharp.SDK.Core; + +namespace Chtn.CSharpSDK.Core +{ + public class WebSocketHandler + { + private static WebSocketHandler _instance; + private ClientWebSocket _webSocket; + private string _connectionId; + + public static WebSocketHandler GetInstance() + { + if (_instance == null) + { + _instance = new WebSocketHandler(); + } + return _instance; + } + + public async Task ConnectAsync(string userId, string token) + { + var client = HttpClientFactory.CreateClient(); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token); + + try + { + var response = await client.PostAsync("v2/ws/makeToken", + new StringContent(JsonConvert.SerializeObject(new { userid = userId }), Encoding.UTF8, "application/json")); + + var content = await response.Content.ReadAsStringAsync(); + var authData = JsonConvert.DeserializeObject(content); + + string wsUrl = $"{EnvironmentConfig.Get().WsUrl}/vs/ws?userid={userId}&access_token={authData.token}"; + + _webSocket = new ClientWebSocket(); + await _webSocket.ConnectAsync(new Uri(wsUrl), CancellationToken.None); + + Console.WriteLine("Connected to websocket successfully"); + } + catch (Exception ex) + { + Console.WriteLine($"Websocket hiba: {ex.Message}"); + } + } + } +} \ No newline at end of file