You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
69 lines
1.9 KiB
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Net.WebSockets;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Shentun.Peis.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class AiMessageWsController : ControllerBase
|
|
{
|
|
|
|
[HttpGet]
|
|
public IAsyncEnumerable<string> GetData()
|
|
{
|
|
return GenerateDataAsync();
|
|
}
|
|
|
|
private async IAsyncEnumerable<string> GenerateDataAsync()
|
|
{
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
await Task.Delay(100); // 模拟延迟
|
|
yield return $"Data {i}";
|
|
}
|
|
}
|
|
|
|
//public async Task HandleWebSocket()
|
|
//{
|
|
// if (HttpContext.WebSockets.IsWebSocketRequest)
|
|
// {
|
|
// using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
|
// await ProcessDeepSeekStream(webSocket);
|
|
// }
|
|
// else
|
|
// {
|
|
// HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
// }
|
|
//}
|
|
|
|
|
|
|
|
//private async Task ProcessDeepSeekStream(WebSocket webSocket)
|
|
//{
|
|
// // 调用deepseek API
|
|
// var stream = await GetDeepSeekStream();
|
|
|
|
// foreach (var chunk in stream)
|
|
// {
|
|
// var buffer = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(chunk));
|
|
// await webSocket.SendAsync(
|
|
// new ArraySegment<byte>(buffer),
|
|
// WebSocketMessageType.Text,
|
|
// true,
|
|
// CancellationToken.None);
|
|
// }
|
|
|
|
// await webSocket.CloseAsync(
|
|
// WebSocketCloseStatus.NormalClosure,
|
|
// "Stream completed",
|
|
// CancellationToken.None);
|
|
//}
|
|
|
|
}
|
|
}
|