Unity插件内的C#脚本中未触发回调

时间:2019-03-06 06:28:48

标签: c# sockets unity3d iasyncresult

我有一个C#脚本,试图将其插入Unity插件。

该脚本实际上只是在创建套接字连接,提取数据并对其进行解析。当在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);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

您还可以删除Update,并且OnApplicationQuit应该是析构函数或某些清除方法。