public class MyHub : Hub
{
public async Task SendMessage(string message)
{
// İstemcilere mesaj gönderme
await Clients.All.SendAsync("ReceiveMessage", message);
}
}var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSignalR();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapHub<MyHub>("/myhub");
app.Run(); void startSignalR() async {
hubConnection = HubConnectionBuilder()
.withUrl('https://yapayzeka.uygunstock.com/myhub') // SignalR hub URL'si
.build();
hubConnection.onclose((error) {
setState(() {
connectionStatus = 'Bağlantı Kapandı: ${error}';
});
});
hubConnection.on('ReceiveMessage', (arguments) {
final message = arguments?[0] as String;
print('Gelen Mesaj: $message');
setState(() {
connectionStatus = 'Gelen Mesaj: $message';
});
});
try {
await hubConnection.start();
setState(() {
connectionStatus = 'Bağlantı Başarılı';
});
// İsteğe bağlı olarak bir mesaj gönderebilirsiniz
await hubConnection.invoke('SendMessage', args: ['Merhaba, Flutter!']);
} catch (e) {
setState(() {
connectionStatus = 'Bağlantı Hatası: $e';
});
}
}