Advertisement
ReiAyanami

Untitled

Jun 14th, 2015
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4.  
  5. /*
  6.  * Simple echo server and client
  7.  * For editor or standalone player it supports eiter client and server, for webgl client only supported
  8.  */
  9. public class StWindow : MonoBehaviour {
  10.    
  11.     private bool _isStarted = false;
  12.     private bool _isServer = false;
  13.     string ip  = "127.0.0.1";
  14.     int   port = 7075;
  15.  
  16.     private int m_ConnectionId = 0;
  17.     private int m_WebSocketHostId = 0;
  18.     private int m_GenericHostId = 0;
  19.  
  20.     private ConnectionConfig m_Config = null;
  21.     private byte m_CommunicationChannel = 0;
  22.  
  23.     private int maxReceivedPing = 0, minReceivedPing = int.MaxValue, lastReceivedPing = 0;
  24.  
  25.     void Start()
  26.     {
  27.         m_Config = new ConnectionConfig();                                         //create configuration containing one reliable channel
  28.         m_CommunicationChannel = m_Config.AddChannel(QosType.Reliable);
  29.     }
  30.  
  31.     void OnGUI () {
  32.         GUI.Box(new Rect(5, 5, 450, 450), "window");       
  33.         if( !_isStarted )
  34.         {
  35.             ip = GUI.TextField(new Rect(10, 10, 250, 30), ip, 25);
  36.             port = Convert.ToInt32( GUI.TextField(new Rect(10, 40, 250, 30), port.ToString(), 25) );
  37. #if !(UNITY_WEBGL && !UNITY_EDITOR)
  38.             if ( GUI.Button( new Rect(10, 70, 250, 30), "start server" ) )
  39.             {
  40.                 _isStarted = true;
  41.                 _isServer = true;
  42.                 NetworkTransport.Init();
  43.  
  44.                 HostTopology topology = new HostTopology(m_Config, 12);
  45.                 m_WebSocketHostId = NetworkTransport.AddWebsocketHost(topology, port, null);           //add 2 host one for udp another for websocket, as websocket works via tcp we can do this
  46.                 m_GenericHostId = NetworkTransport.AddHost(topology, port, null);
  47.             }
  48. #endif
  49.             if (GUI.Button(new Rect(10, 100, 250, 30), "start client"))
  50.             {
  51.                 _isStarted = true;
  52.                 _isServer = false;
  53.                 NetworkTransport.Init();
  54.  
  55.                 HostTopology topology = new HostTopology(m_Config, 12);
  56.                 m_GenericHostId = NetworkTransport.AddHost(topology, 0); //any port for udp client, for websocket second parameter is ignored, as webgl based game can be client only
  57.                 byte error;
  58.                 m_ConnectionId = NetworkTransport.Connect(m_GenericHostId, ip, port, 0, out error);
  59.             }
  60.         }
  61.         else
  62.         {
  63.             GUI.Label(new Rect(10, 20, 250, 500), "Min: " + minReceivedPing);
  64.             GUI.Label(new Rect(10, 40, 250, 500), "Max: " + maxReceivedPing);
  65.             GUI.Label(new Rect(10, 60, 250, 500), "Last: " + lastReceivedPing);
  66.             if (GUI.Button(new Rect(10, 120, 250, 50), "stop"))
  67.             {
  68.                 _isStarted = false;
  69.                 NetworkTransport.Shutdown();
  70.             }
  71.         }
  72.     }
  73.  
  74.     void Update()
  75.     {
  76.         if (!_isStarted)
  77.             return;
  78.         int recHostId;
  79.         int connectionId;
  80.         int channelId;
  81.         byte[] recBuffer = new byte[1024];
  82.         int bufferSize = 1024;
  83.         int dataSize;
  84.         byte error;
  85.        
  86.         NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
  87.         if(error != 0)
  88.         {
  89.             Debug.Log ((NetworkError)error + " " + dataSize + " ");
  90.             return;
  91.         }
  92.         switch (recData)
  93.         {
  94.            
  95.         case NetworkEventType.ConnectEvent:
  96.         {
  97.             if (!_isServer)
  98.             {
  99.                 int networkTime = NetworkTransport.GetNetworkTimestamp();
  100.                 byte[] bytes = System.BitConverter.GetBytes(networkTime);
  101.                
  102.                 NetworkTransport.Send(recHostId, connectionId, m_CommunicationChannel, bytes, bytes.Length, out error);     //when client received connection signal it starts send echo               
  103.             }
  104.             Debug.Log(String.Format("Connect from host {0} connection {1}", recHostId, connectionId));
  105.             break;
  106.         }
  107.            
  108.         case NetworkEventType.DataEvent:  //if server will receive echo it will send it back to client, when client will receive echo from serve wit will send other message
  109.         {
  110.             Debug.Log(String.Format("Received event host {0} connection {1} channel {2} message length {3}", recHostId, connectionId, channelId, dataSize));
  111.  
  112.             int receivedTimestamp = System.BitConverter.ToInt32(recBuffer, 0);
  113.             lastReceivedPing = NetworkTransport.GetRemoteDelayTimeMS(recHostId, connectionId, receivedTimestamp, out error);
  114.             if(error != 0)
  115.                 Debug.Log ((NetworkError)error);   
  116.             else
  117.             {
  118.                 if(lastReceivedPing < minReceivedPing)
  119.                     minReceivedPing = lastReceivedPing;
  120.                 if(lastReceivedPing > maxReceivedPing)
  121.                     maxReceivedPing = lastReceivedPing;
  122.             }
  123.  
  124.  
  125.             int networkTime = NetworkTransport.GetNetworkTimestamp();
  126.             byte[] bytes = System.BitConverter.GetBytes(networkTime);
  127.  
  128.             NetworkTransport.Send(recHostId, connectionId, m_CommunicationChannel, bytes, bytes.Length, out error);    
  129.             if(error != 0)
  130.                 Debug.Log ((NetworkError)error);       
  131.         }
  132.             break;
  133.         case NetworkEventType.DisconnectEvent:
  134.         {
  135.             Debug.Log(String.Format("DisConnect from host {0} connection {1}", recHostId, connectionId));
  136.             break;
  137.         }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement