using Newtonsoft.Json;
using Quobject.SocketIoClientDotNet.Client;
using System;
namespace SocketIOComponent
{
///
/// Class responsible for sending and receiving socket.IO messages
///
public class SocketIOClass
{
#region Fields
Socket _socket = null;
#endregion
#region Public properties
///
/// Returns the socket.IO uri in the form: ws://IP:SocketIOport
///
public string Uri { get; private set; }
#endregion
///
/// Constructor for the SocketIOClass
///
/// Socket io uri in the format: ws://IP:SocketIOport
public SocketIOClass(string uri)
{
_socket = IO.Socket(uri);
Uri = uri;
_socket.On(SocketIOEvent.PrivateCallRequest.ToString(), (data) =>
{
PrivateCallInfo callInfo = null;
try
{
callInfo = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive private call request:\n" + ex.ToString());
}
if (callInfo != null)
OnPrivateCallRequestReceived(new PrivateCallRequestReceivedEventArgs(callInfo));
});
_socket.On(SocketIOEvent.GroupCallRequest.ToString(), (data) =>
{
GroupCallInfo callInfo = null;
try
{
callInfo = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive group call request:\n" + ex.ToString());
}
if (callInfo != null)
OnGroupCallRequestReceived(new GroupCallRequestReceivedEventArgs(callInfo));
});
_socket.On(SocketIOEvent.Sms.ToString(), (data) =>
{
SmsInfo smsInfo = null;
try
{
smsInfo = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive sms:\n" + ex.ToString());
}
if (smsInfo != null)
OnSmsReceived(new SmsReceivedEventArgs(smsInfo));
});
_socket.On(SocketIOEvent.SmsAck.ToString(), (data) =>
{
SmsAckInfo smsAckInfo = null;
try
{
smsAckInfo = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive sms ack:\n" + ex.ToString());
}
if (smsAckInfo != null)
OnSmsAckReceived(new SmsAckReceivedEventArgs(smsAckInfo));
});
_socket.On(SocketIOEvent.Ars.ToString(), (data) =>
{
ArsInfo ars = null;
try
{
ars = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive ars:\n" + ex.ToString());
}
if (ars != null)
OnArsReceived(new ArsReceivedEventArgs(ars));
});
_socket.On(SocketIOEvent.EmergencyReq.ToString(), (data) =>
{
EmergencyInfo emerg = null;
try
{
emerg = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive emergency request:\n" + ex.ToString());
}
if (emerg != null)
OnEmergencyReceived(new EmergencyReceivedEventArgs(emerg));
});
_socket.On(SocketIOEvent.Gps.ToString(), (data) =>
{
GpsInfo gps = null;
try
{
gps = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive gps:\n" + ex.ToString());
}
if (gps != null)
OnGpsReceived(new GpsReceivedEventArgs(gps));
});
_socket.On(SocketIOEvent.PollResponse.ToString(), (data) =>
{
GpsInfo gps = null;
try
{
gps = JsonConvert.DeserializeObject(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive poll response:\n" + ex.ToString());
}
if (gps != null)
OnPollResponseReceived(new GpsReceivedEventArgs(gps));
});
}
#region Public Methods
///
/// Sends private call request
///
///
public void SendPrivateCallRequest(PrivateCallInfo callInfo)
{
_socket?.Emit(SocketIOEvent.PrivateCallRequest.ToString(), JsonConvert.SerializeObject(callInfo));
//Console.WriteLine("----> " + callInfo.ToString());
}
///
/// Sends group call request
///
/// Call info for Socket.IO
public void SendGroupCallRequest(GroupCallInfo callInfo)
{
_socket?.Emit(SocketIOEvent.GroupCallRequest.ToString(), JsonConvert.SerializeObject(callInfo));
//Console.WriteLine("----> " + callInfo.ToString());
}
///
/// Sends an sms request on Socket.IO
///
/// Sms data for Socket.IO
public void SendSms(SmsInfo sms)
{
_socket?.Emit(SocketIOEvent.Sms.ToString(), JsonConvert.SerializeObject(sms));
}
///
/// Sends an sms ack on Socket.IO
///
///
public void SendSmsAck(SmsAckInfo smsAck)
{
_socket?.Emit(SocketIOEvent.SmsAck.ToString(), JsonConvert.SerializeObject(smsAck));
}
///
/// Sends ARS on Socket.IO
///
/// ARS data for SocketIO
public void SendArs(ArsInfo arsInfo)
{
_socket?.Emit(SocketIOEvent.Ars.ToString(), JsonConvert.SerializeObject(arsInfo));
}
///
/// Sends emergency ack request
///
/// Object containing emergency ack parameters
public void SendEmergencyAck(EmergencyAckInfo emergencyAckInfo)
{
_socket?.Emit(SocketIOEvent.EmergencyAck.ToString(), JsonConvert.SerializeObject(emergencyAckInfo));
}
///
/// Sends poll request
///
/// Object containing poll request parameters
public void SendPollRequest(PollRequestInfo pollRequestInfo)
{
_socket?.Emit(SocketIOEvent.PollReq.ToString(), JsonConvert.SerializeObject(pollRequestInfo));
}
///
/// Disconnects the soket.IO client
///
public void Disconect()
{
_socket?.Disconnect();
}
#endregion
#region Events
///
/// Occurs when a private call request is received on socket.IO
///
public event EventHandler PrivateCallRequestReceived;
///
/// Occurs when a group call request is received on socket.IO
///
public event EventHandler GroupCallRequestReceived;
///
/// Occurs when a sms is received on socket.IO
///
public event EventHandler SmsReceived;
///
/// Occurs when a sms ack is received on socket.IO
///
public event EventHandler SmsAckReceived;
///
/// Occurs when an Ars is received on socket.IO
///
public event EventHandler ArsReceived;
///
/// Occurs when a Emergency is received on socket.IO
///
public event EventHandler EmergencyReceived;
///
/// Occurs when a GPS Position is received on socket.IO
///
public event EventHandler GpsReceived;
///
/// Occurs when a Poll response is received on socket.IO
///
public event EventHandler PollReceived;
private void OnPrivateCallRequestReceived(PrivateCallRequestReceivedEventArgs e)
{
EventHandler handler = PrivateCallRequestReceived;
if (handler != null)
handler(this, e);
}
private void OnGroupCallRequestReceived(GroupCallRequestReceivedEventArgs e)
{
EventHandler handler = GroupCallRequestReceived;
if (handler != null)
handler(this, e);
}
private void OnSmsReceived(SmsReceivedEventArgs e)
{
EventHandler handler = SmsReceived;
if (handler != null)
handler(this, e);
}
private void OnSmsAckReceived(SmsAckReceivedEventArgs e)
{
EventHandler handler = SmsAckReceived;
if (handler != null)
handler(this, e);
}
private void OnArsReceived(ArsReceivedEventArgs e)
{
EventHandler handler = ArsReceived;
if (handler != null)
handler(this, e);
}
private void OnEmergencyReceived(EmergencyReceivedEventArgs e)
{
EventHandler handler = EmergencyReceived;
if (handler != null)
handler(this, e);
}
private void OnGpsReceived(GpsReceivedEventArgs e)
{
EventHandler handler = GpsReceived;
if (handler != null)
handler(this, e);
}
private void OnPollResponseReceived(GpsReceivedEventArgs e)
{
EventHandler handler = PollReceived;
if (handler != null)
handler(this, e);
}
#endregion
}
}