Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4019.00元
photon server无法发送数据给客户端
Draco发起了问答2017-02-09
7
回复
2602
浏览
服务器部分

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老师指导一下smiley 谢谢

所有回复
  • siki 2017-02-09
     while (true)
                {
                    peer.Service();
                }

    放到单独的线程里面,然后1秒执行60次就行了

    • Draco 2017-02-09

      @m@ 还是一样.... Not authorized.... 想问一下siki老师,做一个实时射击类游戏的话,服务器用photon好呢?还是自己写socket好呢? 外面的游戏公司企业或独立游戏开发者一般是怎么选的呢?

      (1) 回复
    • siki 2017-02-09

      回复 @ Draco: 有能力就自己写,想快速开发的话就用photon 想快速开发还是用photon 特别大型的公司比如LOL还是自己写

      (1) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
  • 大苏打 2017-12-26

     

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 大苏打 2017-12-26
    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)
            {
              
            }
        }
    }
    

     

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 大苏打 2017-12-26
    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();
                }
            }
           }
        }
    
    

     

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 大苏打 2017-12-26

    弄个一个月还是没有找到原因,老师能百忙之中给我解惑吗?@m@

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!