67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using Bitget_Server.Domain.Enums;
|
|
using Bitget_Server.Infrastructure.Bitget;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Bitget_Server.Api.Controllers;
|
|
|
|
/// <summary>
|
|
/// 기존 WinForms 의 BitGetWebSocektStart / closeWebSocket 버튼 REST 화.
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/websocket")]
|
|
[Produces("application/json")]
|
|
public class WebSocketController : ControllerBase
|
|
{
|
|
private readonly BitgetWebSocketClient _ws;
|
|
private readonly ILogger<WebSocketController> _logger;
|
|
|
|
public WebSocketController(BitgetWebSocketClient ws, ILogger<WebSocketController> logger)
|
|
{
|
|
_ws = ws;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>현재 WebSocket 연결 상태.</summary>
|
|
[HttpGet("status")]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
public IActionResult GetStatus() => Ok(new { connected = _ws.IsConnected });
|
|
|
|
/// <summary>
|
|
/// WebSocket 연결 시작. subscribeArgs 는 Bitget 공식 포맷의 args 리스트.
|
|
/// 예: [{"instType":"USDT-FUTURES","channel":"ticker","instId":"BTCUSDT"}]
|
|
/// </summary>
|
|
[HttpPost("start")]
|
|
[ProducesResponseType(StatusCodes.Status202Accepted)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> Start(
|
|
[FromQuery] BitgetWebSocketType type,
|
|
[FromBody] IEnumerable<object> subscribeArgs,
|
|
CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var dic = new Dictionary<string, object>
|
|
{
|
|
{ "op", "subscribe" },
|
|
{ "args", subscribeArgs ?? Array.Empty<object>() }
|
|
};
|
|
await _ws.StartAsync(type, dic, ct);
|
|
return Accepted();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "WebSocket Start 실패");
|
|
return Problem(detail: ex.Message, statusCode: StatusCodes.Status500InternalServerError, title: "WebSocket start failed");
|
|
}
|
|
}
|
|
|
|
/// <summary>WebSocket 연결 종료.</summary>
|
|
[HttpPost("stop")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
public async Task<IActionResult> Stop(CancellationToken ct)
|
|
{
|
|
await _ws.StopAsync(ct);
|
|
return NoContent();
|
|
}
|
|
}
|