A Question About OpRaiseEvent and OnEvent

version ; Photon-Server-SDK_v3-4-31-10808
//--------------------------------------------------OpRaiseEvent Code-----------------------------
 if (GUI.Button(new Rect(Screen.width / 2 + 150, 200, 50, 30), "Send"))//SendMsg
        {
        Hashtable chatContent = new Hashtable {{cvEvent.Key.ChatMsg, chatMsg}};
         peer.OpRaiseEvent((byte) cvEvent.Code.Chat, chatContent,true);
        }
//--------------------------------------------------OnEvent Code-----------------------------------------
public void OnEvent(EventData eventData)
    {
        Debug.Log("触发了事件:" + eventData.ToStringFull());
        
        string username; //接收到的用户名
        int userId; //接收到的用户的编号
        userId = (int) eventData.Parameters[LiteEventKey.ActorNr]; 
        switch (eventData.Code)
        {
                case (byte) cvEvent.Code.Chat:
                Hashtable customEventContent = eventData.Parameters[LiteEventKey.Data] as Hashtable;
                string chatMsg = customEventContent[(byte)cvEvent.Key.ChatMsg].ToString();
                msg = userDic[userId] + ":" + chatMsg;
                  break;
          }
        Debug.Log(msg);
    }
//---------------------------------------------------------------------------------------------------------------
I want to build a chat room .but some user can't get the msg;
for example :



1.user1 enter room ;
2.user2 enter room;
3.user3 enter room;
4.user3 send msg ,user 1 and user 2 both receive the msg.
5.user2 send msg , user 1 can receive the msg,but user 3 cant ;
6.user1 send msg , user 2 and user3 cant receive the mag;

Can U tell me what is wrong? thank U so much!!


//----------------------------------------------------Full Code ---------------------------------------------------------


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


public class GameClient : MonoBehaviour,IPhotonPeerListener {
    public void DebugReturn(DebugLevel level, string message)
    {
       Debug.Log("Level:"+level+ "  Msg:" + message);
    }//debug

    public void OnStatusChanged(StatusCode statusCode)
    {
        switch (statusCode)
        {
            case StatusCode.Connect:
                Debug.Log("Connect Succuse");
                break;
            case StatusCode.Disconnect:
                Debug.Log("DisConnect/Connect Fail ");
                break;
            case StatusCode.Exception://异常
                Debug.Log("Connect Error");
                break;
        }
    }//连接状态改变

    public void OnEvent(EventData eventData)
    {
        Debug.Log("触发了事件:" + eventData.ToStringFull());
        
        string username; //接收到的用户名
        int userId; //接收到的用户的编号
        userId = (int) eventData.Parameters[LiteEventKey.ActorNr]; //获取用户ID
        switch (eventData.Code)
        {
            case LiteEventCode.Join:
                if (!userDic.ContainsKey(userId)) //如果不存在用户ID
                {
                    username =
                        ((Hashtable) eventData.Parameters[LiteEventKey.ActorProperties])[(byte) cvEvent.Key.UserName]
                        .ToString(); //获取用户名
                    userDic.Add(userId, username); //加入到玩家字典
                    msg = "玩家编号为:" + userId + "的玩家:" + userDic[userId] + "进入房间";
                }
                else
                {
                    msg = "已存在玩家编号为" + userId + "的用户,名字为:" + userDic[userId];
                }
                break;
            case LiteEventCode.Leave:
                msg = "玩家编号为:" + userId + "的玩家:" + userDic[userId] + "离开房间";
                userDic.Remove(userId); //移除玩家字典里的玩家信息
                break;
            case LiteEventCode.PropertiesChanged:
                break;
            case (byte) cvEvent.Code.Chat:
                Hashtable customEventContent = eventData.Parameters[LiteEventKey.Data] as Hashtable;
                string chatMsg = customEventContent[(byte)cvEvent.Key.ChatMsg].ToString();
                if (userDic.ContainsKey(userId)) //如果存在用户ID
                {
                  msg = userDic[userId] + ":" + chatMsg;
                }
                break;
            default:
                break;
        }
        Debug.Log(msg);
    } //响应事件

