Socket transmission of protobuf byte streams by unity Explorer (4) and unityprotobuf
Copyright:This article is the original article, reprint please declare http://www.cnblogs.com/unityExplorer/p/7027659.html
I have already mentioned socket transmission in the previous article. This article mainly talks about reconnection due to disconnection. There is nothing to say. Just go to the code.
1 // I use a coroutine for the reconnection function, and it is not recommended to use a thread to reconnect, because most of the time, reconnection is required to tell the user, the use of threads will cause the display to be very troublesome. 2 int reconnectTime = 0; // Number of reconnections 3 IEnumerator Reconnect () 4 {5 // Close the socket. 6 Close () is defined in the previous article (); 7 reconnectTime ++; 8 bool isSuccess = false; 9 // re-initialize the socket and obtain the initialization result 10 isSuccess = Init (); 11 if (! IsSuccess) 12 {13 // if the number of reconnections is less than 3, try again. 14 // if the network has been disconnected for more than 3 times) 16 {17 yield return new WaitForSecondsRealtime (5); 18 StartCoroutine (Reconnect (); 19} 20 else21 Debug. log ("network error"); 22} 23 else24 {25 reconnectTime = 0; 26 // create a temporary List, store unsent messages 27 List <byte []> lb = new List <byte []> (); 28 lb. addRange (sendList); 29 // clear the original message set, add the logon request, and then add the unsent message 30 sendList. clear (); 31 // log on again, AddSendMessageQueue 32 AddSendMessageQueue ("logged on protobuf object", "logged on Message id"); 33 sendList. addRange (lb); 34}