最近题主在研究PhotonServer如何实现联网功能时,在搭建服务器与客户端时遇到了许多小问题,题主在网上参照了siki老师的photonServer搭建教程,但是遇到的问题却很多,原因是siki老师所讲的版本是几年前的版本,所以跟现在的新版本是有不同之处的,也由于题主没有网络通信的基础,所以一直折腾了我不少时间,现在终于搞定了整个流程,现在我来为大家分享一下2017年最新的PhotonServer搭建教程。
客户端 GameDemoChatClient
using ExitGames.Client.Photon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameDemoChatClient //客户端
{
class ChatServerListener : IPhotonPeerListener
{
public bool isConnected = false;
public void DebugReturn(DebugLevel level, string message)
{
}
public void OnEvent(EventData eventData) //返回给客户端时调用
{
//throw new NotImplementedException();
}
public void OnOperationResponse(OperationResponse operationResponse) //得到服务器端的响应
{
Dictionary<byte, object> dict = operationResponse.Parameters;
object v = null;
dict.TryGetValue(1,out v);
if (v == null)
{
Console.WriteLine("error code:" + operationResponse.ReturnCode + " error message:" + operationResponse.DebugMessage);
}
else
{
Console.WriteLine("从服务端得到数据:" + v.ToString());
}
}
public void OnMessage(object messages)
{
}
public void OnStatusChanged(StatusCode statusCode) //客户端连接服务端成功时调用
{
switch (statusCode)
{
case StatusCode.Connect:
isConnected = true;
Console.WriteLine("连接成功!");
break;
}
}
}
class Program
{
static void Main(string[] args)
{
string localhost = "127.0.0.1:4530";
string ChatServer = "ChatServer";
ChatServerListener listener = new ChatServerListener();
PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Tcp);
peer.Connect(localhost, ChatServer); //链接服务器
Console.WriteLine("连接服务器中......");
while (listener.isConnected == false)
{
peer.Service();
}
Dictionary<byte, object> dict = new Dictionary<byte, object>();
dict.Add(1,"username");
dict.Add(2, "password");
//向服务端发送请求
peer.OpCustom(1,dict,true);
while (true) {
peer.Service();
}
}
}
}
服务端:ChatPeer
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PhotonHostRuntimeInterfaces;
using Photon.SocketServer.Rpc;
namespace ChatServer
{
class ChatPeer : ClientPeer //新版本这个继承ClientPeer
{
public ChatPeer(InitRequest initRequest) : base(initRequest)
{
}
//当客户端发起请求的时候调用
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
Dictionary<byte, object> dict = new Dictionary<byte, object>();
dict.Add(1, "sucess");
OperationResponse response = new OperationResponse(1, dict);
SendOperationResponse(response,sendParameters);
}
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
}
}
PhotonServer.config配置文件(重点)
这段代码写在PhotonServer.config的语句块里,如图:
<ChatServer DisplayName="Chat Server">
<TCPListeners>
<TCPListener
IPAddress="0.0.0.0"
Port="4530"
OverrideApplication="ChatServer">
>
TCPListener>
TCPListeners>
<Runtime
Assembly="PhotonHostRuntime, Culture=neutral"
Type="PhotonHostRuntime.PhotonDomainManager"
UnhandledExceptionPolicy="Ignore">
Runtime>
<Applications Default="ChatServer">
<Application
Name="ChatServer"
BaseDirectory="ChatServer"
Assembly="ChatServer"
Type="ChatServer.ChatServer"
>Application>
Applications>
ChatServer>
ChatServer
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChatServer
{
public class ChatServer : ApplicationBase //抽象类
{
protected override PeerBase CreatePeer(InitRequest initRequest) //客户端连接我们的服务端时调用这个方法
{
return new ChatPeer(initRequest);
}
protected override void Setup()
{
}
protected override void TearDown()
{
throw new NotImplementedException();
}
}
}
配置完config文件后,启动PhotonServer,可以看到新的ChatServer的Application连接,启动成功。
测试连接成功!
在这里,我给大家展示一下常见的启动错误,例如:
这都是因为PhotonServer.config配置文件不对的问题!,按照我的新版本的配置即可解决问题!
下面进行unity的测试:
首先在PhotonServer的lib文件下面导入Photon3Unity3D.dll文件到unity中。
挂载到任意GameObject上
PhotonServerEngine脚本
using UnityEngine;
using System.Collections;
using ExitGames.Client.Photon;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class PhotonServerEngine : MonoBehaviour,IPhotonPeerListener {
private PhotonPeer peer;
private bool isconnect = false;
void Start()
{
peer = new PhotonPeer(this, ConnectionProtocol.Tcp);
peer.Connect("127.0.0.1:4530", "ChatServer");//连接服务器
}
void Update()
{
peer.Service();
}
void OnGUI()
{
if (isconnect)
{
if (GUILayout.Button("Send"))
{
Dictionary<byte, object> dict = new Dictionary<byte, object>();
dict.Add(1, "username");
dict.Add(2, "password");
peer.OpCustom(1, dict, true);
}
}
}
public void DebugReturn(DebugLevel level, string message)
{
Debug.Log(level + ":" + message);
}
public void OnEvent(EventData eventData)
{
}
public void OnOperationResponse(OperationResponse operationResponse)
{
Dictionary<byte, object> dict = operationResponse.Parameters;
object val = null;
dict.TryGetValue(1, out val);
Debug.Log("getserver" + val.ToString());
}
public void OnStatusChanged(StatusCode statusCode)
{
switch (statusCode)
{
case StatusCode.Connect:
isconnect = true;
Debug.Log("Connect");
break;
}
}
}
欢迎你关注我的博客。