using SuperSocket;
using SuperSocket.Channel;
using SuperSocket.Server;
using System.Text;
namespace IStation.Server
{
///
/// Session会话
///
internal class MySession : AppSession
{
///
/// 会话名称
///
public string SessionName { get; set; }
///
/// 是否连接
///
public bool IsConnected
{
get
{
return this.State == SessionState.Connected;
}
}
///
/// 关闭事件
///
public event Action SessionClosedEvent;
protected override ValueTask OnSessionConnectedAsync()
{
LogHelper.Info(this.RemoteEndPoint.ToString() + "连接成功!");
return base.OnSessionConnectedAsync();
}
protected override ValueTask OnSessionClosedAsync(CloseEventArgs e)
{
this.SessionClosedEvent?.Invoke();
LogHelper.Info($"{this.SessionName},Socket连接关闭,关闭原因:{e.Reason}");
return base.OnSessionClosedAsync(e);
}
///
/// 发送文本
///
public async void Send(string message)
{
var bts = Encoding.UTF8.GetBytes(message);
var memory = new ReadOnlyMemory(bts);
await ((IAppSession)this).SendAsync(memory);
}
///
/// 发送字节
///
public async void Send(byte[] data)
{
var memory = new ReadOnlyMemory(data);
await ((IAppSession)this).SendAsync(memory);
}
///
/// 发送字节
///
public void Send(byte[] data, int offset, int length)
{
var bts = new byte[length];
Buffer.BlockCopy(data, offset, bts, 0, length);
Send(bts);
}
///
/// 关闭
///
public async void Close(string reason)
{
LogHelper.Info($"{this.SessionName} 准备关闭会话,原因:{reason}");
await base.CloseAsync();
}
}
}