untiy 链接 Photon 学习日志

1.创建客户端

2.创建单利PhotonEngine脚本

3.建立服务器连接

4.Unity发送请求request

(1)客户端

发送

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

接收响应。在PhotoEngine.cs

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

在ClienPeer.cs
//客户端发起请求的时候
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
switch (operationRequest.OperationCode)
{
case 1:
MyGameServer.log.Info(“收到客户端的请求,OpCode:1”);
//返回相应
OperationResponse opResponse = new OperationResponse(operationRequest.OperationCode);
//返回code,为发送过来的code,返回的参数,为发送过来的参数
SendOperationResponse(opResponse, sendParameters);
break;
default:
break;
}
}

(3)效果

在这里插入图片描述

5.请求带数据

(1)客户端

test.cs

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

PhotonEngine.cs

public void OnOperationResponse(OperationResponse operationResponse)
    {
      switch(operationResponse .OperationCode )
        {
            case 1:
                Debug.Log("收到服务器的响应,OpCode:1");
                //解析数据
                var data = operationResponse.Parameters;
                object intValue;
                data.TryGetValue(1, out intValue);
                object StringValue;
                data.TryGetValue(2, out StringValue);
                Debug.Log("收到客户端的请求,OpCode:1" + intValue.ToString() + ":" + StringValue.ToString());
                break;
            default:
                break;
        }
    }
(2)服务器

解析参数

ClienPeer.cs

  protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch (operationRequest.OperationCode)
            {
                case 1:
                    //解析数据
                    var data = operationRequest.Parameters;
                    object intValue;
                    data.TryGetValue(1, out intValue);
                    object StringValue;
                    data.TryGetValue(2, out StringValue);
                    MyGameServer.log.Info("收到客户端的请求,OpCode:1"+intValue.ToString()+":"+StringValue.ToString());
                    //返回响应
                    OperationResponse opResponse = new OperationResponse(operationRequest.OperationCode);
                    //构造参数
                    var data2 = new Dictionary<byte, object>();
                    data2.Add(1, 100);
                    data2.Add(2, "这个是服务器发送参数");
                    opResponse.SetParameters(data2);
                    //返回code,为发送过来的code,返回的参数,为发送过来的参数
                    SendOperationResponse(opResponse, sendParameters);
                    break;
                default:
                    break;
            }
        }
(3)结果

untiy 链接 Photon 学习日志_第1张图片
在这里插入图片描述

6.Server发送客户端Event

(1)客户端

PhotonEngine.cs

public void OnEvent(EventData eventData)
    {
        switch (eventData.Code)
        {
            case 1:      
                //解析数据
                var data = eventData .Parameters;
                object intValue, StringValue;
                data.TryGetValue(1, out intValue);  
                data.TryGetValue(2, out StringValue);
                Debug.Log("收到服务器的响应Event推送,OpCode:1" + intValue.ToString() + ":" + StringValue.ToString());
                break;
            default:
                break;
        }
    }
(2)服务器

ClienPeer.cs

 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch (operationRequest.OperationCode)
            {
                case 1:
                    //解析数据
                    var data = operationRequest.Parameters;
                    object intValue;
                    data.TryGetValue(1, out intValue);
                    object StringValue;
                    data.TryGetValue(2, out StringValue);
                    MyGameServer.log.Info("收到客户端的请求,OpCode:1"+intValue.ToString()+":"+StringValue.ToString());
                    //返回响应
                    OperationResponse opResponse = new OperationResponse(operationRequest.OperationCode);
                    //构造参数
                    var data2 = new Dictionary<byte, object>();
                    data2.Add(1, 100);
                    data2.Add(2, "这个是服务器发送参数");
                    opResponse.SetParameters(data2);
                    //返回code,为发送过来的code,返回的参数,为发送过来的参数
                    SendOperationResponse(opResponse, sendParameters);
                    //推送一个Event
                    EventData ed = new EventData(1);
                    ed.Parameters = data2;
                    SendEvent(ed, new SendParameters());
                    break;
                default:
                    break;
            }
        }
(3)结果

untiy 链接 Photon 学习日志_第2张图片

你可能感兴趣的:(untiy 链接 Photon 学习日志)