Skip to content

Instantly share code, notes, and snippets.

@okunishinishi
Last active February 7, 2017 06:37
Show Gist options
  • Save okunishinishi/c3032759d0f6dd5e05b53ea1d545025e to your computer and use it in GitHub Desktop.
Save okunishinishi/c3032759d0f6dd5e05b53ea1d545025e to your computer and use it in GitHub Desktop.
[SUGOS] チュートリアル01 - Hello Worldしてみる ref: http://qiita.com/okunishinishi@github/items/65c0336b0aee9c29cc65
mkdir sugos-tutorial-01
cd sugos-tutorial-01
npm init -y
npm install sugo-actor sugo-caller sugo-hub co asleep -S
#!/usr/bin/env node
/**
* Start hub server
* @see https://github.com/realglobe-Inc/sugo-hub
*/
'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)
})
node ./hub.js
/**
* Sample of module with simple call-return function
* @module tableTennis
* @see https://github.com/realglobe-Inc/sugo-module-base#usage
*/
'use strict'
const { Module } = require('sugo-actor')
const co = require('co')
const asleep = require('asleep')
// Create a new module
const tableTennis = new Module({
ping (message = '') {
// Just wait 500ms and return pong.
return co(function * () {
yield asleep(500)
return `pong! ${message}`
})
}
})
module.exports = tableTennis
#!/usr/bin/env node
/**
* Connect actor
*/
'use strict'
const sugoActor = require('sugo-actor')
const co = require('co')
const tableTennis = require('./modules/table-tennis')
co(function * () {
let actor = sugoActor({
host: 'localhost:3000',
key: 'my-actor-01',
/** Modules to load */
modules: {
tableTennis
}
})
// Connect to hub
yield actor.connect()
console.log(`Actor connected to: ${actor.socket.io.uri}`)
}).catch((err) => console.error(err))
node ./actor.js
#!/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 myActor01 = yield caller.connect('my-actor-01')
let tableTennis = myActor01.get('tableTennis')
let pong = yield tableTennis.ping('hello world!')
console.log(`Pong from myActor01/tableTennis: "${pong}"`)
}).catch((err) => console.error(err))
node ./caller.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment