In this article, Eventbus implementation-Publish subscription-XML load is only applicable to the scope of the event propagation, if it involves more than one server between the event propagation is not, the solution is useful MSMQ solution, node.js solution, also useful Redis release subscription solution, This time with C # socket to implement, can implement immediately push events to all subscribed to the relevant event server.
This subsystem applies to the following scenario:
It is mainly divided into 2 parts: event bus broker used by each server and event bus server.
The communication protocol between broker and server is 3: ME, Subscribe, Publish. Separate representative: My name is, I want to subscribe to the event is, I triggered the event.
Event bus server is based on the SuperSocket open source component written, very convenient. 3 corresponding command classes were implemented (we recommend that you take a look at the SuperSocket documentation) as follows:
public class Me:commandbase<eventdispatcherbussession, stringrequestinfo> {public override void Exec Utecommand (eventdispatcherbussession session, Stringrequestinfo Requestinfo) {session.
associatedserveridentity = EventBase.Helper.RestoreSpecialCharacters (Requestinfo.parameters[0]); Console.WriteLine (String. Format ("[{0}]: Connected.", session.
associatedserveridentity)); } public class Publish:commandbase<eventdispatcherbussession, stringrequestinfo> {public O verride void ExecuteCommand (eventdispatcherbussession session, Stringrequestinfo Requestinfo) {string
Evtclasspath = EventBase.Helper.RestoreSpecialCharacters (Requestinfo.parameters[0]);
String evtxml = EventBase.Helper.RestoreSpecialCharacters (requestinfo.parameters[1]); foreach (Eventdispatcherbussession s in session). Appserver.getallsessions ()) {if (s.associateDserveridentity = Session.
Associatedserveridentity) continue; if (S.subscribedeventclasses.contains (Evtclasspath)) {s.send ("Publish" + requestinfo . body);//forward only Console.WriteLine (string. Format ("Forwarding Publish command from {1} to server: [{0}]", S.associatedserveridentity, session.
associatedserveridentity)); }}} public class Subscribe:commandbase<eventdispatcherbussession, stringrequestinfo&
Gt
{public override void ExecuteCommand (eventdispatcherbussession session, Stringrequestinfo Requestinfo) {
String classPath = EventBase.Helper.RestoreSpecialCharacters (Requestinfo.parameters[0]); if (session).
Subscribedeventclasses.contains (ClassPath)) return; Session.
Subscribedeventclasses.add (ClassPath);
String msg = ""; Msg+=striNg. Format (' [{0}], now subscribed these events:\r\n ', session.
associatedserveridentity); foreach (String Evtclass in session. subscribedeventclasses) msg + string.
Format ("{0}\r\n", evtclass);
Console.WriteLine (msg); }
}