前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >unity3d 对接 workerman 实现联机游戏功能案例分享

unity3d 对接 workerman 实现联机游戏功能案例分享

原创
作者头像
用户7718188
发布2022-11-06 20:18:58
4000
发布2022-11-06 20:18:58
举报
文章被收录于专栏:高级工程司

客户端是 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 Workerman\Worker;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 Connection\n";};// 接收客户端消息$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 closed\n";};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;        }    }}

4. 演示效果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 简单通讯
  • 2. workerman 安装
  • 3. 方块移动案例
  • 4. 演示效果
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档