1

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.

2
  • You're asking way too much here. Commented Feb 5, 2017 at 15:31
  • @MarkC. Any suggestions on what can I do to me more precise? Commented Feb 6, 2017 at 7:56

1 Answer 1

1

So since I am unity but I do not want to use SocketIO as MonoBehaviour, I tweaked the Unity SocketIO package for unity to work without the Unity itself.

This is what I did:

First I have downloaded the package itself: https://www.assetstore.unity3d.com/en/#!/content/21721

Then I changed the SocketIOComponent Awake method to be its constructor and changed its signature to accept the url (more params can be sent)

public SocketIOComponent(string url)

The next thing I did was creating a wrapper which I have called SocketIOAdapter

This is the code:

public class SocketIOAdapter {

    SocketIOComponent socketIO;
    protected Thread socketThread;
    public SocketIOAdapter(string url)
    {
        socketIO = new SocketIOComponent(url);
    }

    public void Connect()
    {
        socketIO.Connect();
        socketThread = new Thread(socketIO.Update);
        socketThread.Start();
    }

    public void Emit(string eventName, JSONObject json)
    {
        socketIO.Emit(eventName, json);
    }

    public void On(string eventName, System.Action<SocketIOEvent> callback)
    {
        socketIO.On(eventName, callback);
    }
}

And lastly I have tweaked the Update method of SocketIOComponent to sleep and continue rather than return, like this:

if (ackList.Count == 0)
{
   Thread.Sleep(100);
   continue;
}
if (DateTime.Now.Subtract(ackList[0].time).TotalSeconds < ackExpirationTime)
{
    Thread.Sleep(100);
    continue;
}

Finally I have used it like this:

socketIO = new SocketIOAdapter(serviceManager.getConfig().get("servers.socket") as string);
socketIO.Connect();
socketIO.On("connect", this.OnConnect);

Listened like this:

void OnConnect(SocketIOEvent e)
{
    socketIO.Emit("shoot", new JSONObject("{}"));
}

So far so good, it's working well. Hope this helps someone out there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.