PhotonServer(3)-连接服务器-客户端
模拟一次:客户端发起一个请求,服务器响应这个请求
- 创建一个ChatServerListener,用来监听服务器的返回
- 客户端这边,由PhotonPeer带上Listener发起一个请求. Service发送这个请求.
- 每有一个客户端请求,客户端都创建一个Peer,.
- 由Peer中的OnOperationReqiust响应用户的请求.
- 客户端/服务器的交互消息用Dictionary<byte,object>.根据编码区分消息的类型.
服务器端:
入口
using Photon.SocketServer;
namespace PhotonTest
{
class ChatServer : ApplicationBase
{
/// <summary>
/// 有一个客户端连接过来调用
/// </summary>
/// <param name="initRequest"></param>
/// <returns></returns>
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new ChatPeer(initRequest);
}
/// <summary>
/// Server启动时调用
/// </summary>
protected override void Setup()
{
}
/// <summary>
/// Server关闭时调用
/// </summary>
protected override void TearDown()
{
}
}
}
Peer
using Photon.SocketServer;
using System.Collections.Generic;
using PhotonHostRuntimeInterfaces;
namespace PhotonTest
{
/// <summary>
/// 用来和客户端通信
/// </summary>
public class ChatPeer :ClientPeer
{
public ChatPeer(InitRequest initRequest) : base(initRequest)
{
}
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
/// <summary>
/// 客户端发起请求时候调用
/// </summary>
/// <param name="operationRequest"></param>
/// <param name="sendParameters"></param>
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
//响应客户端
Dictionary<byte, object> dict = new Dictionary<byte, object>();
dict.Add(1, "Dhjan");
OperationResponse operation = new OperationResponse(1, dict);
SendOperationResponse(operation, sendParameters);
}
}
}
客户端:
using ExitGames.Client.Photon;
using System;
using System.Collections.Generic;
namespace PhotonTestClient
{
/// <summary>
/// 客户端这边监听服务器的消息
/// </summary>
class ChatServerListener : IPhotonPeerListener
{
public bool isConnected = false;
public void DebugReturn(DebugLevel level, string message)
{
}
public void OnEvent(EventData eventData)
{
}
public void OnMessage(object messages)
{
}
/// <summary>
/// 得到服务器端响应
/// </summary>
/// <param name="operationResponse"></param>
public void OnOperationResponse(OperationResponse operationResponse)
{
Dictionary<byte, object> dict = operationResponse.Parameters;
object temp = null;
dict.TryGetValue(1, out temp);
Console.WriteLine(temp.ToString());
}
/// <summary>
/// 服务器连接状态变化时,这里响应服务器的变化
/// </summary>
/// <param name="statusCode"></param>
public void OnStatusChanged(StatusCode statusCode)
{
switch (statusCode)
{
case StatusCode.Connect:
isConnected = true;
Console.WriteLine("连接成功");
break;
default:
break;
}
}
}
class Program
{
static void Main(string[] args)
{
ChatServerListener listener = new ChatServerListener();
PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Tcp);
//本机IP地址"127.0.0.1"记得带上端口号,+Application的名字
//这里把一个connect请求加入队列.
peer.Connect("127.0.0.1:4530", "PhotonTest");
while (listener.isConnected==false)
{
peer.Service();//PhotonPeer维护一个队列,Service调用的时候,如果队列中有请求,侧发送这个请求
}
Dictionary<byte, object> dict = new Dictionary<byte, object>();
dict.Add(1, "username");
dict.Add(2, "password");
peer.OpCustom(1, dict, true);//发起一个请求,请求是异步创建的,所以不能马上调用Service
while (true) { peer.Service(); }//不停调用Service,如果队列中有东西,则响应这个请求.
}
}
}
涂鸦:
欢迎交流 : D:workhai@Outlook.com