Skip to content

Instantly share code, notes, and snippets.

@EddieAbbondanzio
Created November 14, 2019 00:48
Show Gist options
  • Save EddieAbbondanzio/81eb3675f8cb8b51acbdc8888f9ab1a3 to your computer and use it in GitHub Desktop.
Save EddieAbbondanzio/81eb3675f8cb8b51acbdc8888f9ab1a3 to your computer and use it in GitHub Desktop.
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();
}
}
@Adyvan
Copy link

Adyvan commented Aug 18, 2021

need to add next line:

// Server Start
netManager.Start(9050);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment