Skip to content

Instantly share code, notes, and snippets.

@okunishinishi
Last active April 17, 2017 02:46
Show Gist options
  • Save okunishinishi/8a07f866bb94a9228561358682e5dc26 to your computer and use it in GitHub Desktop.
Save okunishinishi/8a07f866bb94a9228561358682e5dc26 to your computer and use it in GitHub Desktop.
[SUGOS] チュートリアル07 - Hubを冗長化する ref: http://qiita.com/okunishinishi@github/items/1fbd70b315fc1baae34c
mkdir sugos-tutorial-06
cd sugos-tutorial-06
npm init -y
npm install sugo-actor sugo-caller sugo-hub sugo-observer co asleep -S
redis-server
#!/usr/bin/env node
'use strict'
const sugoHub = require('sugo-hub')
const co = require('co')
const ports = [ 3000, 3001 ]
co(function * () {
// Start hub server for each ports
for (let port of ports) {
let hub = sugoHub({
storage: {
// Use redis as storage
// See https://github.com/realglobe-Inc/sugo-hub#using-redis-server
redis: {
host: 'localhost',
port: '6379',
db: 1
}
}
})
yield hub.listen(port)
console.log(`SUGO Cloud started at port: ${hub.port}`)
}
}).catch((err) => {
console.error(err)
process.exit(1)
})
node ./hub.js
#!/usr/bin/env node
'use strict'
const sugoActor = require('sugo-actor')
const { Module } = sugoActor
const co = require('co')
const asleep = require('asleep')
const HUB_O1 = 'localhost:3000'
const HUB_O2 = 'localhost:3001'
co(function * () {
const tableTennis = () => new Module({
ping (message = '') {
// Just wait 500ms and return pong.
return co(function * () {
yield asleep(500)
return `pong! ${message}`
})
}
})
// Prepare actors for each hubs
let actors = [
sugoActor({
host: HUB_O1,
key: 'my-actor-07@hub01',
modules: { tableTennis: tableTennis() }
}),
sugoActor({
host: HUB_O2,
key: 'my-actor-07@hub02',
modules: { tableTennis: tableTennis() }
})
]
for (let actor of actors) {
// 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')
const HUB_O1 = 'localhost:3000'
co(function * () {
let caller = sugoCaller({
host: HUB_O1
})
// Try actors on each hub
let actorKeys = [
'my-actor-07@hub01',
'my-actor-07@hub02'
]
for (let actorKey of actorKeys) {
// Connect to actor
let myActor07 = yield caller.connect(actorKey)
let tableTennis = myActor07.get('tableTennis')
let pong = yield tableTennis.ping('hello world!')
console.log(`Pong from ${actorKey}/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