有统一和Web Player的套接字

时间:2016-09-12 14:56:41

标签: .net unity3d websocket

我正在使用.Net套接字并且在我为任何桌面操作系统导出独立应用程序时工作正常,即使我直接从Unity项目运行应用程序,但问题在我使用统一时开始网络播放器。我想,它拒绝了这个端口,因为端口。我将端口更改为我正在使用的应用程序,现在我没有拒绝客户端连接,但它仍然不起作用,显然客户端的数据没有到达服务器,也没有发生任何事情。 Unity网络播放器是否可以正常使用套接字?

public class SocketMain : MonoBehaviour {
System.Threading.Thread SocketThread;
volatile bool keepReading = false;

string alto = "", ancho ="";
int al = 0, an = 0;
// Use this for initialization
void Start () {
    Application.runInBackground = true;
    startServer();
}
bool bandera = false;
// Update is called once per frame
void Update () {

    if(al != 0 && an != 0)
    {
        Grid grid = gameObject.AddComponent<Grid>();
        grid.xSize = al;
        grid.zSize = an;
        al = 0;
        an = 0;
    }

}



void startServer (){
    SocketThread = new System.Threading.Thread(networkCode);
    SocketThread.IsBackground = true;
    SocketThread.Start();
}

private string getIPAddress()
{
    IPHostEntry host;
    string localIP = "";
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            localIP = ip.ToString();
        }

    }
    return localIP;
}

Socket listener;
Socket handler;

void networkCode()
{
    string data;

    // Data buffer for incoming data.
    byte[] bytes = new Byte[1024];

    // host running the application.
    Debug.Log("Ip " + getIPAddress().ToString());
    IPAddress[] ipArray = Dns.GetHostAddresses("127.0.0.1");
    IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755);

    // Create a TCP/IP socket.
    listener = new Socket(ipArray[0].AddressFamily,
        SocketType.Stream, ProtocolType.Tcp);

    // Bind the socket to the local endpoint and 
    // listen for incoming connections.

    try
    {
        listener.Bind(localEndPoint);
        listener.Listen(10);

        // Start listening for connections.
        while (true)
        {
            keepReading = true;

            // Mientras se espera la conexion de un cliente.
            Debug.Log("Esperando Conexxion");     

            handler = listener.Accept();
            Debug.Log("Se ha Desconectado el cluiente");     
            data = null;


            while (keepReading)
            {
                bytes = new byte[1024];

                int bytesRec = handler.Receive(bytes);

                Debug.Log("Received from Server");

                if (bytesRec <= 0)
                {
                    keepReading = false;
                    handler.Disconnect(true);
                    break;
                }
                //Datos recibidos y concatenados
                data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                //Pintamos data complto
                Debug.Log(data);

                var definition = new {alto = "", ancho ="" };

                //Deserealizamos data
                var medidas = JsonConvert.DeserializeAnonymousType(data, definition);

                Debug.Log("alto :" + medidas.alto + " , ancho :" + medidas.ancho);

                //Termina deserializacion
                alto = medidas.alto;
                ancho = medidas.ancho;
                al = int.Parse(alto);
                an = int.Parse(ancho);





                if (data.IndexOf("<EOF>") > -1)
                {
                    break;
                }

                System.Threading.Thread.Sleep(1);
            }

            System.Threading.Thread.Sleep(1);
        }
    }
    catch (Exception e)
    {
        Debug.Log(e.ToString());
    }
}

void stopServer()
{
    keepReading = false;

    //stop thread
    if (SocketThread != null)
    {
        SocketThread.Abort();
    }

    if (handler != null && handler.Connected)
    {
        handler.Disconnect(false);
        Debug.Log("Disconnected!");
    }
}

void OnDisable()
{
    stopServer();
}

}

这是我的服务器类

1 个答案:

答案 0 :(得分:1)

不是.NET套接字在Unity3D WebPlayer中不起作用,但多线程不起作用。

  

多线程在网络播放器中不可用,但不是由于Unity,   从我记得它的浏览器插件安全部分,即插件   无法运行多个线程/生成新线程

http://forum.unity3d.com/threads/webplayer-multi-threading.76087/