TaidouChatServerClient(客户端)

来源:互联网 发布:centos ftp客户端命令 编辑:程序博客网 时间:2024/06/06 04:17

需添加引用:"ExitGamesLibs.dll" "Photon.SocketServer.dll" "PhotonHostRuntimeInterfaces.dll"" Photon3DotNet.dll"





using ExitGames.Client.Photon;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TaidouChatServerClient  
{


    class ChatServerListener : IPhotonPeerListener  //实现 接口IPhotonPeerListener
    {

        public bool isConnected = false;

      //进行一些Debug的回掉和Debug的输出 日志的输出

        public void DebugReturn(DebugLevel level, string message)
        {


        }

    //服务器端进行回应时,服务器端进行调用的  OnEvent和OnOperationResponse
        public void OnEvent(EventData eventData)
        {


        }


        public void OnMessage(object messages)
        {


        } 


        //得到服务器端的响应
        public void OnOperationResponse(OperationResponse operationResponse)
        {
            Dictionary<byte, object> dict = operationResponse.Parameters;//.Parameters就是我们的字典参数
            object v = null;
            dict.TryGetValue(1, out v);
            Console.WriteLine("Get value from server:" + v.ToString());
        }

    //表示客户端和服务器端连接状态发生变化时 调用的
        public void OnStatusChanged(StatusCode statusCode)
        {
            Console.WriteLine(statusCode);
            switch (statusCode)
            {
                case StatusCode.Connect:
                    isConnected = true;
                    Console.WriteLine("Connect .");
                    break;
            }
        }
    }


//用peer向服务器端发起请求,用ChatServerListener(也就是listener)去响应服务器端的请求

    class Program
    {
        static void Main(string[] args)
        {
            ChatServerListener listener = new ChatServerListener();  创建一个监听器listener
            PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Tcp); /*利用PhotonPeer与服务器端进行交互     监听类是用来监听服务器返回的一些方法,返回的一些消息的,需要一个IPhotonPeerListener,所以创建一个监听类ChatServerListener 实现接口IPhotonPeerListener*/
            peer.Connect("127.0.0.1:4530", "ChatServer");//连接服务器,需要时间
            Console.WriteLine("Connecting ... ");
            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);//OpCustom用来向服务器端发起一个请求    true表示发起的请求可靠  

//需要一些时间,故后边不能立即跟peer.Service();



            while (true)
            {
                peer.Service();
            }


        }
    }
}
0 0
原创粉丝点击