/// <summary> /// Disconnect from server /// </summary> public void Disconnect() { if (ServerConnection == null || !ServerConnection.Connected) { return; } _logger.Log("Disconnecting from server..."); NetworkIdGenerator.SetLastId(0); _packet.Clear(); _packet.Add(new Data_Disconnect()); DataReceived += CloseConnection; ServerConnection = null; }
/// <summary> /// Connect to the server /// </summary> /// <param name="ip">IP-address</param> /// <param name="port">Port</param> public void Connect(string ip, int port) { _logger = Debug.unityLogger; IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), port); ServerConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _logger.Log($"Trying to connect to {ip}:{port}..."); try { ServerConnection.Connect(ipEndPoint); } catch (SocketException ex) { _logger.LogError("Connection Error", ex.Message); return; } _packet = new DataPacket(); _logger.Log($"Successfully connected to {ip}:{port}"); NetworkIdGenerator.SetLastId(0); }
private void Awake() { networkId = NetworkIdGenerator.Generate(); }