Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Created March 8, 2017 02:23
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 RickStrahl/0866f703fed1e61d268da1518cdd1556 to your computer and use it in GitHub Desktop.
Save RickStrahl/0866f703fed1e61d268da1518cdd1556 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mobi2GoClient.Entities;
using Newtonsoft.Json.Linq;
using Quobject.EngineIoClientDotNet.Client;
using Quobject.SocketIoClientDotNet.Client;
using Socket = Quobject.SocketIoClientDotNet.Client.Socket;
namespace Mobi2GoClient
{
public class Mobi2GoClient : IDisposable
{
public const string STR_API_KEY = "290bbbbf70d1e781e34c15473892ffab62986eca";
private Socket Socket;
private bool IsRunning = false;
public void Connect()
{
var options = new IO.Options();
options.Query = new Dictionary<string, string>();
options.Query.Add("api_key", STR_API_KEY);
Socket = IO.Socket("https://onverra-staging.mobi2go.com/",options);
}
public void Listen()
{
Socket.On(Socket.EVENT_CONNECT, () =>
{
Console.WriteLine("connected...");
});
Socket.On(Socket.EVENT_DISCONNECT, () =>
{
Console.WriteLine("disconnected...");
});
Socket.On(Socket.EVENT_ERROR, (data) =>
{
var ex = data as EngineIOException;
Console.WriteLine("error..." + ex.GetBaseException().Message);
});
Socket.On(Socket.EVENT_CONNECT_ERROR, (data) =>
{
var ex = data as EngineIOException;
Console.WriteLine("connect error..." + ex.GetBaseException().Message );
});
Socket.On(Socket.EVENT_RECONNECT, () =>
{
Console.WriteLine("reconnected...");
});
Socket.On("dispatch_message", (data) =>
{
var sequenceId = (String)((JObject)data)["sequence_id"];
var jorder = ((JObject)data)["data"];
var order = jorder.ToObject<Order>();
Console.WriteLine($"{order.id} - {order.total.ToString("n2")} - {order.customer.last_name}");
Socket.Emit("dispatch_message_ack", JObject.FromObject(new
{
sequence_id = sequenceId,
success = true,
data = "Custom data."
}));
});
Socket = Socket.Connect();
}
public void Start()
{
Task.Run(() =>
{
IsRunning = true;
Connect();
Listen();
while (IsRunning)
{
Task.Delay(50);
}
Socket?.Close();
});
}
public void Stop()
{
IsRunning = false;
}
public void Dispose()
{
Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment