Last active
August 29, 2015 14:08
-
-
Save thash/fa7e9030f3273d823a86 to your computer and use it in GitHub Desktop.
AllAbout 022 EventMachine
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
require 'bundler/setup' | |
Bundler.require | |
require 'open-uri' | |
EM.run do | |
puts "main: #{Thread.current}" | |
operation = -> do | |
puts "defer operation: #{Thread.current}" | |
open('http://allabout.co.jp/'){|f| f.read.scan(/<title>(.*)<\/title>/) } | |
end | |
callback = -> (result) do | |
puts "callback: #{Thread.current}" | |
puts result | |
end | |
puts '--- calling defer' | |
EM.defer(operation, callback) | |
puts '--- called defer' | |
EM.stop | |
end |
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
require 'bundler/setup' | |
Bundler.require | |
class Dice | |
include EM::Deferrable | |
def throw | |
rand(1..6) | |
end | |
end | |
EM.run do | |
dice = Dice.new | |
# 成功時の処理を登録 | |
dice.callback do |n| | |
puts "it's odd: #{n}" | |
end | |
# 失敗時の処理を登録 | |
dice.errback do |n| | |
puts "it's even: #{n}" | |
end | |
case num = dice.throw | |
when 1,3,5 | |
dice.succeed(num) # diceオブジェクトに成功状態をセット | |
when 2,4,6 | |
dice.fail(num) # diceオブジェクトに失敗状態をセット | |
end | |
EM.stop | |
end |
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
source 'https://rubygems.org' | |
gem 'eventmachine' |
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
$ ruby defer.rb | |
main: #<Thread:0x007fe2e18cb790> | |
--- calling defer | |
--- called defer | |
defer operation: #<Thread:0x007fe2e20d7970> | |
callback: #<Thread:0x007fe2e18cb790> | |
All About(オールアバウト) |
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
$ ruby deferrable.rb | |
it's odd: 5 | |
$ ruby deferrable.rb | |
it's even: 2 |
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
$ ruby server.rb | |
running echo server on 8081 | |
### 別コンソールから8081に接続, 話しかける | |
$ telnet 127.0.0.1 8081 | |
hi | |
>>> echo: hi |
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
require 'bundler/setup' | |
Bundler.require | |
module EchoServer | |
def post_init | |
puts '-- connection established' | |
end | |
def receive_data(data) | |
send_data ">>> echo: #{data}" | |
end | |
end | |
EventMachine::run { | |
EventMachine::start_server '127.0.0.1', 8081, EchoServer | |
puts 'running echo server on 8081' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment