如何配置PhotonServer服务器:https://blog.csdn.net/abaidaye/article/details/132096415
总管理类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhotonServerFirst.Bll
{
public class BLLManager
{
private static BLLManager bLLManager;
public static BLLManager Instance
{
get
{
if(bLLManager == null)
{
bLLManager = new BLLManager();
}
return bLLManager;
}
}
//登录注册管理
public IMessageHandler accountBLL;
private BLLManager()
{
accountBLL = new Account.AccountBLL();
}
}
}
控制层接口
using Net;
namespace PhotonServerFirst.Bll
{
public interface IMessageHandler
{
//处理客户端断开的后续工作
void OnDisconnect(PSpeer peer);
//处理客户端的请求
void OnOperationRequest(PSpeer peer, PhotonMessage message);
}
}
登录注册控制类
using Net;
using PhotonServerFirst.Dal;
namespace PhotonServerFirst.Bll.Account
{
class AccountBLL : IMessageHandler
{
public void OnDisconnect(PSpeer peer)
{
throw new System.NotImplementedException();
}
public void OnOperationRequest(PSpeer peer, PhotonMessage message)
{
//判断命令
switch (message.Command)
{
case MessageType.Account_Register:
Register(peer, message);
break;
case MessageType.Account_Login:
Login(peer, message);
break;
}
}
//注册请求 0账号1密码
void Register(PSpeer peer, PhotonMessage message)
{
object[] objs = (object[])message.Content;
//添加用户
int res = DAlManager.Instance.accountDAL.Add((string)objs[0],(string)objs[1]);
//服务器响应
SendMessage.Send(peer, MessageType.Type_Account, MessageType.Account_Register_Res, res);
}
//登陆请求 0账号1密码
void Login(PSpeer peer, PhotonMessage message)
{
object[] objs = (object[])message.Content;
//登录
int res = DAlManager.Instance.accountDAL.Login(peer, (string)objs[0], (string)objs[1]);
//响应
SendMessage.Send(peer, MessageType.Type_Account, MessageType.Account_Login_res, res);
}
}
}
总数据管理层
using PhotonServerFirst.Bll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhotonServerFirst.Dal
{
class DAlManager
{
private static DAlManager dALManager;
public static DAlManager Instance
{
get
{
if (dALManager == null)
{
dALManager = new DAlManager();
}
return dALManager;
}
}
//登录注册管理
public AccountDAL accountDAL;
private DAlManager()
{
accountDAL = new AccountDAL();
}
}
}
登录注册数据管理层
using PhotonServerFirst.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhotonServerFirst.Dal
{
class AccountDAL
{
///
/// 保存注册的账号
///
private List<AccountModel> accountList = new List<AccountModel>();
private int id = 1;
///
///保存已经登录的账号
///
private Dictionary<PSpeer, AccountModel> peerAccountDic = new Dictionary<PSpeer, AccountModel>();
///
/// 添加账号
///
/// 用户名
///密码
///1 成功 -1账号已存在 0失败
public int Add(string account, string password)
{
//如果账号已经存在
foreach (AccountModel model in accountList)
{
if (model.Account == account)
{
return -1;
}
}
//如果不存在
AccountModel accountModel = new AccountModel();
accountModel.Account = account;
accountModel.Password = password;
accountModel.ID = id++;
accountList.Add(accountModel);
return 1;
}
///
/// 登录账号
///
/// 连接对象
/// 账号
/// 密码
/// 登陆成功返回账号id -1已经登陆 0用户名密码错误
public int Login(PSpeer peer, string account, string password)
{
//是否已经登陆
foreach (AccountModel model in peerAccountDic.Values)
{
if (model.Account == account)
{
return -1;
}
}
//判断用户名密码是否正确
foreach (AccountModel model in accountList)
{
if (model.Account == account && model.Password == password)
{
peerAccountDic.Add(peer, model);
return model.ID;
}
}
return 0;
}
}
}
登录注册层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhotonServerFirst.Model
{
///
/// 账号模型
///
class AccountModel
{
public int ID;
public string Account;
public string Password;
}
}
服务器配置类
using Photon.SocketServer;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
using System.IO;
namespace PhotonServerFirst
{
public class PSTest : ApplicationBase
{
//日志需要的
public static readonly ILogger log = LogManager.GetCurrentClassLogger();
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new PSpeer(initRequest);
}
//初始化
protected override void Setup()
{
InitLog();
}
//server端关闭的时候
protected override void TearDown()
{
}
#region 日志
///
/// 初始化日志以及配置
///
private void InitLog()
{
//日志的初始化
log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = this.ApplicationRootPath + @"\bin_Win64\log";
//设置日志的路径
FileInfo configFileInfo = new FileInfo(this.BinaryPath + @"\log4net.config");
//获取配置文件
if (configFileInfo.Exists)
{
//对photonserver设置日志为log4net
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
XmlConfigurator.ConfigureAndWatch(configFileInfo);
log.Info("初始化成功");
}
}
#endregion
}
}
服务器面向客户端类
using System;
using System.Collections.Generic;
using Net;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using PhotonServerFirst.Bll;
namespace PhotonServerFirst
{
public class PSpeer : ClientPeer
{
public PSpeer(InitRequest initRequest) : base(initRequest)
{
}
//处理客户端断开的后续工作
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
//关闭管理器
BLLManager.Instance.accountBLL.OnDisconnect(this);
}
//处理客户端的请求
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
var dic = operationRequest.Parameters;
//转为PhotonMessage
PhotonMessage message = new PhotonMessage();
message.Type = (byte)dic[0];
message.Command = (int)dic[1];
List<object> objs = new List<object>();
for (byte i = 2; i < dic.Count; i++)
{
objs.Add(dic[i]);
}
message.Content = objs.ToArray();
//消息分发
switch (message.Type)
{
case MessageType.Type_Account:
BLLManager.Instance.accountBLL.OnOperationRequest(this, message);
break;
case MessageType.Type_User:
break;
}
}
}
}
消息类
因为这个类是unity和服务器端都需要有的,所以最好生成为dll
文件放进unity(net3.5以下)
namespace Net
{
public class PhotonMessage
{
public byte Type;
public int Command;
public object Content;
public PhotonMessage() { }
public PhotonMessage(byte type, int command, object content)
{
Type = type;
Command = command;
Content = content;
}
}
//消息类型
public class MessageType
{
public const byte Type_Account = 1;
public const byte Type_User = 2;
//注册账号
public const int Account_Register = 100;
public const int Account_Register_Res = 101;
//登陆
public const int Account_Login = 102;
public const int Account_Login_res = 103;
}
}
发送消息类
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhotonServerFirst
{
class SendMessage
{
///
/// 发送消息
/// summary>
/// 连接对象 param >
/// < param name="type">类型
/// 命令
/// 参数 param >
public static void Send(PSpeer peer, byte type,int command,params object[] objs)
{
Dictionary<byte, object> dic = new Dictionary<byte, object>();
dic.Add(0, type);
dic.Add(1, command);
byte i = 2;
foreach (object o in objs)
{
dic.Add(i++,o);
}
EventData ed = new EventData(0, dic);
peer.SendEvent(ed, new SendParameters());
}
}
}