This is a custom UnityWebRequest Download Handler to support SSE's (Server-Sent Events) in Unity!
Simply inhert SseDownloadHandlerBase
and supply your own logic (Deserialise incoming lines, expose events, etc).
Then set the download handler to be an instance of your class.
webRequest.downloadHandler = new LogSseExampleDownloadHandler();
public class LogSseExampleDownloadHandler : SseDownloadHandlerBase
{
public LogSseExampleDownloadHandler() : base(new byte[1024]) { }
protected override void OnNewLineReceived(string line)
{
Debug.Log(line);
}
}
public class LogSseWithPooledBufferExampleDownloadHandler : SseDownloadHandlerBase
{
private byte[] _pooledBuffer;
public LogSseWithPooledBufferExampleDownloadHandler Create()
{
var buffer = ArrayPool<byte>.Shared.Rent(1024);
return new LogSseWithPooledBufferExampleDownloadHandler(buffer);
}
private LogSseWithPooledBufferExampleDownloadHandler(byte[] buffer) : base(buffer)
{
_pooledBuffer = buffer;
}
protected override void OnNewLineReceived(string line)
{
Debug.Log(line);
}
public override void Dispose()
{
base.Dispose();
ArrayPool<byte>.Shared.Return(_pooledBuffer);
}
}