Core logic ported from TS: HttpClientFactory, Environment and WebSocketHandler.

This commit is contained in:
2026-04-09 09:41:16 +02:00
parent 63e52336e2
commit 3ee4edac6c
6 changed files with 132 additions and 7 deletions

View File

@@ -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
View 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
View 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
View 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}");
}
}
}
}