Distributed Eventbus Socket implementation: Publish Subscription

Source: Internet
Author: User
Tags foreach eventbase socket eventbus

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); }
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.