Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity - How to Create a Basic Server / Client With LiteNetLib
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LiteNetLib;
public class Client : MonoBehaviour {
NetManager netManager;
EventBasedNetListener netListener;
// Start is called before the first frame update
void Start() {
netListener = new EventBasedNetListener();
netListener.PeerConnectedEvent += (server) => {
Debug.LogError($"Connected to server: {server}");
};
netManager = new NetManager(netListener);
netManager.Start(); // Don't forget to call .Start()!
netManager.Connect("localhost", 9050);
}
// Update is called once per frame
void Update() {
netManager.PollEvents();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LiteNetLib;
public class Server : MonoBehaviour {
NetManager netManager;
NetListener netListener;
// Start is called before the first frame update
void Start() {
netListener = new EventBasedNetListener();
netListener.ConnectionRequestEvent += (request) => {
request.Accept();
};
netListener.PeerConnectedEvent += (client) => {
Debug.LogError($"Client connected: {client}");
};
netManager = new NetManager(netListener);
}
// Update is called once per frame
void Update() {
netManager.PollEvents();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.