77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.IO.Pipelines;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Protocols;
|
|
using Microsoft.AspNetCore.Sockets;
|
|
using Microsoft.AspNetCore.Sockets.Features;
|
|
|
|
namespace SocketsSample.EndPoints
|
|
{
|
|
public class MessagesEndPoint : EndPoint
|
|
{
|
|
public ConnectionList Connections { get; } = new ConnectionList();
|
|
|
|
public override async Task OnConnectedAsync(ConnectionContext connection)
|
|
{
|
|
Connections.Add(connection);
|
|
|
|
await Broadcast($"{connection.ConnectionId} connected ({connection.Features.Get<IConnectionMetadataFeature>().Metadata[ConnectionMetadataNames.Transport]})");
|
|
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
var result = await connection.Transport.Input.ReadAsync();
|
|
var buffer = result.Buffer;
|
|
|
|
try
|
|
{
|
|
if (!buffer.IsEmpty)
|
|
{
|
|
// We can avoid the copy here but we'll deal with that later
|
|
var text = Encoding.UTF8.GetString(buffer.ToArray());
|
|
text = $"{connection.ConnectionId}: {text}";
|
|
await Broadcast(Encoding.UTF8.GetBytes(text));
|
|
}
|
|
else if (result.IsCompleted)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
connection.Transport.Input.AdvanceTo(buffer.End);
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
Connections.Remove(connection);
|
|
|
|
await Broadcast($"{connection.ConnectionId} disconnected ({connection.Features.Get<IConnectionMetadataFeature>().Metadata[ConnectionMetadataNames.Transport]})");
|
|
}
|
|
}
|
|
|
|
private Task Broadcast(string text)
|
|
{
|
|
return Broadcast(Encoding.UTF8.GetBytes(text));
|
|
}
|
|
|
|
private Task Broadcast(byte[] payload)
|
|
{
|
|
var tasks = new List<Task>(Connections.Count);
|
|
|
|
foreach (var c in Connections)
|
|
{
|
|
tasks.Add(c.Transport.Output.WriteAsync(payload));
|
|
}
|
|
|
|
return Task.WhenAll(tasks);
|
|
}
|
|
}
|
|
}
|