基于Unity创建PhotonServer服务器一:
2018-02-02 17:22
239 查看
1:新建一个Unity项目并且在项目中新建一个文件夹Plugins将Photon3Unity3D.dll放进这个文件夹
2:在主相机上添加一个名为PhotonServerEngine的脚本
3:打开脚本在Start方法里面new一个PhotonServer并且添加引用并且实现一下IPhotonPeerListener的接口:
public class PhotonServerEngine : MonoBehaviour,IPhotonPeerListener
{
private PhotonPeer peer;
private bool isConnected = false;//查看是否连接成功(监听器)
void Start ()
{
peer = new PhotonPeer(this, ConnectionProtocol.Tcp);//this 表示用当前类充当Listener
peer.Connect("127.0.0.1:4530","ChatServer");
}
void Update ()
{
peer.Service();//每一帧都向服务器发送请求确认一下
}
void OnGUI()
{
if (isConnected)//如果连接成功默认为true
{
if (GUILayout.Button("Send a operation."))//这里创建了一个button 每点击一次执行下面的方法
{
Dictionary<byte, object> dict = new Dictionary<byte, object>()
{
{1,"username" },
{2,"password"}
};
peer.OpCustom(1,dict,true);
注释:peer.OpCustom(第一个参数是操作码,用来区分我们这个请求是用来干嘛的,第二个字典是用来存储我们的一些参数, 第三个参数表示发送的信息是否可靠,如果设置为true会确保发送到服务器端);
}
}
}
public void DebugReturn(DebugLevel level, string message)
{
Debug.Log(level + ":" + message);
}
public void OnEvent(EventData eventData)
9a25
{
throw new System.NotImplementedException();
}
public void OnOperationResponse(OperationResponse operationResponse)
{
Dictionary<byte, object> dict = operationResponse.Parameters;
object v = null;
dict.TryGetValue(1,out v);
Debug.Log("Get value from server."+v.ToString());
}
//客户端和服务器端连接发生变化时调用
public void OnStatusChanged(StatusCode statusCode)
{
switch (statusCode)
{
case StatusCode.Connect:
isConnected = true;
Debug.Log("Connected success.");
break;
}
}
}
2:在主相机上添加一个名为PhotonServerEngine的脚本
3:打开脚本在Start方法里面new一个PhotonServer并且添加引用并且实现一下IPhotonPeerListener的接口:
public class PhotonServerEngine : MonoBehaviour,IPhotonPeerListener
{
private PhotonPeer peer;
private bool isConnected = false;//查看是否连接成功(监听器)
void Start ()
{
peer = new PhotonPeer(this, ConnectionProtocol.Tcp);//this 表示用当前类充当Listener
peer.Connect("127.0.0.1:4530","ChatServer");
}
void Update ()
{
peer.Service();//每一帧都向服务器发送请求确认一下
}
void OnGUI()
{
if (isConnected)//如果连接成功默认为true
{
if (GUILayout.Button("Send a operation."))//这里创建了一个button 每点击一次执行下面的方法
{
Dictionary<byte, object> dict = new Dictionary<byte, object>()
{
{1,"username" },
{2,"password"}
};
peer.OpCustom(1,dict,true);
注释:peer.OpCustom(第一个参数是操作码,用来区分我们这个请求是用来干嘛的,第二个字典是用来存储我们的一些参数, 第三个参数表示发送的信息是否可靠,如果设置为true会确保发送到服务器端);
}
}
}
public void DebugReturn(DebugLevel level, string message)
{
Debug.Log(level + ":" + message);
}
public void OnEvent(EventData eventData)
9a25
{
throw new System.NotImplementedException();
}
public void OnOperationResponse(OperationResponse operationResponse)
{
Dictionary<byte, object> dict = operationResponse.Parameters;
object v = null;
dict.TryGetValue(1,out v);
Debug.Log("Get value from server."+v.ToString());
}
//客户端和服务器端连接发生变化时调用
public void OnStatusChanged(StatusCode statusCode)
{
switch (statusCode)
{
case StatusCode.Connect:
isConnected = true;
Debug.Log("Connected success.");
break;
}
}
}
相关文章推荐
- 从基于服务器的多维数据集创建的本地多维数据集
- 创建vsftpd服务器基于mysql的虚拟用户认证
- 基于 svn 服务器及 cocoapods-repo-svn 插件进行组件化私有库的创建
- 基于Unity的游戏项目客户端服务器寻路同步方案
- 运用Unity的组件创建服务器和客户端,实现游戏网络化
- 为ASP.NET MVC创建一个基于Unity的ControllerFactory
- 基于Actor模式的c#网络游戏服务器的实现和Unity游戏客户端的连接
- 创建同义词-基于远程服务器视图
- 基于WinSvr2012共享文件夹的Hyper-V实时迁移之二文件服务器及迁移用虚拟机的创建
- 基于SuperSocket实现的WebSocket服务器 和Unity中使用Websocket
- Unity3D笔记——解决Unity官方《Tanks》同时启动会创建两个服务器问题
- 基于WinSvr2012共享文件夹的Hyper-V实时迁移之二文件服务器及迁移用虚拟机的创建
- TCP并发服务器(六)——创建线程池,每个线程accept,accept使用互斥锁保护——基于UNP代码
- 创建基于本地协议的git服务器
- 基于tigase服务器的安卓客户端开发---创建聊天
- 基于 HTTP 长连接的“服务器推”技术
- javascript操作写入txt文件及消息: Automation 服务器不能创建对象问题
- 如何创建你自己的Git服务器
- 创建链接服务器
- 创建eclipse项目到git服务器