1.(客户端发送给服务端)test中的构造参数并且发送
完整代码如下
test.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour {
void Start () {
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
SendReauest();
}
}
private void SendReauest()
{
//构造参数
var data = new Dictionary<byte, object>();
//构造参数
data.Add(1,100);
data.Add(2,"这个是参数");
//发送
PhotonEngine.peer.OpCustom(1,data,true);
//协议 前端后端
PhotonEngine.peer.OpCustom(2,data,true);
PhotonEngine.peer.OpCustom(3,data,true);
PhotonEngine.peer.OpCustom(4,data,true);
PhotonEngine.peer.OpCustom(5,data,true);
}
}
2.服务端收到客户端的请求,并且解析数据,输出参数到log日志。
除此之外服务端还要构造一些参数并且响应给客户端。
完整代码如下:
ClientPeer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace RRGameServer
{
class ClientPeer : Photon.SocketServer.ClientPeer
{
public ClientPeer(InitRequest initRequest) : base(initRequest)
{
}
//当每个客户端断开时
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
//当客户端发起请求的时候
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.客户端收到服务端发过来的响应,并且解析数据,输出到客户端上面。
完整代码如下
PhotonEngine.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;
using Common;
public class PhotonEngine : MonoBehaviour,IPhotonPeerListener {
public static PhotonEngine Instance;
public static PhotonPeer peer;
private void Awake()
{
if (Instance == null)
{
Instance = this;
//当前Gameobject不会随场景的销毁而销毁
DontDestroyOnLoad(this.gameObject);
}
}
void Start () {
peer = new PhotonPeer(this,ConnectionProtocol.Udp);
peer.Connect("127.0.0.1:5055", "MyGame6");//通过ip连接到到对应的我们config中配置的Allpication
}
void Update () {
peer.Service();//开启服务
}
private void OnDestroy()//销毁的时候
{
if(peer!=null&&peer.PeerState == PeerStateValue.Connected)//如果存在且保持连接状态
{
peer.Disconnect();//就断开连接
}
}
public void DebugReturn(DebugLevel level, string message)
{
}
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;
//}
}
public void OnOperationResponse(OperationResponse operationResponse)
{
//DicTool.GetValue(RequestDic, (OperationCode)operationResponse.OperationCode).
// OnOprationRespionse(operationResponse);
//return;
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;
}
}
public void OnStatusChanged(StatusCode statusCode)
{
Debug.LogError(statusCode);
}
//private Dictionary<OperationCode, Request> RequestDic = new Dictionary<OperationCode, Request>();
public void AddRequest(Request r)
{
//RequestDic.Add(r.OpCode, r);
}
public void RemoveRequest(Request r)
{
// RequestDic.Remove(r.OpCode);
}
}
效果图:
客户端:
服务端: