Continue to the last article said ~
Receive broadcasts from other people and respond in onevent
For example, receiving messages from the party.
Public voidOnEvent (EventData EventData) {Debug.Log ("The event was triggered:"+eventdata.tostringfull ()); //handling events forwarded back by the server Switch(eventdata.code) { CaseLiteeventcode.join:intActornr = (int) Eventdata.parameters[liteeventkey.actornr]; Debug.Log ("The player's number is:"+Actornr); Break; } }
This message is a system-given code that can be used in Liteeventkey, and what do we do with the custom data?
Case (byte) Opcodeenum.gettag: = (Hashtable) eventdata.parameters[liteeventkey.data]; int tag = (int) message[(byte) Opkeyenum.tag]; break;
Opcodeenum and Opkeyenum are my custom enumerations, and our custom data exists in Liteeventkey.data, which is not playing, it's just a hash table we're transmitting, and the real data is in this hash table.
The data processing in Onoperationresponse is the same as before. It's not going to be explained here.
Let's talk about a more troubled question, for a designated mass. This time we do not have the lite way, we write a designated mass.
Bloggers here have two methods for reference, if there is a more brilliant method please advise.
First of all, it's easier to understand.
When a player is connected to a server, a new peer is created in the application, so that application is in charge of the global. This allows us to use a dictionary to record each peer for a simple mass operation. The code is added as follows in application
Publicdictionary<int, mypeer> Peer =Newdictionary<int, mypeer>(); protected Overridepeerbase Createpeer (initrequest initrequest) {//establish the connection and pass it back to Photon ServerMypeer Mypeer=NewMypeer (Initrequest.protocol, Initrequest.photonpeer); Peer.add (Mypeer. ConnectionID, Mypeer); returnMypeer; } Public voidradioevent () {Operationresponse or=NewOperationresponse (); foreach(Mypeer Mypeerinchpeer.values) {or. ReturnCode=0; Or. DebugMessage="";//return MessageOr. Operationcode = (byte) Opcodeenum.login;//CodingMypeer. Sendoperationresponse (Or,Newsendparameters ()); } }
Now let's take a look at the second method, when we use the first method, we will find a problem, how can we divide the room like lite. Specify the broadcast (if someone can solve the problem thanks to share), and how the client sends the message to the server when it is processed on the peer side, however, how to call the application side of the mass is also a problem.
I use the following method to solve:
namespacechat{ Public classChatpeer:peerbase {Private Static ReadOnly ObjectSyncRoot =New Object(); PublicChatpeer (Irpcprotocol protocol,iphotonpeer unmanagedpeer):Base(protocol, unmanagedpeer) {Lock(syncRoot) {broadcastmessage+= This. Onbroadcastmessage; } } Private Static EventAction<chatpeer, EventData, sendparameters>Broadcastmessage; protected Override voidOnDisconnect (Disconnectreason Reasoncode,stringreasondetail) { Lock(syncRoot) {broadcastmessage-= This. Onbroadcastmessage; } } protected Override voidonoperationrequest (operationrequest operationrequest, sendparameters sendparameters) {var@event =NewEventData (1) {Parameters =Operationrequest.parameters}; Lock(syncRoot) {broadcastmessage ( This, @event, sendparameters); } varResponse =NewOperationresponse (Operationrequest.operationcode); This. Sendoperationresponse (response, sendparameters); } Private voidOnbroadcastmessage (Chatpeer peer, EventData @event, Sendparameters sendparameters) {if(Peer! = This) { This. Sendevent (@event, sendparameters); } } }}
The first is an action delegate (do not know what the delegate means can Baidu a bit)
Private static event Action<chatpeer, EventData, sendparameters> broadcastmessage; Static let him resident memory
Peer is created with a onbriadcastmessage delegate to Brioadcastmessage
When the server receives a bulk message from the client, the delegate is enabled
Broadcastmessage (This, @event, sendparameters);
The delegates in the broadcastmessage are executed once, by
if (peer! = this) {this. Sendevent (@event, sendparameters); }
To control who is sent to the mass. Very powerful method. It seems to be the official demo.
Photon Server advanced & production of a new game (ii)