发送
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;
}
}
在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;
}
}
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;
}
}
解析参数
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;
}
}
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;
}
}
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;
}
}