Last active
August 24, 2020 13:08
-
-
Save rjproz/942643cc96f949cac7409734e10e222e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ClientInstance | |
| { | |
| public string color; | |
| public LNSConnector connector; | |
| private string id; | |
| private NetDataWriter writer; | |
| public void Start(string id) | |
| { | |
| this.id = id; | |
| connector = new LNSConnector("rjproz", LNSMainThreadDispatcher.GetInstance()); | |
| connector.id = connector.displayName = id; | |
| connector.onConnected = OnConnected; | |
| connector.onRoomCreated = OnRoomCreated; | |
| connector.onMasterClientUpdated = OnMasterClientUpdated; | |
| connector.onRoomCreateFailed = OnRoomCreateFailed; | |
| connector.onRoomJoined = OnRoomJoined; | |
| connector.onRoomJoinFailed = OnRoomJoinFailed; | |
| connector.onPlayerConnected = OnPlayerConnected; | |
| connector.onPlayerDisconnected = OnPlayerDisconnected; | |
| connector.onDataReceived = OnDataReceived; | |
| connector.Connect("192.168.0.100", 10002); | |
| writer = new NetDataWriter(); | |
| } | |
| private void OnDataReceived(NetPacketReader reader) | |
| { | |
| try | |
| { | |
| string msg = reader.GetString(); | |
| LogColor(id + " : " + msg); | |
| } | |
| catch(System.Exception ex) | |
| { | |
| Debug.LogError(ex.Message + " "+ex.StackTrace); | |
| } | |
| } | |
| public void SendMessage() | |
| { | |
| writer.Reset(); | |
| writer.Put("This is message is from " + id + " at " + System.DateTime.Now); | |
| connector.SendData(writer, LiteNetLib.DeliveryMethod.ReliableUnordered); | |
| } | |
| private void OnRoomJoinFailed() | |
| { | |
| LogColor(id +": OnRoomJoinFailed"); | |
| } | |
| private void OnRoomJoined() | |
| { | |
| LogColor(id + ": OnRoomJoined"); | |
| } | |
| private void OnMasterClientUpdated(string obj) | |
| { | |
| LogColor(id + ": OnMasterClientUpdated to " + obj); | |
| } | |
| private void OnPlayerDisconnected(LNSClient client) | |
| { | |
| LogColor(id + ": Player disconnected : " + client.displayName); | |
| } | |
| private void OnPlayerConnected(LNSClient client) | |
| { | |
| LogColor(id + ": Player Connected : " + client.displayName); | |
| } | |
| private void OnRoomCreateFailed() | |
| { | |
| LogColor(id + ": On Room failed to create"); | |
| } | |
| private void OnRoomCreated() | |
| { | |
| LogColor(id + ": On Room created"); | |
| } | |
| private void OnConnected() | |
| { | |
| LogColor(id + ": On Connected to server"); | |
| connector.JoinRoomOrCreateIfNotExist("test"); | |
| } | |
| private void LogColor(string str) | |
| { | |
| Debug.Log("<color="+ color + ">"+str+"</color>"); | |
| } | |
| // Update is called once per frame | |
| public void OnDisable() | |
| { | |
| connector.Disconnect(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment