美文网首页
Unity与Photon通信

Unity与Photon通信

作者: 沉麟 | 来源:发表于2019-09-26 20:41 被阅读0次

unity发送请求REQUEST

客户端----
发送消息:
在unity脚本里写个函数调用

public void SendReauset()
    {
        //构造参数
        var data = new Dictionary<byte,object>();
        //发送
        PhotonEngine.Instance.peer.OpCustom(1,data,true);
    }

接收响应:
在PhotonEngine脚本中

public void OnOperationResponse(OperationResponse operationResponse)
    {
        switch (operationResponse.OperationCode)
        {
            case 1:
                Debug.Log("收到服务器的响应:OpCaode:1");
                break;
            default:
                break;
        }
    }

服务器----
ClientPeer.cs里面

 //当每个客户端发起请求时执行
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch (operationRequest.OperationCode)
            {
                case 1:
                    MyGameServer.log.Info("收到客户端的请求:OpCaode:1");
                    //返回响应
                    OperationResponse operation = new OperationResponse(operationRequest.OperationCode);
                    //返回的code为发过来的code,返回的参数为发过来的参数
                    SendOperationResponse(operation, sendParameters);
                    break;
                default:
                    break;
            }
        }

如何请求带数据
客户端----
发送数据:

public void SendReauset()
    {
        //构造参数
        var data = new Dictionary<byte,object>();
        //发送带参数
        data.Add(1, 100);
        data.Add(2, "这是个参数");
        //发送
        PhotonEngine.Instance.peer.OpCustom(1,data,true);
    }

响应接收服务器数据:

public void OnOperationResponse(OperationResponse operationResponse)
    {
        switch (operationResponse.OperationCode)
        {
            case 1:
                Debug.Log("收到服务器的响应:OpCaode:1");

                //解析服务器的数据
                var data = operationResponse.Parameters;
                object intValue;
                data.TryGetValue(1, out intValue);
                object stringValue;
                data.TryGetValue(2, out stringValue);
                Debug.Log("解析服务器的数据:OpCaode:1" + intValue.ToString() + ":" + stringValue.ToString());

                break;
            default:
                break;
        }
    }

服务器----
ClientPeer.cs里面

        //当每个客户端发起请求时执行
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch (operationRequest.OperationCode)
            {
                case 1:
                    MyGameServer.log.Info("收到客户端的请求:OpCaode:1");
                    //解析客户端的数据
                    var data = operationRequest.Parameters;
                    object intValue;
                    data.TryGetValue(1, out intValue);
                    object stringValue;
                    data.TryGetValue(2, out stringValue);
                    MyGameServer.log.Info("解析客户端的数据:OpCaode:1" + intValue.ToString() + ":" + stringValue.ToString());

                    //返回响应
                    OperationResponse operation = new OperationResponse(operationRequest.OperationCode);

                    //构造参数
                    var data2 = new Dictionary<byte, object>();
                    //发送带参数
                    data2.Add(1, 100);
                    data2.Add(2, "这是个参数,服务器发送的");

                    operation.SetParameters(data2);
                    //返回的参数为发送的参数
                    SendOperationResponse(operation, sendParameters);
                    break;
                default:
                    break;
            }
        }

相关文章

网友评论

      本文标题:Unity与Photon通信

      本文链接:https://www.haomeiwen.com/subject/bhvnuctx.html