Advertisement
Hyluss

[Unity] Login Core

Dec 23rd, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Net;
  5. using System.IO;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9.  
  10. public class LoginCore : MonoBehaviour {
  11.  
  12.     public string serverAddress = "127.0.0.1";
  13.     public int serverPort = 32211;
  14.  
  15.     private TcpClient _client;
  16.     private NetworkStream _stream;
  17.     private Thread _thread;
  18.  
  19.     private byte[] _buffer = new byte[1024];
  20.     private string receiveMsg = "";
  21.     private bool isConnected = false;
  22.     private bool connectionError = false;
  23.     private float connectionTimer = 0.0f;
  24.  
  25.     void Start() {
  26.         SetupConnection();
  27.     }
  28.  
  29.     void OnApplicationQuit() {
  30.         CloseConnection();
  31.     }
  32.  
  33.     void OnGUI() {
  34.         if(connectionError) {
  35.             GUI.Label(new Rect(10,10,250,25), "Time to reconnect: "+connectionTimer.ToString()+" of 15 s");
  36.             if(connectionTimer >= 15.0f) {
  37.                 connectionError = false;
  38.                 connectionTimer = 0.0f;
  39.                 SetupConnection();
  40.             } else {
  41.                 connectionTimer += Time.deltaTime;
  42.             }
  43.         } else if(isConnected) {
  44.             if(GUI.Button(new Rect(10,10,250,25), "LOGIN"))
  45.                 SendData("0x000/Testuser/Testpassword");
  46.         }
  47.     }
  48.  
  49.     private void SetupConnection() {
  50.         try {
  51.             _thread = new Thread(ReceiveData);
  52.             _thread.IsBackground = true;
  53.             _client = new TcpClient(serverAddress, serverPort);
  54.             _stream = _client.GetStream();
  55.             _thread.Start();
  56.             isConnected = true;
  57.         } catch (Exception e) {
  58.             CloseConnection();
  59.             connectionError = true;
  60.             Debug.Log(e.ToString());
  61.         }
  62.     }
  63.  
  64.     private void ReceiveData() {
  65.         if(!isConnected)
  66.             return;
  67.  
  68.         int numberOfBytesRead = 0;
  69.         while(isConnected && _stream.CanRead) {
  70.             try {
  71.                 numberOfBytesRead = _stream.Read(_buffer, 0, _buffer.Length);
  72.                 receiveMsg = Encoding.ASCII.GetString(_buffer, 0, numberOfBytesRead);
  73.                 _stream.Flush();
  74.                 Debug.Log(receiveMsg);
  75.                 receiveMsg = "";
  76.             } catch (Exception e) {
  77.                 CloseConnection();
  78.                 Debug.Log(e.ToString());
  79.             }
  80.         }
  81.     }
  82.  
  83.     private void CloseConnection() {
  84.         if(isConnected) {
  85.             _thread.Interrupt();
  86.             _stream.Close();
  87.             _client.Close();
  88.             isConnected = false;
  89.             receiveMsg = "";
  90.         }
  91.     }
  92.  
  93.     private void SendData(string msgToSend) {
  94.         byte[] bytesToSend = Encoding.ASCII.GetBytes(msgToSend);
  95.         if(_stream.CanWrite)
  96.             _stream.Write(bytesToSend, 0, bytesToSend.Length);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement