Core logic ported from TS: HttpClientFactory, Environment and WebSocketHandler.
This commit is contained in:
@@ -4,4 +4,9 @@
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
25
Chtn.CSharp.SDK.sln
Normal file
25
Chtn.CSharp.SDK.sln
Normal file
@@ -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
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
15
Core/EnvironmentConfig.cs
Normal file
15
Core/EnvironmentConfig.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
19
Core/HttpClientFactory.cs
Normal file
19
Core/HttpClientFactory.cs
Normal file
@@ -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)
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Core/WebSocketHandler.cs
Normal file
56
Core/WebSocketHandler.cs
Normal file
@@ -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<dynamic>(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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user