Find specific connected client by id and send event

Options
MonRoyals
edited March 2012 in Photon Server
Hi ExitGames-Team,

I would like to do the following:

1.) User connects to the server and registers itself with a specific id e.g. "Player1"
2.) This information is stored in a dabase probably (or can it be somewhere in the code for querying?)
3.) A second user "Player2 "connects and the server queries the database for friend users which are currently available. "Player1" is returned
4.) I would like to call an operation or raise an event for "Player1" to inform him that a new friend is online. Is this possible via operations or events? Can operations also be the other way around, that means the server calls the client?

Thanks

Comments

  • ddks
    Options
    Hi there,

    Not part of the exit games team but am doing all the things you are asking for using Photon. Are you wanting sample code or?

    1) when a client connects to a server the it gets a unique connectionid which is maintained by the server (inside code). for example my server has a list of all clients connnected. then when a client peer connects the connection id is stored in this list

    2) I don't store them in a DB but rather link a user-id to the connection-id (so my list above has connection-id, user-id among with several other properties)

    3) as you can see from above this is quite easy. You just need to setup the operation hanlder and return the reponse.

    4) you normally use operations from the client to the server. the server returns a response. The second scenario is where the server sends an event to the client. E.g. if player 1 has joined and player 2 joins as well on the connect of player 2 we can send an event to player 1 (player 2 has joined the game). I don't think a server peer can call an operation on a client. A server peer can call an operation on another server peer (e.g. if you have a master with a sub server like a chat server for all chat operations). There are two kind of peers: server peer and client peer

    Now behind the scenes it can get more tricky depending on the server design. It depends on the type of game you are designing e.g. full blown MMO with interest areas vs limites 10 player games using a join-room design.

    Hope this helps,
    Dan
  • Thanks for the answer. This helps ;). Sample code would be great for getting the connection id which is unique. For the last point sample code would also be great. I'm not sure if I got your point regarding telling player 1 that player 2 has joined via an event. Can I send events to specific connection id's?

    Thanks, this community seems to be great in supporting each other ;)
  • ddks
    Options
    Hey,

    As i mentioned earlier the design of the photon server allows for various models. In my design all my clients connect to master server which acts as a router - directing requests. And in this design the master maintains a list of clients so yes the master can send an event to any client at any time.

    Before sharing some sample code, just would be helpful to know what you need to achieve. This stuff can get pretty complex:
    - a client peer class to receive all requests from clients
    - a master server peer class that is linked to this client peer class but is really the connection to all the sub server peer instances
    - sub server peer classes that are going to be the recipient for various types of operations
    - the actual request handlers which contain the meat for specific operation requests. e.g. the login request would have a loginRequestHandler which checks the password, username and eventually sends a response back the client.

    The above might be total overkill, If you are creating a 10 - 20 players game using the Unity engine you could consider using the Photon Unity Plugin which in combination with the Cloud option takes care of most things that i listed above.

    Dan
  • Hey,
    my plan is to host a root server by my own and to install a single photon server instance on this machine. I can't use the cloud service because I'm planning to use a lot of server code processing. I'm a Marmalade developer so I can't use the Unity plugin ;-).
    I think mantaining a user list with the associated connection id should not be very hard on the server side. And if the server can raise events on a specific connection id, than I have everything I need to implement what is in my mind. I haven't started digging in the server code right now, but this will start today. I think the loginRequestHandler will do everything I need for the user management and also would be the starting point for calling "friends" with previously stored connection ids. Maybe code for calling events for a specific id could help, but as I said I haven't looked into the server implementation right now so probably it's an easy task ;-).

    Thanks
    Damir
  • I've just got my first server application running. From my current knowledge I'm not able to get a peer for an connection id. So my solution would be to save every peer with a unique id in a map. Than it should be no problem to query this map from the new peer to look for the connected friends.
  • ddks
    Options
    A few pointers to help:
    1) your root server should be based on:
    public class RootServer : NodeResolverBase
    2) implement
    protected override PeerBase CreatePeer(InitRequest initRequest)
    which is the core request part. inside here you create the client peer e.g.: return new ClientPeer(initRequest, this); Also the root has the list of client peers (I used a dictionary for that:
    public Dictionary<String, ClientPeer> ConnectedClients { get; protected set; }
    3) ClientPeer is a public class
    ClientPeer : PeerBase and the constructor has:
    public Unity3DPeer(InitRequest request, RootServer rootServer) : base (request.Protocol, request.PhotonPeer)
    {
    _server = rootServer;
    ClientId = new GUID();
    _server.ConnectedClients.Add(ClientId, this);
    }

    4) then inside the clientpeer class there are several overrides
    protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)

    5) make sure the appropiate client dll is added to your client project

    Use the src SDK samples as most of my code is also done picking apart how it is done in there.

    Dan
  • Thanks Dan,

    I think I have all the information I need for implementing that.

    Damir