Skip to content

Instantly share code, notes, and snippets.

@coreyjs
Last active August 29, 2015 14:14
Show Gist options
  • Save coreyjs/f7287e999d8252670357 to your computer and use it in GitHub Desktop.
Save coreyjs/f7287e999d8252670357 to your computer and use it in GitHub Desktop.
Pulsar Unity Plugin
using UnityEngine;
using System.Collections;
using System.ComponentModel;
using System.Net;
using System.IO;
using System.Text;
namespace PulsarApp{
public class Pulsar : Singleton<Pulsar> {
private string ApiKey{ get; set; }
/// <summary>
/// Gets or sets the app key/token.
/// </summary>
/// <value>The app key.</value>
private string AppKey{ get; set; }
private string BaseUrl{ get; set; }
private bool isInitialized = false;
/// <summary>
/// Set the console logging to be verbose
/// </summary>
public bool Verbose = true;
public Pulsar(){
}
public void Initialize(string ApiKey, string AppKey){
this.ApiKey = ApiKey;
this.AppKey = AppKey;
this.BaseUrl = "www";
this.isInitialized = true;
if(Verbose){
Debug.Log("Pulsar Initialized");
}
}
public IEnumerator AddEventMessage(string dataLevel, string eventData){
// POST to the API
if(isInitialized){
StringBuilder paramBuilder = new StringBuilder();
paramBuilder.Append(string.Format("pulsar[api_key]={0}", this.ApiKey));
paramBuilder.Append(string.Format("&pulsar[app_key]={0}", this.AppKey));
paramBuilder.Append(string.Format("&message[level]={0}", dataLevel));
paramBuilder.Append(string.Format("&message[event]={0}", eventData));
byte[] byteData = Encoding.ASCII.GetBytes(paramBuilder.ToString());
WWWForm form = new WWWForm();
WWW request = new WWW(this.BaseUrl, byteData, form.headers);
if(Verbose){
Debug.Log("Message Prepared, Sending");
}
yield return request;
if(Verbose){
if(request.error == null){
Debug.Log("Pulsar Request Success: ");
}else{
Debug.Log("Pulsar Request Failure: " + request.error);
}
}
}
}
/// <summary>
/// Adds the event message.
/// </summary>
/// <param name="eventData">Event data.</param>
public void AddEventMessage(string eventData){
this.AddEventMessage("", eventData);
}
}
#region Singleton Base
public abstract class Singleton<T> where T : Singleton<T>, new()
{
private static volatile T instance;
private static object syncRoot = new Object();
public static T Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new T();
}
}
return instance;
}
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment