Skip to content

Instantly share code, notes, and snippets.

@ferretnt ferretnt/EventSourceTest.cs Secret
Created Jan 31, 2020

What would you like to do?
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>>";
HTTPRequest request = new HTTPRequest(new Uri(firebaseHost), OnRequestFinished);
var eventSource = new EventSource(new Uri(firebaseHost));
// Start to connect to the server
eventSource.OnMessage += OnMessage;
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;
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
You can’t perform that action at this time.