2017-07-25 28 views
-3

我在JAVA中获得了tcp server,并且在统一脚本中获得了名为Client的类。 Client是一个singleton,它连接到服务器的constructor。当我按下播放按钮时,它会连接到服务器,但当我再次按下停止游戏时,客户端不会自动断开连接,因此当我再次运行游戏时,它将获取堆栈,因为他尝试从已连接的计算机进行连接。我试图在destructor断开连接,但它从未被调用过。我该怎么做?我也试图实施IDisposable,但我不知道你是否需要打电话或garbage collector称它(对我来说不是)。 这是客户端的代码(readwriteloop是不是这个问题很重要,但我离开它,以防有人需要它):当完成团结游戏时调用代码

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 
    { 
     get 
     { 
      if (instance == null) 
       instance = new Client(); 
      return instance; 
     } 
    } 

    private Client() 
    { 
     Init(); 
    } 

    public void Init() 
    { 
     Log.D("Init the 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 is null, msg not handheld"); 
      } 
     } 
    } 

    public void disconnect() 
    { 
     tcp.Close(); 
     stream.Close(); 
    } 

    // read a message from the server (will wait until a message is recved 
    private Message Read() 
    { 
     try 
     { 
      int val; 
      string res = ""; 
      do 
      { 
       val = stream.ReadByte(); 
       res += (char)val; 
      } while (val != Code.END_MESSAGE && val != -1); 
      return new Message(res); 
     } 
     catch (IOException) 
     { 
      Log.E("Could not read from the server, disconnecting"); 
      throw new System.Exception("could not read from the server"); 
     } 
    } 

    // Write a message to the 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 to the client"); 
      throw new System.Exception("could not write to the server: " + msg.getCode().ToString() + msg.toString()); 
     } 
    } 

    public void Dispose() 
    { 
     Log.D("never been called"); 
    } 

    ~Client() 
    { 
     Log.D("never been called"); 
    } 
} 
+1

请问您可以发布您的代码吗? –

+0

@MichaelMeyer我编辑我的问题 – Roni

+0

也许这有帮助吗? https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html https://docs.unity3d.com/ScriptReference/EditorApplication-playmodeStateChanged.html – yes

回答

1

使用OnApplicationQuit()回调函数和断开您的TCP服务器或致电disconnect()方法在那里。此回调将在应用程序关闭之前运行,无论是在编辑器中还是在内置的独立播放器中。
OnApplicationQuit()