Cocos2dX通过Java服务器向Unity传输数据四

前端之家收集整理的这篇文章主要介绍了Cocos2dX通过Java服务器向Unity传输数据四前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Unity3D相关代码

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

public class UnitySocket  {

	public  Socket mSocket=null;

    public UnitySocket()
    {
        
    }

    public  void Connect(string IP,int localPort)
    {
        mSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

        try
        {
            IPAddress ip = IPAddress.Parse(IP);
            IPEndPoint ipe = new IPEndPoint(ip,localPort);

            //---------------------m1-----------------------
            //mSocket.Connect(ipe);

            //---------------------m2------------------------
            mSocket.BeginConnect(ipe,new AsyncCallback(Connected),null);

        }
        catch(Exception e)
        {
            e.GetType();
        }
    }

    void Connected(IAsyncResult iar)
    {
        mSocket.EndConnect(iar);

    }

    bool ReceiveFlag = true;
    public  byte[] readData = new byte[1024];

   public void startReceive()
    {
        if (ReceiveFlag)
        {
            ReceiveFlag = false;
            mSocket.BeginReceive(readData,readData.Length,SocketFlags.None,new AsyncCallback(endReceive),mSocket);
        }
    }

   public void endReceive(IAsyncResult iar)
   {
       ReceiveFlag = true;
       Socket remote = (Socket)iar.AsyncState;
       int recv = remote.EndReceive(iar);
       if (recv > 0)
       {

       }
   }

   public void SendMessage(int rotation)
   {
       byte[] msg = BitConverter.GetBytes(rotation);
       mSocket.BeginSend(msg,msg.Length,new AsyncCallback(SendData),mSocket);
   }

   public void SendMessage(string data)
   {
       byte[] msg = Encoding.UTF8.GetBytes(data);
       mSocket.BeginSend(msg,mSocket);
   }

   void SendData(IAsyncResult iar)
   {
       Socket remote = (Socket)iar.AsyncState;
       int sent = remote.EndSend(iar);
   }

    public  void Send(int data)
    {
        byte[] longth = BitConverter.GetBytes(data);
        mSocket.Send(longth);
    }

    public  void Send(float data)
    {
        byte[] longth = BitConverter.GetBytes(data);
        mSocket.Send(longth);
    }

    public  void Send(string data)
    {
        byte[] longth = Encoding.UTF8.GetBytes(data);
        mSocket.Send(longth);
    }

    public  int ReceiveInt()
    {
        byte[] recvBytes = new byte[4];
        mSocket.Receive(recvBytes,4,0);
        int data=BitConverter.ToInt32(recvBytes,0);
        return data;
    }

    public double ReceiveDouble()
    {
        byte[] recvBytes=new byte[8];
        mSocket.Receive(recvBytes,0);
        double data = BitConverter.ToDouble(recvBytes,0);
        return data;
    }

    public string ReceiveString(int length)
    {
        byte[] recvBytes = new byte[length];
        mSocket.Receive(recvBytes,length,0);
        string data= Encoding.UTF8.GetString(recvBytes);
        return data;
    }



}

@H_403_5@using UnityEngine; using System.Collections; using UnityNetwork; using System; public class BoxManager : NetworkManager { NetTCPClient client = null; UnitySocket socket = null; int ra = 0; int flag = 0; public void Start() { //client = new NetTCPClient(); //client.Connect("172.27.35.1",59422); socket = new UnitySocket(); socket.Connect("172.27.35.1",59422); //BoxMove.Instance.yAngle = socket.ReceiveInt(); //socket.Send(3); //socket.Send("sss"); //int a = socket.ReceiveInt(); //Debug.Log(a); //int b = socket.ReceiveInt(); //int c = socket.ReceiveInt(); //Debug.Log(b); //Debug.Log(c); } public void Send(NetBitStream stream) { client.Send(stream); } public override void Update() { base.Update(); socket.SendMessage(5); socket.SendMessage("UNITY"); socket.startReceive(); ra = BitConverter.ToInt32(socket.readData,0); //if (socket.ReceiveString(8).StartsWith("Rotation")) ra = socket.ReceiveInt(); BoxMove.Instance.yAngle = ra; //flag += 1; //if (flag > 90) //{ // flag = 0; //} Debug.Log(ra); } }
using UnityEngine;
using System.Collections;
using UnityNetwork;

public class BoxMove : MonoBehavIoUr {

    public static BoxMove Instance = null;

    BoxManager _BoxMgr;
    Vector3 s = new Vector3(1,0);
    public float yAngle=0.0f;
	// Use this for initialization
	void Start () {
        DontDestroyOnLoad(this);
        Instance = this;
        _BoxMgr = new BoxManager();
        _BoxMgr.Start();
	}
	
	// Update is called once per frame
	void Update () {
        _BoxMgr.Update();
        //yAngle += 1.0f;
        transform.localRotation = Quaternion.AngleAxis(yAngle,s);
	}

}

猜你在找的Cocos2d-x相关文章