Skip to content

Instantly share code, notes, and snippets.

@ferretnt
Created January 31, 2020 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferretnt/4e3951276527cc8f8deb6c41e2c8d00e to your computer and use it in GitHub Desktop.
Save ferretnt/4e3951276527cc8f8deb6c41e2c8d00e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BestHTTP;
using BestHTTP.ServerSentEvents;
using System;
public class GetRequest : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
string firebaseHost = "https://fir-testdatabase-<<REMOVED>>.firebaseio.com/foo.json";
HTTPRequest request = new HTTPRequest(new Uri(firebaseHost), OnRequestFinished);
request.Send();
var eventSource = new EventSource(new Uri(firebaseHost));
// Start to connect to the server
eventSource.OnMessage += OnMessage;
eventSource.Open();
var unireq = UnityEngine.Networking.UnityWebRequest.Get(firebaseHost);
unireq.SetRequestHeader("Accept", "text/event-stream"); // Removing this clearly works on webgl, but not with streaming.
var response = unireq.SendWebRequest();
int bytesSoFar = 0;
while (!unireq.downloadHandler.isDone)
{
if (unireq.downloadHandler.text.Length != bytesSoFar)
{
string allText = $"Bytes: {unireq.downloadHandler.text.Length} : " + unireq.downloadHandler.text;
Debug.Log(allText);
bytesSoFar = unireq.downloadHandler.text.Length;
}
yield return null;
}
Debug.Log("IsDone()" + unireq.responseCode);
}
// The OnRequestFinished() function's implementation might be this:
void OnRequestFinished(HTTPRequest request, HTTPResponse response)
{
Debug.Log("Request Finished! Text received: " + response.DataAsText);
}
private void OnMessage(EventSource eventSource, Message message)
{
Debug.Log(string.Format("Message: <color=yellow>{0}</color>", message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment