JavascriptEventBus (event Bus) simulates event processing in socket. io

Source: Internet
Author: User
Tags eventbus
Socket. the io client is very elegant in event processing, which is similar to weboscket's limited javascript interface, but supports more custom events: varsocketio. connect (& amp; #39; http: // localhost: 9000/chat & amp; #39;); socket. on (& amp; #39; conn.

The socket. io client is very elegant in event processing, which is similar to weboscket's limited javascript interface, but supports more custom events:

Var socket = io. connect ('HTTP: // localhost: 9000/chat ');

Socket. on ('connect ', function (){
// Your code here
});

Socket. on ('announcement ', function (msg ){
// Your code here...
});

Socket. on ('nicknames', function (nicknames ){
// Your code here...
});

View rawgistfile1.jsThis Gist brought to you by GitHub.
EventBus can be used to decouple event Subscriber/event publishers. If the publisher does not know the subscriber, the subscriber only needs to register the subscriber and wait for the notification. EventBus is a simple, efficient, elegant, and good client architecture. Well, fortunately, javascrui itself supports passing functions as parameters, which is not too troublesome.

Building the simplest EventBus javascript library is not difficult:

Yongboy = {};

Yongboy. eventbus = {
Listeners :{
List :{},
Add: function (event, fn ){
This. list [event] = fn;
},
Remove: function (event ){
This. list [event] = null;
}
},

Subscribe: function (event, fn ){
This. listeners. add (event, fn );
},

// Simulate the event interface of socket. io client
On: function (event, fn ){
This. subscribe (event, fn );
},

Broadcast: function (event ){
If (! This. listeners. list [event])
Return;
Var funcHolder = this. listeners. list [event];
If (! FuncHolder)
Return;

FuncHolder. apply (this, []. slice. call (arguments, 1 ));
},

Unsubscribe: function (event ){
This. listeners. remove (event );
}
};
View rawyongboy. eventbus. jsThis Gist brought to you by GitHub.
With less than 40 lines of code, it provides event subscription, event cancellation, event broadcast/publishing, etc. Although simple, it has already met the simplest page-side EventBus model and can provide a full view.

The client uses the event bus code:





Javascript EventBus Example

Related Article

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.