Ejemplo n.º 1
0
 /// <summary>
 /// 接收
 /// </summary>
 void SocketReceive()
 {
     //进入接收循环
     while (true)
     {
         //对data清零
         recvData = new byte[1024];
         //获取客户端,获取客户端数据,用引用给客户端赋值
         recvLen = socket.ReceiveFrom(recvData, ref ep);
         //接收到一个包
         dpget = ((DgramPlayer)ConvertHelper.ByteToStructure(recvData));
         if (dpget.IP == MyIP)
         {
             continue;
         }
         else if (PlayerList.ContainsKey(dpget.IP))
         {
             //伤害数据包
             if (dpget.damage != 0 && MyIP == dpget.damageIP)
             {
                 MPlayerInfo.getins().damage(dpget.damage);
             }
             else
             {
                 //解析
                 OtherPlayer temp = PlayerList[dpget.IP];
                 GameObject  p    = temp.gobj;
                 temp.pos   = dpget.getpos();
                 temp.rot   = dpget.getrot();
                 temp.speed = dpget.speed;
                 //hp
                 temp.UpdateHp(dpget.HP);
                 //gun
                 temp.GunManage(dpget.getgunrot(), dpget.shooting);
             }
         }
         else
         {
             //加入
             //由于主线程才能使用unity的api,把添加用户的逻辑交给update;
             lock (currentjoin)
             {
                 currentjoin.join = true;
                 currentjoin.IP   = dpget.IP;
             }
         }
     }
 }
Ejemplo n.º 2
0
 //服务器接收
 void SocketReceive()
 {
     //进入接收循环
     while (true)
     {
         //对data清零
         recvData = new byte[1024];
         //获取客户端,获取客户端数据,用引用给客户端赋值
         recvLen = socket.ReceiveFrom(recvData, ref clientEnd);
         pos     = ((DgramPlayer)ConvertHelper.ByteToStructure(recvData)).getpos();
         rot     = ((DgramPlayer)ConvertHelper.ByteToStructure(recvData)).getrot();
         //print("message from: " + clientEnd.ToString()); //打印客户端信息
         //                                                //输出接收到的数据
         //recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen);
         //print("我是服务器,接收到客户端的数据" + recvStr);
         ////将接收到的数据经过处理再发送出去
         //sendStr = "From Server: " + recvStr;
         //SocketSend(sendStr);
     }
 }