学习日记#05
Untiy
见缝插针小游戏
public class Pin : MOnoBehaviour
{
private bool isFire; // 向圆发射
private bool isFly; // 向发射点移动
private Transform startPoint;
private float moveSpeed;
private Transform circleTrans;
pirvate Vector3 targetPos;
void Start()
{
isFire = false;
isFly = false;
startPoint = GameObject.Find("statrPoint").transform;
moveSpeed = 5f;
circleTrans = GameObject.Find("circle").transform;
targetPos = circleTrans.transform - new Vector3(0, 2f, 0);
}
void Update()
{
if(!isFire)
{
if(!isFly)
{
transform.position = Vector3.MoveTowards(transform.position, startPoint.position, moveSpeed * Time.deltaTime);
if(Distance(transform.position, startPoint) < 0.1f)
{
isFly = true;
}
}
}
else
{
transform.position = Vector3.MoveTowards(transform.position,targetPos, moveSpeed * Time.deltaTime);
if(Distance(transform.position, targetPos) < 0.1f)
{
isFire = false;
}
}
}
public void Fire()
{
isFire = true;
}
}
public class GameObject : MonoBehaviour
{
private GameObject circle;
private GameObject pinPre;
private Pin currentPin;
private Transform spawnPoint;
private bool isGameOver;
void Start()
{
circle = GameObject.Find("circle");
pinPre = Resources.Load<GameObject>("Prefabs/pin");
spawnPoint = GameObject.Find("spawnPoint");
isGameOver = false;
Spawn();
}
void Update()
[
if(isGameOver)
{
if(Input.GetMouseButtonDown(0))
{
currentPin.Fire();
}
}
}
void Spawn()
{
GameObject pin = GameObject.Instantiate(pinPre, spawnPoint.position, pinPre.transform.rotation);
current = pin.GetComponent<Pin>();
}
void GameOver()
{
isGameOver = true;
}
IEnumerator GameOverAni()
{
while(ture)
{
camera.backgroundColor = Color.Lerp(camera.backgroundColor, Color.red, Time.deltaTime);
camera.= Color.Lerp(camera.backgroundColor, Color.red, Time.deltaTime);
}
}
}
public class PinHead : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D, other)
{
if(other.name == "PinHead")
{
GameObjec.Ciname.main.GetComponent<GameManager>().GameOver;
}
}
}
服务器 : 服务程序
- 强联网: 不仅客户端能向服务端发送请求,也能由服务端推送消息到客户端 socket
- 弱联网: web 只能从客户端向服务端发出请求 http
- socket套接字
描述ip地址和端口号,是一个通信链句柄
- 端口号
在网络协议中使用端口号识别主机上不同的进程
- 进程 :是具有一定独立功能的程序。是系统进行资源调度和分配的基本单位
- 线程 :线程是进程的一个实体,比进程更小的能独立运行,是cpu调度的基本单位,线程不拥有自己的系统资源
一个线程只能属于一个进程,而一个进程可以有多个线程,但是一个进程必须要有一个线程
资源分配给进程,同一进程的所有线程共享该进程的所有资源
线程是进程内的一个执行单元,也是进程内可调度的实体
不同的进程的线程之间可以利用消息机制实现同步通信
- Tcp与Udp
Tcp 三次握手与四次挥手,可靠连接
udp的速度比tcp快
udp不能保证有序传输
client server
socket socket
| |
| bind ip + port
| |
| listen
connect -> accept
send -> receive
|
receive <- send
close close
- 同步socket : 客户端阻塞
- 异步socket :
- 多路复用socket :服务端使用
- SocketEventArgs : 高并发场景
mySql
公钥
密钥
ServerSocket
using System.Net.Sockets;
internal class ServerSocket : Singleton<ServerSocket>
{
private static string PublicKey = "admin";
private static string SerectKey = "adminstrator";
private string m_IpStr = "127.0.0.1";
private int m_Port = 8999;
privaet Socket m_ListenSocket;
private static List<Socket> m_CheckReadList = new List<Socket>;// 存放所有客户端Socket
private static Dictionary<Socket, CilentSocket> m_ClientDic
= new Dictionary<Socket, CilentSocket>();// 存放所有客户端
private static long m_PingInterval = 30;// 心跳包冗余
private static List<Socket> m_TempList = new List<Socket>(); // 存储所有心跳包超时的客户端
void Init()
{
IPAddress iP = IPAddress.Parse(m_IpStr);
IPEndPoint iPEndPoint = new IPEndPoint(iP, m_Port);
m_ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_ListenSocket.Bind(iPEndPoint);
m_ListenSocket.Listen(200);
Debug.LogInfo("服务器成功启动监听{0}", m_ListenSocket.LocalEndPoint.ToString());
while(true)
{
try
{
ResetCheckRead();
Socket.Select(m_CheckReadList, null, null, 1000);
}
catch(Exception e)
{
Debug.Log("客户端连接异常{0}", e);
}
}
for (int i = m_CheckReadList.Count - 1; i >= 0; --i)
{
Socket socket = m_CheckReadList[i];
if (socket == m_ListenSocket)
{
}
else
{
}
}
// 检查所有客户端的心跳包
long timeNow = GetTimeStamp();
foreach(ClientSocket client in m_ClientDic.Values)
{
if(timeNow - client.LastPingTime > m_PingInverval * 4)
{
Debug.Log("心跳包超时!" + cilentSocket.Socket.RemoteEndPoint.ToString());
m_TempList.Add(client);
}
}
// 对心跳包超时的客户端统一关闭
foreach (CilentSocket cilentSocket in m_TempList)
{
CloseSocket(cilentSocket);
}
}
void ResetCheckRead()
{
m_CheckReadList.Clear();
m_CheckReadList.Add(m_ListenSocket);
// 遍历所有客户端信息
foreach (Socket socket in m_ClientDic.Keys)
{
m_CheckReadList.Add(socket);
}
}
public void ReadClient()
{
try
{
Socket client = listen.Accept();// 接受客户端连接
ClientSocket clientSocket = new ClientSocket();
clientSocket.Socket = client;
clientSocket.LastPingTime = GetTimeStamp();
m_ClientDic.Add(client, clientSocket);
Debug.LogInfo("一个客户端建立连接: {0}, 当前{1}个客户端在线!", client.LocalEndPoint, m_CheckReadList.Count);
}
catch (SocketException e)
{
Debug.LogError("接受一个套接字异常{0}", e);
throw;
}
}
// 时间戳
public static long GetTimeStamp()
{
TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(timeSpan.TotaleSeconds);
}
public void CloseSocket(ClientSocket client)
{
client.Socket.Close();
m_ClientDic.Remove(client.Socket);
Debug.Log("");
}
}
public class ClientSocket
{
public Socket Socket {get; set;}
public long LastPingTime {get; set;}
}