Last active
April 17, 2017 01:57
-
-
Save okunishinishi/4626e0b571f760340cd49688a33d1985 to your computer and use it in GitHub Desktop.
[SUGOS] チュートリアル06 - Observerを使ってみる ref: http://qiita.com/okunishinishi@github/items/256e997d124abf527bb2
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-06 | |
cd sugos-tutorial-06 | |
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 sugo-observer 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
#!/usr/bin/env node | |
/** | |
* Use sugo observer | |
* @see https://github.com/realglobe-Inc/sugo-observer#readme | |
*/ | |
'use strict' | |
const sugoObserver = require('sugo-observer') | |
const asleep = require('asleep') | |
const co = require('co') | |
co(function * () { | |
let observer = sugoObserver(({ event, data }) => { | |
switch (event) { | |
case 'actor:setup': { | |
let { key } = data | |
console.log(`New actor joined: ${key}`) | |
break | |
} | |
case 'caller:join': { | |
let { actor } = data | |
console.log(`New called joined to actor: ${actor.key}`) | |
break | |
} | |
default: | |
break | |
} | |
}, { | |
host: 'localhost:3000' | |
}) | |
yield observer.start() // Start observing | |
/* ... */ | |
yield asleep(200000) | |
yield observer.stop() // Stop observing | |
}).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 ./observer.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 sugoActor = require('sugo-actor') | |
const { Module } = sugoActor | |
const co = require('co') | |
const asleep = require('asleep') | |
co(function * () { | |
let actor = sugoActor({ | |
host: 'localhost:3000', | |
key: 'my-actor-06', | |
/** Modules to load */ | |
modules: { | |
tableTennis: new Module({ | |
ping (message = '') { | |
// Just wait 500ms and return pong. | |
return co(function * () { | |
yield asleep(500) | |
return `pong! ${message}` | |
}) | |
} | |
}) | |
} | |
}) | |
// 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 sugo caller | |
* @see https://github.com/realglobe-Inc/sugo-caller#readme | |
*/ | |
'use strict' | |
const sugoCaller = require('sugo-caller') | |
const co = require('co') | |
co(function * () { | |
let caller = sugoCaller({ | |
host: 'localhost:3000' | |
}) | |
// Connect to actor | |
let myActor06 = yield caller.connect('my-actor-06') | |
let tableTennis = myActor06.get('tableTennis') | |
let pong = yield tableTennis.ping('hello world!') | |
console.log(`Pong from myActor06/tableTennis: "${pong}"`) | |
}).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