Last active
February 7, 2017 06:44
-
-
Save okunishinishi/5cda37a5b209dcf92e7c161a57f6d4bd to your computer and use it in GitHub Desktop.
[SUGOS] チュートリアル02 - Event Emitしてみる ref: http://qiita.com/okunishinishi@github/items/a25e5b89cf7505302b13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir sugos-tutorial-02 | |
cd sugos-tutorial-02 | |
npm init -y | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install sugo-actor sugo-caller sugo-hub co asleep -S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict' | |
const sugoHub = require('sugo-hub') | |
const co = require('co') | |
co(function * () { | |
let hub = sugoHub({}) | |
yield hub.listen(3000) | |
console.log(`SUGO Cloud started at port: ${hub.port}`) | |
}).catch((err) => { | |
console.error(err) | |
process.exit(1) | |
}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node ./hub.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sample of module with event emitting | |
* @module tableTennis | |
*/ | |
'use strict' | |
const { Module } = require('sugo-actor') | |
const co = require('co') | |
const asleep = require('asleep') | |
const timeBomb = new Module({ | |
countDown (count) { | |
const s = this | |
return co(function * () { | |
let abort = () => { count = -1 } | |
s.on('abort', abort) // Listen to events from the caller | |
while (count > 0) { | |
count-- | |
s.emit('tick', { count }) // Emit an event to the caller | |
yield new Promise((resolve) => | |
setTimeout(() => resolve(), 1000) | |
) | |
} | |
s.off('abort', abort) // Remove event listener | |
let isAborted = count === -1 | |
return isAborted ? 'hiss...' : 'booom!!!' | |
}) | |
} | |
}) | |
module.exports = timeBomb | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict' | |
const sugoActor = require('sugo-actor') | |
const co = require('co') | |
const timeBomb = require('./modules/time-bomb') | |
co(function * () { | |
let actor = sugoActor({ | |
host: 'localhost:3000', | |
key: 'my-actor-02', | |
/** Modules to load */ | |
modules: { | |
timeBomb | |
} | |
}) | |
// Connect to hub | |
yield actor.connect() | |
console.log(`Actor connected to: ${actor.socket.io.uri}`) | |
}).catch((err) => console.error(err)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node ./actor.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict' | |
const sugoCaller = require('sugo-caller') | |
const co = require('co') | |
co(function * () { | |
let caller = sugoCaller({ | |
host: 'localhost:3000' | |
}) | |
// Connect to actor | |
let myActor02 = yield caller.connect('my-actor-02') | |
let timeBomb = myActor02.get('timeBomb') | |
let tick = (data) => console.log(`tick: ${data.count}`) | |
timeBomb.on('tick', tick) // Add listener | |
let booom = yield timeBomb.countDown(10) | |
console.log(booom) | |
timeBomb.off('tick', tick) // Remove listener | |
}).catch((err) => console.error(err)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node ./caller.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment