Skip to content

Easy to use threaded socket communication in unity

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

WeibelLab/Comms-Unity

Repository files navigation

Comms-Unity

Easy to use threaded socket communication in unity

Importing into Project

Go to the Package Manager window, press the add button, and select "Add package from Git URL..." image

Paste in this link: https://github.com/WeibelLab/Comms-Unity.git You may get an error saying import failed. Paste in the link a second time. Unity will then import the package and display a series of errors in the Console. Unity will automatically fix all of these, you can safely clear them.

Using Comms

Add as a component image

You'll need a Server and a Client - these can be on the same or different computers. If they are on different computers, make sure unity is allowed through the firewall, and your system recognizes your network as a private network.

  1. Setup the server first image
    Choose a large port (1200-49000)
    I recommend using dynamic messages. You might choose non-dynamic to marginally increase performance or if working with a custom client/server.
  2. Setup the client image
    set the host to the IP address of your server's computer (127.0.0.1 if on the same computer) and the port to whatever you set your server to listen on.
  3. Run both your client and your server and see if you get a connection message image
  4. Now make a script to send messages. Drag in the ReliableCommunication reference onto the component.
using Comms;
using UnityEngine;

public class SendMessage : MonoBehaviour
{
    public ReliableCommunication comm;

    private void Start() {
        // Sending Strings
        comm.Send("Hello");

        // Sending JSON
        // JSONObject json = new JSONObject();
        // json.AddField("type", "foo");
        // json.AddField("data", "bar");
        // comm.Send(json);
    }
}
  1. And a script to receive messages
using Comms;
using UnityEngine;

public class ReceiveMessage : MonoBehaviour
{
    public ReliableCommunication comm;

    public void OnMessage(string msg) {
        Debug.Log("Client said: " + msg);
        // Do something
    }
}

To hook up the event listener,

  1. go into the "Data Events" tab
  2. select your message type (string for this example) image
  3. add an event
  4. connect to your script image Now when you run the server then client, your client will send a 'hello' message to the server which will log the message.
  5. Add connection/disconnection messages If you want to know when clients connect or disconnect:
  6. Add code that will run when the events happen (note that the methods receive a ReliableCommunication reference):
public void OnConnection(ReliableCommunication instance) {
    Debug.Log(instance.name + " connected");
}

public void OnDisconnection(ReliableCommunication instance) {
    Debug.Log(instance.name + " disconnected");
}
  1. Go to the "Status Events" tab and link the events to your script image

About

Easy to use threaded socket communication in unity

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published

Languages