how to send it

Options
mad_sir1
mad_sir1 ✭✭
edited July 2013 in Photon Server
Hi guys!
How to send a message to all players ?? I modify my server side code like this ,but it does't work:

In LiteApplication.cs :
private static readonly HashSet<PeerBase> PEER = new HashSet<PeerBase >();
public void Remove(LitePeer peer)
{
lock (PEER)
{
PEER.Remove(peer);
}
}

public void SendWorldMessage(EventData eventd)
{
lock (PEER)
{
ApplicationBase.Instance.BroadCastEvent(eventd,PEER, new SendParameters());
}
}

protected override PeerBase CreatePeer(InitRequest initRequest)
{
LitePeer peer= new LitePeer(initRequest.Protocol, initRequest.PhotonPeer);
PEER.Add (peer);//this operation I can get the all the peer connect to the photon server
return peer;
}
:?
and then In LitePeer.cs:
case OperationCode.LobbyMessageOperation:
{
LiteApplication kk = new LiteApplication();
var informa = new Dictionary<byte, object>();
informa.Add((byte)ParameterKey.lobbymessage, info);
eventdate = new EventData((byte)EventCode.LobbyMessage, informa);
kk.SendWorldMessage(eventdate);
}
break;

but I can't receive the event on the client side ,what something else I have missed ?? or anyone can give a successfull sample code ,I have confused on it many days , :cry: anyone can help me??

Comments

  • The problem is that you create an instance of the LiteApplication in your peer class - the instance gets disposed immediately afterwards and does not have a chance to send out the event.

    You need to pass a reference to the application into your peer:

    [code2=csharp]protected override PeerBase CreatePeer(InitRequest initRequest)
    {
    LitePeer peer= new LitePeer(initRequest.Protocol, initRequest.PhotonPeer, this);
    PEER.Add (peer);//this operation I can get the all the peer connect to the photon server
    return peer;
    }[/code2]

    ... and store the "application" reference in a local variable of your peer and use that one to broadcast the event.
  • mad_sir1
    Options
    Thanks Nicole ,you say store the application?? but how to use a local variable to store the application ??I don't quite understand ?can you give me some example code ?? thanks !
  • Please get yourself a good book about C# programming and read about variables & scope. We can't give extensive support about basic programming techniques.

    [code2=csharp]public class LitePeer : PeerBase
    {

    private LiteApplication application;

    public LitePeer(IRpcProtocol rpcProtocol, IPhotonPeer nativePeer, LiteApplication app)
    : base(rpcProtocol, nativePeer)
    {
    this.application = app;
    }
    }[/code2]
  • mad_sir1
    Options
    Oh ,NIcole ,I sorry to tell you ,your method is the same to me ,serveral days ago ,I have try it ,I am sorry that I didn't have give you the code that I have store the application , so I just want to know you how to store the application,but your method is the same to me , this method can't get the event ,and it lead a problem that when I join the room ,the logs show ERROR Lite.Room [(null)] - System.NullReferenceException: 未将对象引用设置到对象的实例。I think I should give the all code for guys ,everyone can see it ,and what is the problem?
    in the LiteApplication.cs:
    public static readonly HashSet<PeerBase> PEER = new HashSet<PeerBase>();
    protected override PeerBase CreatePeer(InitRequest initRequest)
    {
    LitePeer peer= new LitePeer(initRequest.Protocol, initRequest.PhotonPeer ,this);
    PEER.Add (peer);
    return peer;
    }
    :|
    In the LItePeer.cs:
    private LiteApplication yingyong;
    public LitePeer(IRpcProtocol rpcProtocol, IPhotonPeer nativePeer,LiteApplication aaaaaa)
    : base(rpcProtocol, nativePeer)
    {
    this.yingyong = aaaaaa;

    }

    case OperationCode.LobbyMessageOperation:
    string message1 = (string)operationRequest.Parameters[(byte)ParameterKey.lobbymessage];
    var info = new Dictionary<byte, object>();
    info.Add((byte)ParameterKey.lobbymessage, message1);
    var informa = new Dictionary<byte, object>();
    informa.Add((byte)ParameterKey.lobbymessage, info);
    var eventdate = new EventData((byte)EventCode.LobbyMessage, informa);
    yingyong.BroadCastEvent(eventdate,LiteApplication.PEER,sendParameters);
    return;
    :roll:
    In the LIteLobbyPeer.cs:
    public LiteLobbyPeer(IRpcProtocol rpcProtocol, IPhotonPeer photonPeer)
    : base(rpcProtocol, photonPeer ,new LiteApplication() )
    {

    } :?:
    Yes these are all my code ,Nicole ,I'm sorry that I haven't give you all the code first time ,now ,this time I give the all codes, but I can't get what problem is happen ,I can't get the event, so,anyone has advice or what server side code I should modify?? :P
  • Your code to broadcast the event looks fine now.

    I wouldn't create a new LiteApplication object in the LiteLobbyPeer constructor, just pass the LiteLobbyApplication (it inherits from LiteApplication). But that should not be the reason for any errors.

    For the NullReferenceException, please attach a debugger and step through your code, or check the line number yourself. In general, if something does not work as you expect, please try to debug yourself - check if the peer lists contain the peers you expect, etc.
  • mad_sir1
    Options
    Hi Nicole thanks for your quickly reply!, as the code you see ,If these code:

    LitePeer peer= new LitePeer(initRequest.Protocol, initRequest.PhotonPeer );
    public LitePeer(IRpcProtocol rpcProtocol, IPhotonPeer nativePeer)
    public LiteLobbyPeer(IRpcProtocol rpcProtocol, IPhotonPeer photonPeer)
    : base(rpcProtocol, photonPeer )

    then everything is ok ,but when I add these code:

    LitePeer peer= new LitePeer(initRequest.Protocol, initRequest.PhotonPeer ,this);
    public LitePeer(IRpcProtocol rpcProtocol, IPhotonPeer nativePeer,LiteApplication aaaaaa)
    public LiteLobbyPeer(IRpcProtocol rpcProtocol, IPhotonPeer photonPeer)
    : base(rpcProtocol, photonPeer ,new LiteApplication() )
    then the openlogs show System.NullReferenceException: 未将对象引用设置到对象的实例。so it means that these codes have problem ,but I have debug it ,every object isn't null , maybe some where I have lost ??or any methods to modify??if I solve the problem ,I think I can broast the event!
  • As I said: I wouldn't create a new LiteApplication object in the LiteLobbyPeer constructor, just pass the LiteLobbyApplication (it inherits from LiteApplication).

    [code2=csharp]public LiteLobbyPeer(IRpcProtocol rpcProtocol, IPhotonPeer photonPeer, LiteApplication app)
    : base(rpcProtocol, photonPeer, app) { .. }[/code2]

    I can't read that chinese log message, sorry, you need to debug the NullReferenceException yourself.
  • mad_sir1
    Options
    Thanks Nicole ,I think I should find another way to sovle this problem ,anyway,I should thank you for your help !
    by the way:未将对象设置引用到对象的实例,it means Object reference not set to an instance of an object...!