forked from fullstackers/bus.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcept.js
40 lines (31 loc) · 1.05 KB
/
concept.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var io = require('socket.io')()
, socketMessages = require('socket-messages').make()
, messageExchange = require('message-exchange').make()
;
messageExchange.handler.on('say', function (message, exchange) {
exchange.channel(message.target).publish(message);
});
messageExchange.on('message', function (message) {
this.publish(message);
});
socketMessages.exchange(messageExchange).action('say').attach(io);
io.on('connection', function (socket) {
function handle (message) {
socket.emit.apply(socket, [message.action, message.actor].concat(message.content));
}
messageExchange.channel(socket.id).on('message', handle);
socket.on('disconnect', function () {
messageExchange.channel(socket.id).removeListener('message', handle);
});
});
io.listen(3000);
setTimeout(function () {
var i = require('socket.io-client')('http://localhost:3000/');
i.on('connect', function () {
i.on('say', function (who, what) {
console.log(who + ' said "' + what + '" to me');
process.exit();
});
i.emit('say', 'hello');
});
},1000);