【问题标题】:call a code when done playing in unity统一播放时调用代码
【发布时间】:2017-12-31 11:16:20
【问题描述】:

我在 JAVA 中有一个 tcp server,在统一脚本中有一个名为 Client 的类。 Client 是一个singleton,它通过其constructor 连接到服务器。当我按下播放按钮时,它连接到服务器,但是当我再次按下停止游戏时,客户端不会自动断开连接,所以当我再次运行游戏时,它会获得堆栈,因为他试图从他已经连接的计算机连接。我试图在destructor 断开连接,但它从未被调用过。我该怎么做?我也尝试实现IDisposable,但我不知道您是否需要调用它或garbage collector 调用它(对我来说不是)。 这是客户的代码(read writeloop 对这个问题并不重要,但我留下了它以防有人想要它):

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");
    }
}

【问题讨论】:

标签: c# unity3d tcp tcpclient


【解决方案1】:

使用OnApplicationQuit() 回调函数并断开与您的tcp 服务器的连接,或在其中调用您的disconnect() 方法。此回调将在应用程序关闭之前在编辑器和内置的独立播放器中运行。
OnApplicationQuit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多