服务器部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
namespace ChatServer
{
class ChatServer:ApplicationBase
{
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new ChatPeer(initRequest);
}
protected override void Setup()
{
}
protected override void TearDown()
{
}
}
}
__________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace ChatServer
{
class ChatPeer : 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, "siki");
OperationResponse response = new OperationResponse(1, dict);
SendOperationResponse(response, new SendParameters());
}
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
}
}
______________________________________________________________________________________________
客户端部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ExitGames.Client.Photon;
namespace ChatServerClient
{
class ChatServerListener : IPhotonPeerListener
{
public bool isConnected = false;
//Debug,日志的回调,输出
public void DebugReturn(DebugLevel level, string message)
{
}
//发送请求时,服务器响应,服务端调用
public void OnEvent(EventData eventData)
{
}
//发送请求时,服务器响应,服务端调用
public void OnOperationResponse(OperationResponse operationResponse)
{
Dictionary<byte, object> dict = operationResponse.Parameters;
object value = null;
dict.TryGetValue(1, out value);
if (value == null)
{
Console.WriteLine("error code:" + operationResponse.ReturnCode + " error message:" + operationResponse.DebugMessage);
}
else
{
Console.WriteLine("Get value form server: " + value.ToString());
}
}
//状态改变的时候调用,连接,断开,出错时
public void OnStatusChanged(StatusCode statusCode)
{
switch (statusCode)
{
case StatusCode.Connect:
isConnected = true;
Console.WriteLine("Connected.");
break;
}
}
public void OnMessage(object messages)
{
}
}
class Program
{
static void Main(string[] args)
{
ChatServerListener listener = new ChatServerListener();
PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Tcp);
peer.Connect("127.0.0.1:4531", "ChatServer"); //连接服务器
Console.WriteLine("Connecting ...");
while (!listener.isConnected)
{
peer.Service();//Photon的机制是不会立即发出请求,会放在队列里面,只有调用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();
}
}
}
}
因为近期需要做到服务器部分,想了解一下,所以在看siki老师的泰斗破环神中photon部分,照着课程打代码后无法得到服务器发送过来的消息。源码如上。
运行结果如下
配置服务器方面没有问题,能够成功连接上,但是就是不能从服务器那边发消息给客户端 。。。
刚开始接触photon不太熟悉,谷歌搜索了code -3 和 Not authorized 都找不到合适的解决方法。
电脑也重启过...搞了一整天没能解决...
请siki老师指导一下 谢谢
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
{
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, "siki");
OperationResponse response = new OperationResponse(1, dict);
SendOperationResponse(response,sendParameters);
}
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
}
}
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)
{
ChatServerListener listener = new ChatServerListener();
PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Tcp);
peer.Connect("127.0.0.1:4530","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();
}
}
}
}