This repository has been archived on 2023-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
hamakaze/Hamakaze/WebSocket/WsCloseMessage.cs

37 lines
1.1 KiB
C#
Raw Normal View History

2022-04-09 22:34:05 +00:00
using System;
using System.Text;
namespace Hamakaze.WebSocket {
public class WsCloseMessage : WsMessage, IHasBinaryData {
2022-04-09 22:34:05 +00:00
public WsCloseReason Reason { get; }
public string ReasonPhrase { get; }
public byte[] Data { get; }
public WsCloseMessage(WsCloseReason reason) {
Reason = reason;
ReasonPhrase = string.Empty;
Data = Array.Empty<byte>();
}
public WsCloseMessage(byte[] data) {
if(data == null) {
Reason = WsCloseReason.NoStatus;
ReasonPhrase = string.Empty;
Data = Array.Empty<byte>();
} else {
Reason = (WsCloseReason)WsUtils.ToU16(data);
Data = data;
if(data.Length > 2)
try {
ReasonPhrase = Encoding.UTF8.GetString(data, 2, data.Length - 2);
} catch {
ReasonPhrase = string.Empty;
}
else
ReasonPhrase = string.Empty;
}
}
}
}