客户端是 C#,就简单先以和服务器端连接,发送,接收做例子,进一步就是方块移动,坐标传递。
1. 简单通讯
客户端只是用面板画出一个输入框 (地址) 和显示区域 (接收服务端发送的内容),而服务器端是创建 TCP 服务,接收与发送。
(1). 客户端连接
1 | //连接 public void Connetion() { //清理text recvText.text = ""; //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Connect string host = hostInput.text; int port = int.Parse(portInput.text); socket.Connect(host, port); clientText.text = "客户端地址1 " socket.LocalEndPoint.ToString(); //Recv socket.BeginReceive(readBuff, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCb, null); } |
---|
(2). 客户端接收
2 | //接收回调 private void ReceiveCb(IAsyncResult ar) { try { //count是接收数据的大小 int count = socket.EndReceive(ar); //数据处理 string str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count); if (recvStr.Length > 300) recvStr = ""; recvStr = str "n"; recvText.text = "接收的消息 " recvStr; Debug.LogError("接收的消息 " recvStr); //继续接收 socket.BeginReceive(readBuff, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCb, null); } catch (Exception e) { recvText.text = "链接已断开"; socket.Close(); } } |
---|
(3). 客户端发送
3 | //发送数据 public void Send() { string str = textInput.text; byte[] bytes = System.Text.Encoding.Default.GetBytes("test:" str); try { socket.Send(bytes); } catch { } } |
---|
2. workerman 安装
(1). 新启一个项目,进入该目录,composer require workerman/workerman
(2). 创建一个 start.php
4 | <?phpuse WorkermanWorker;require_once __DIR__ . '/vendor/autoload.php';// #### 开启TCP服务 ####$worker = new Worker('tcp://0.0.0.0:1234');// 4 processes//$worker->count = 4;// 客户端连接回调$worker->onConnect = function ($connection) { echo "New Connectionn";};// 接收客户端消息$worker->onMessage = function ($connection, $data) use ($worker) { // Send data to client echo json_encode($data) . "n"; //$ip = $connection->getRemoteIp(); foreach($worker->connections as $connection) { $connection->send($data); } //$connection->send("Hello $data n");};// 客户端关闭回调$worker->onClose = function ($connection) { echo "Connection closedn";};Worker::runAll();?> |
---|
(3). 启动,输入 php start.php start,成功如下
(4). 打开客户端的 6asyn 场景并运行,输入 TCP 服务的地址和端口
(5). 点击发送,就可以查看 workerman 接收到的信息。
3. 方块移动案例
方块移动服务器端几乎不用修改,在连接成功后,将多个客户端的坐标传递到服务器端,服务器处理后再给所有连接发送坐标,客户端再将数据绘制到场景中。
(1). 前后端数据约定
POS 用于标识行为,比如 POS 为坐标移动,同理聊天可以用 IM,登陆用 LOGIN 做标识等 (攻击)。第二个为客户端连接标识,标识往后为坐标 X, Y, Z。
(2). 坐标的整合发送
服务器端在接收消息回调中,循环所有连接端,并给所有连接端发送从客户端发送过来的坐标。
5 | $worker->onMessage = function ($connection, $data) use ($worker) { // 循环连接 foreach($worker->connections as $connection) { // 发送坐标 $connection->send($data); }}; |
---|
客户端维护一个名为 players 的字典,它将存放所有玩家的信息。msgList 是消息列表,接收到服务端的消息后,客户端会将消息保存在 msgList 中,等待 Update 逐一进行处理。
6 | using UnityEngine;using System;using System.Collections;using System.Collections.Generic;using System.Net;using System.Net.Sockets;using UnityEngine.UI; public class Walk : MonoBehaviour{ //socket和缓冲区 Socket socket; const int BUFFER_SIZE = 1024; public byte[] readBuff = new byte[BUFFER_SIZE]; //玩家列表 Dictionary<string, GameObject> players = new Dictionary<string, GameObject>(); //消息列表 List<string> msgList = new List<string>(); //Player预设 public GameObject prefab; //自己的IP和端口 string id; //添加玩家 void AddPlayer(string id, Vector3 pos) { GameObject player = (GameObject)Instantiate(prefab, pos, Quaternion.identity); TextMesh textMesh = player.GetComponentInChildren<TextMesh>(); textMesh.text = id; players.Add(id, player); } //发送位置协议 void SendPos() { GameObject player = players[id]; Vector3 pos = player.transform.position; //组装协议 string str = "POS "; str = id " "; str = pos.x.ToString() " "; str = pos.y.ToString() " "; str = pos.z.ToString() " "; byte[] bytes = System.Text.Encoding.Default.GetBytes(str); socket.Send(bytes); Debug.Log("发送 " str); } //发送离开协议 void SendLeave() { //组装协议 string str = "LEAVE "; str = id " "; byte[] bytes = System.Text.Encoding.Default.GetBytes(str); socket.Send(bytes); Debug.Log("发送 " str); } //移动 void Move() { if (id == "") return; GameObject player = players[id]; //上 if (Input.GetKey(KeyCode.UpArrow)) { player.transform.position = new Vector3(0, 0, 1); SendPos(); } //下 else if (Input.GetKey(KeyCode.DownArrow)) { player.transform.position = new Vector3(0, 0, -1); ; SendPos(); } //左 else if (Input.GetKey(KeyCode.LeftArrow)) { player.transform.position = new Vector3(-1, 0, 0); SendPos(); } //右 else if (Input.GetKey(KeyCode.RightArrow)) { player.transform.position = new Vector3(1, 0, 0); SendPos(); } } //离开 void OnDestory() { SendLeave(); } //开始 void Start() { Connect(); //请求其他玩家列表,略 //把自己放在一个随机位置 UnityEngine.Random.seed = (int)DateTime.Now.Ticks; float x = 100 UnityEngine.Random.Range(-30, 30); float y = 0; float z = 100 UnityEngine.Random.Range(-30, 30); Vector3 pos = new Vector3(x, y, z); AddPlayer(id, pos); //同步 SendPos(); } //链接 void Connect() { //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Connect socket.Connect("192.168.1.199", 1234); id = socket.LocalEndPoint.ToString(); //Recv socket.BeginReceive(readBuff, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCb, null); } //接收回调 private void ReceiveCb(IAsyncResult ar) { try { int count = socket.EndReceive(ar); //数据处理 string str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count); msgList.Add(str); //继续接收 socket.BeginReceive(readBuff, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCb, null); } catch (Exception e) { socket.Close(); } } void Update() { //处理消息列表 for (int i = 0; i < msgList.Count; i ) HandleMsg(); //移动 Move(); } //处理消息列表 void HandleMsg() { //获取一条消息 if (msgList.Count <= 0) return; string str = msgList[0]; msgList.RemoveAt(0); //根据协议做不同的消息处理 string[] args = str.Split(' '); if (args[0] == "POS") { OnRecvPos(args[1], args[2], args[3], args[4]); } else if (args[0] == "LEAVE") { OnRecvLeave(args[1]); } } //处理更新位置的协议 public void OnRecvPos(string id, string xStr, string yStr, string zStr) { //不更新自己的位置 if (id == this.id) return; //解析协议 float x = float.Parse(xStr); float y = float.Parse(yStr); float z = float.Parse(zStr); Vector3 pos = new Vector3(x, y, z); //已经初始化该玩家 if (players.ContainsKey(id)) { players[id].transform.position = pos; } //尚未初始化该玩家 else { AddPlayer(id, pos); } } //处理玩家离开的协议 public void OnRecvLeave(string id) { if (players.ContainsKey(id)) { Destroy(players[id]); players[id] = null; } }} |
---|