2

我有一个试图放入 Unity 插件的 C# 脚本。

该脚本本质上只是创建一个套接字连接、摄取数据并对其进行解析。在 Unity 中用作“独立”脚本时,它可以完美运行。

这是脚本的代码:

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Linq;

public class ClientSocket
{
    private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private byte[] _recieveBuffer = new byte[8142];

    void Start()
    {
        SetupServer();
    }

    void Update()
    {

    }

    void OnApplicationQuit()
    {
        _clientSocket.Close();
    }

    private void SetupServer()
    {
        try
        {
            _clientSocket.Connect("127.0.0.1", 12345);

        }
        catch (SocketException ex)
        {
            Debug.Log(ex.Message);
        }

        _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);

    }

    private void ReceiveCallback(IAsyncResult AR)
    {
        //Check how much bytes are recieved and call EndRecieve to finalize handshake
        int recieved = _clientSocket.EndReceive(AR);

        if (recieved <= 0)
            return;

        //Copy the recieved data into new buffer, to avoid null bytes
        byte[] recData = new byte[recieved];
        Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);

        string incomingData = System.Text.Encoding.Default.GetString(_recieveBuffer);
        Debug.Log(incomingData);            
    }
}

正如我所提到的,这在作为脚本时非常有用——创建了一个连接,我ReceiveCallback触发了它,并从套接字连接接收数据。

现在,当我尝试在 Unity 插件中使用基本相同的代码时,就会创建连接;但是回调永远不会触发......对于为什么这可能会有所不同感到不知所措。任何想法或指针表示赞赏!

这是在插件中使用的稍作修改的版本:

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Linq;

namespace InputsPlugin
{
    public class ClientSocket
    {
        private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private byte[] _recieveBuffer = new byte[8142];

        public void StartConnection()
        {
            SetupServer();
        }

        private void SetupServer()
        {
            try
            {
                _clientSocket.Connect("127.0.0.1", 12345);
                Debug.Log("Connected!!!!");
            }
            catch (SocketException ex)
            {
                Debug.Log(ex.Message);
            }

            Debug.Log("Callback?");
            _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
            Debug.Log("Callback?");
        }

        private void ReceiveCallback(IAsyncResult AR)
        {
            Debug.Log("Callback!!!!");

            //Check how much bytes are recieved and call EndRecieve to finalize handshake
            int recieved = _clientSocket.EndReceive(AR);
            Debug.Log(recieved);

            if (recieved <= 0)
                return;

            //Copy the recieved data into new buffer, to avoid null bytes
            byte[] recData = new byte[recieved];
            Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);

            string incomingData = System.Text.Encoding.Default.GetString(_recieveBuffer);
            Debug.Log(incomingData);

            //Start receiving again
            _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }

        private void SendData(byte[] data)
        {
            SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
            socketAsyncData.SetBuffer(data, 0, data.Length);
            _clientSocket.SendAsync(socketAsyncData);
        }
    }
}
4

1 回答 1

3

您的 Start 方法很可能应该转换为构造函数,因为引擎不再调用它。作为私有方法,它不能从外部调用。

因此,您的 SetupServer 可能永远不会被调用。

您还可以删除 Update 和 OnApplicationQuit 应该是析构函数或一些清理方法。

于 2019-03-06T06:41:22.627 回答