Added connect and disconnect messages
This commit is contained in:
parent
1ad1120c41
commit
58d08c07d0
|
|
@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Sockets
|
||||||
{
|
{
|
||||||
public class Message
|
public class Message
|
||||||
{
|
{
|
||||||
public ReadableBuffer Payload { get; set; }
|
public byte[] Payload { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Bus
|
public class Bus
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Channels;
|
using Channels;
|
||||||
using Microsoft.AspNetCore.Sockets;
|
using Microsoft.AspNetCore.Sockets;
|
||||||
|
|
@ -13,6 +14,11 @@ namespace SocketsSample
|
||||||
|
|
||||||
public override async Task OnConnected(Connection connection)
|
public override async Task OnConnected(Connection connection)
|
||||||
{
|
{
|
||||||
|
await bus.Publish(nameof(ChatEndPoint), new Message
|
||||||
|
{
|
||||||
|
Payload = Encoding.UTF8.GetBytes($"{connection.ConnectionId} connected")
|
||||||
|
});
|
||||||
|
|
||||||
using (bus.Subscribe(nameof(ChatEndPoint), message => OnMessage(message, connection)))
|
using (bus.Subscribe(nameof(ChatEndPoint), message => OnMessage(message, connection)))
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
|
|
@ -27,7 +33,7 @@ namespace SocketsSample
|
||||||
|
|
||||||
await bus.Publish(nameof(ChatEndPoint), new Message()
|
await bus.Publish(nameof(ChatEndPoint), new Message()
|
||||||
{
|
{
|
||||||
Payload = input
|
Payload = input.ToArray()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|
@ -37,6 +43,11 @@ namespace SocketsSample
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await bus.Publish(nameof(ChatEndPoint), new Message
|
||||||
|
{
|
||||||
|
Payload = Encoding.UTF8.GetBytes($"{connection.ConnectionId} disconnected")
|
||||||
|
});
|
||||||
|
|
||||||
connection.Channel.Input.Complete();
|
connection.Channel.Input.Complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,7 +55,7 @@ namespace SocketsSample
|
||||||
{
|
{
|
||||||
var buffer = connection.Channel.Output.Alloc();
|
var buffer = connection.Channel.Output.Alloc();
|
||||||
var payload = message.Payload;
|
var payload = message.Payload;
|
||||||
buffer.Append(ref payload);
|
buffer.Write(payload);
|
||||||
await buffer.FlushAsync();
|
await buffer.FlushAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue