c# - call a code when done playing in unity -


i got tcp server in java , class called client among unity scripts. client singleton connects server @ constructor. when press play button connects server when press again stop game client not disconnects automatically when run game again gets stack because tries connect computer connected. tried disconnect @ destructor never been called. how can it? tried implement idisposable not know if need call or garbage collector calls (for me not). client's code (read write , loop not important question left in case wants it):

using assets.scripts.tools; using system.collections; using system.collections.generic; using system.io; using system.net.sockets; using system.threading; using unityengine; using system;  public class client : system.object, idisposable{     private tcpclient tcp;     private stream stream;     private iclientcallback callback;     private thread recvthread;      private static client instance = null;      public static client instance     {                 {             if (instance == null)                 instance = new client();             return instance;         }     }      private client()     {         init();     }      public void init()     {         log.d("init client");         tcp = new tcpclient();         tcp.connect(main.ip, main.port);         stream = tcp.getstream();         recvthread = new thread(loop);         recvthread.start();     }      public void setcallback(iclientcallback callback)     {         this.callback = callback;     }      // recving loop     private void loop()     {         while (true)         {             message msg = read();             if (callback != null)             {                 callback.callback(msg);             }             else             {                 log.e("callbackpointer null, msg not handheld");             }         }     }      public void disconnect()     {         tcp.close();         stream.close();     }      // read message server (will wait until message recved     private message read()     {         try         {             int val;             string res = "";                         {                 val = stream.readbyte();                 res += (char)val;             } while (val != code.end_message && val != -1);             return new message(res);         }         catch (ioexception)         {             log.e("could not read server, disconnecting");             throw new system.exception("could not read server");         }     }      // write message server     public void write(message msg)     {         try         {             log.d("write:", msg.getdebug());             stream.write(msg.getbytes(), 0, msg.getbytes().length);             stream.flush();         }         catch (ioexception)         {             log.e("could not send message client");             throw new system.exception("could not write server: " + msg.getcode().tostring() + msg.tostring());         }     }      public void dispose()     {         log.d("never been called");     }      ~client()     {         log.d("never been called");     } } 

use onapplicationquit() callback function , disconnect tcp server or call disconnect() method within there. callback run right before application closes, both in editor , in built standalone player.
onapplicationquit()


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -