Socket transmission of protobuf byte streams by unity Explorer (4) and unityprotobuf

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.