    public void OnOperationResponse(OperationResponse operationResponse) //接受服务器返回信息
    {
        switch (operationResponse.OperationCode)
        {
            case LiteOpCode.Join:
                int playerId = (int)operationResponse.Parameters[LiteOpKey.ActorNr];
                Debug.Log("玩家编号为:" + playerId + "进入房间");
                break;
            case LiteOpCode.Leave:
                Debug.Log("玩家退出房间");
                break;
          default:
                ExcuteMsg(operationResponse);//执行游戏指令信息
                break;
        }
    }

    void ExcuteMsg(OperationResponse operationResponse)//执行游戏指令信息
    {

    }

  
    
    private LitePeer peer;
    private string myName= "Input name";//玩家名
    private string msg = "";//输出显示事件信息
    private string chatMsg = "";//聊天信息
    private byte channel = 0;
    private int[] userIdList;
    private Dictionary<int,string> userDic=new Dictionary<int, string>();//用于存放用户编号和名字

    public bool isBroadCast=true;
    // Use this for initialization
    void Start ()
    {
        peer=new LitePeer(this,ConnectionProtocol.Udp);//,端口5055要和服务器配置文件的端口一样
        peer.Connect("127.0.0.1:5055", "Lite");//连接服务器的
        
    }
	
	// Update is called once per frame
	void Update ()
    {
		peer.Service();//监听端口获取数据;
	}

    void SendMsg2Server(OperationRequest operationRequest)
    {
       // Debug.Log(operationRequest.Parameters.ContainsKey(LiteOpKey.GameId));
        //Debug.Log(operationRequest.OperationCode);
        peer.OpCustom(operationRequest, true, channel, false);//可信信道发送请求到服务器;
        
    }


    void OnGUI()
    {
        myName = GUI.TextField(new Rect(Screen.width / 2 - 50, 15, 100, 30), myName); //输入姓名
      
        GUI.Label(new Rect(Screen.width / 2 - 50, 150, 400, 50), msg); //显示信息

        chatMsg = GUI.TextField(new Rect(Screen.width / 2 - 150, 200, 300, 30), chatMsg); //聊天框

        if (GUI.Button(new Rect(Screen.width / 2 + 150, 200, 50, 30), "Send"))//发送聊天框内容
        {
        Hashtable chatContent = new Hashtable {{cvEvent.Key.ChatMsg, chatMsg}};
            if (chatMsg=="")
            {
                return;
            }

            peer.OpRaiseEvent((byte) cvEvent.Code.Chat, chatContent,true);

            msg = myName + ":" + chatMsg;
            chatMsg = "";
        }


            if (GUI.Button(new Rect(Screen.width / 2 - 50, 50, 100, 30), "EnterRoom"))
        {
            #region 简化前 用PhotonPeer

            //  OperationRequest operationRequest = new OperationRequest {OperationCode = LiteOpCode.Join};
            //  Dictionary<byte, object> para = new Dictionary<byte, object> {{LiteOpKey.GameId, "1"}};
            //operationRequest.Parameters = para;
            //  SendMsg2Server(operationRequest);


            #endregion

            //简化后用LitePeer
            Hashtable gameProperties = new Hashtable(), actorProperties = new Hashtable();
            actorProperties.Add((byte) cvEvent.Key.UserName, myName);
            peer.OpJoin("游戏名字/房间名字", gameProperties, actorProperties, isBroadCast /*是否广播玩家属性*/);
        }


        if (GUI.Button(new Rect(Screen.width / 2 - 50, 100, 100, 30), "LeaveRoom"))
        {
            peer.OpLeave();
           
        }
    }


    
}

Answers

  • edited June 15
    public static class cvEvent
    {
        public enum Code : byte
        {
            Chat=10
        }
    
        public enum Key : byte
        {
            UserName = 11,ChatMsg=12
        }
    
    }

  • JohnTubeJohnTube mod
    edited June 16
    Hi @SheepJeffery,

    Thank you for choosing Photon!

    I will not answer your question directly.

    Any reason you are using old Lite and not LoadBalancing?
    Here is an updated "Hello World" for Photon Server 4 but it really should just be used for a starting point to learn more about LoadBalancing.
Sign In or Register to comment.