I've been trying to implement the Emit Broadcast and On methods on C#.
I KNOW there is a package called SocketIO4DotNet and I do not wish to use it as it is deprecated. I rather understand how to do it.
I have tried to read the code of that package but it uses too dependancies over dependencies and it is going nowhere.
Currently I have the following code:
public class SocketIO
{
protected string host;
protected int port;
TcpClient client;
public SocketIO(string host, int port)
{
this.host = host;
this.port = port;
}
public bool connect()
{
if (isConnected())
{
return false;
}
try
{
client = new TcpClient();
client.Connect(host, port);
return true;
}
catch (Exception ex)
{
client = null;
return false;
}
}
public bool close()
{
if (!isConnected())
{
return false;
}
try
{
client.Close();
}
catch (Exception ex)
{
}
finally
{
client = null;
}
return true;
}
public void Emit(string message, string data)
{
if (!isConnected())
{
return;
}
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(data);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
string received = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
}
protected bool isConnected()
{
if (client == null)
{
return false;
}
return true;
}
I need to understand how to create the listener (On method) and how to use the Emit and Broadcast in a way that I send a message along with the data.