Unity3d客户端与PhotonServer通信

先打开Unity3d,在Assert下创建一个Plugins文件夹,将PhotonServer SDK文件夹下lib下的Photon3Unity3D.dll拖到Plugins文件夹下


服务器端和客户端之间的关系图:

创建一个空物体,名字为PhotonEngine,创建一个脚本,名字为PhotonEngine:

using ExitGames.Client.Photon;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhotonEngine : MonoBehaviour,IPhotonPeerListener {

    private static PhotonEngine Instance;
    private static PhotonPeer peer;
    public static PhotonPeer Peer
    {
        get { return peer; }
    }
    

    void Awake()
    {
        if(Instance==null)   //单例模式的处理,只有一个Instance
        {
            Instance = this;
            DontDestroyOnLoad(this.gameObject);     //不销毁这个物体
            
        }
        else if(Instance!=this)
        {
            Destroy(this.gameObject);return;       //删除多余的PhotonEngine
        }
    }
	// Use this for initialization
	void Start ()
    {
        //通过Listender接收服务器端的响应
        peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055", "MyGame1");      //连接服务器端
	}
	
	// Update is called once per frame
	void Update ()
    {
        //if(peer.PeerState==PeerStateValue.Connected)  //判断peer已经连接
        //{
            peer.Service();  //什么状态下都要执行
        //}
       
	}
    void OnDestroy()
    {
        if(peer!=null && peer.PeerState==PeerStateValue.Connecting)
        {
            peer.Disconnect();            //断开连接
        }
    }

    public void DebugReturn(DebugLevel level, string message)
    {
        
    }

    //处理服务器端直接发送过来的信息
    public void OnEvent(EventData eventData)
    {
        switch(eventData.Code)
        {
            case 1:
                Dictionary<byte,object> data = eventData.Parameters;
                object intValue;
                data.TryGetValue(1, out intValue);
                object stringValue;
                data.TryGetValue(2, out stringValue);
                Debug.Log("接收服务器直接发送来的信息:"+intValue.ToString()+stringValue.ToString());
                break;
            case 2:
                break;
            default:
                break;
        }
    }

    //接收服务器发来的响应
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        switch(operationResponse.OperationCode)
        {
            case 1:
                Debug.Log("接收一个来自服务器的响应");
                Dictionary<byte,object> data = operationResponse.Parameters;
                object intValue;
                data.TryGetValue(1, out intValue);
                object stringValue;
                data.TryGetValue(2, out stringValue);
                Debug.Log("接收来自服务器端的响应数据:" + intValue.ToString() + stringValue.ToString());
                break;
            case 2:
                break;
            default:
                break;
        }
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
        Debug.Log(statusCode);
    }
}


创建一个Test脚本,放在PhotonEngine物件上:

public class test : MonoBehaviour {

	
	// Update is called once per frame
	void Update ()
    {
		if(Input.GetMouseButtonDown(0))
        {
            SendRequest();
        }
	}
    void SendRequest()
    {
        Dictionary<byte, object> data = new Dictionary<byte, object>();
        int intValue=100;               //向服务器端发送数据
        data.Add(1, intValue);
        string stringValue = "hfasfadh滑石粉哈";
        data.Add(2, stringValue);
        PhotonEngine.Peer.OpCustom(1, data, true);     //向服务器端发送信息
    }
}

在服务器端的ClientPeer类中实现通信:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

namespace MyGameServer
{
    public class ClientPeer : Photon.SocketServer.ClientPeer
    {

        public ClientPeer(InitRequest initRequest):base(initRequest)
        {

        }
        //处理客户端断开连接的后续工作
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            
        }
        //处理客户端的请求
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch(operationRequest.OperationCode) //通过opCode区分
            {
                case 1:
                    MyGameServer.log.Info("接收了一个客户端的请求");
                    Dictionary<byte,object> data= operationRequest.Parameters;
                    object intValue;
                    data.TryGetValue(1, out intValue);    //得到客户端发送的数据
                    object stringValue;
                    data.TryGetValue(2, out stringValue);
                    MyGameServer.log.Info("接收来自客户端的数据:" + intValue.ToString() + stringValue.ToString());

                    OperationResponse opResponse = new OperationResponse(1);
                    Dictionary<byte, object> data2 = new Dictionary<byte, object>();
                    int intValue2 = 100;
                    string stringValue2 = "asfaasdf发了疯has";
                    data2.Add(1, intValue2);
                    data2.Add(2, stringValue2);
                    opResponse.SetParameters(data2);           //向客户端回应数据
                    SendOperationResponse(opResponse, sendParameters);   //向客户端发出响应

                    EventData ed = new EventData(1);
                    Dictionary<byte, object> data3 = new Dictionary<byte, object>();
                    int intValue3 = 200;
                    data3.Add(1, intValue3);
                    string stringValue3 = "fdasf飞杰";
                    data3.Add(2, stringValue3);
                    ed.SetParameters(data3);
                    SendEvent(ed, new SendParameters());      //直接向客户端发送信息
                    
                    break;
                case 2:
                    break;
                default:
                    break;
            }
        }
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值