ตัวอย่าง shooting_star observer ที่หามานาน

หลังจาก ประสบปัญหาว่า จะรู้ว่า user online และ offline จากห้องแชทได้ยังไง

ในที่สุดก็ไปเจอมาแล้ว ที่นี่ หรือว่า ที่นี่

โค้ดเป็นตามนี้

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require File.dirname(__FILE__) + '/../config/environment'
require 'drb/drb'

class ChatObserver
include DRb::DRbUndumped
def name; 'simple_chat/chatroom observer' end
def enter(params)
puts "ChatObserver:"
puts params.inspect
end
def leave(params)
puts "ChatObserver:"
puts params.inspect
end
end

Meteor::shooter.observe('<strong>chat1</strong>', ChatObserver.new) # สมมติว่า channel ชื่อ chat1 ละกัน [ ของในตัวอย่างมันเป็น simple_chat/chatroom ตัวหลังเดาว่าเป็น tag (ไม่ชัวร์นะ) ]
puts 'ChatObserver installed successfully.'
puts '[ PRESS ENTER KEY TO EXIT ]'
gets

นอกจากนี้ยังมีการเขียน observer ที่มีประโยชน์อยู่ในเทสของเวอร์ชั่น 3.2.0

อยู่ใน test/lib/shooting_star_test.rb

require File.join(File.dirname(__FILE__), '../test_helper')
require 'shooting_star'
require 'socket'
require 'thread'

$command_line = 'echo "testing"'

class ShootingStarTest &lt; Test::Unit::TestCase
class TestObserver
include DRb::DRbUndumped
def name; 'test-observer' end
def enter(params) @params = params end
def params; @params end
end

def setup
@config = ShootingStar.configure :silent =&gt; true,
:pid_file =&gt; 'tmp/pids/shooting_star.test.pid',
:log_file =&gt; 'log/shooting_star.test.log',
:server =&gt; {:host =&gt; '127.0.0.1', :port =&gt; 8081},
:shooter =&gt; {:uri =&gt; 'druby://127.0.0.1:7124'}
mutex = Mutex.new
mutex.lock
@thread = Thread.new{ShootingStar.start{mutex.unlock}}
mutex.lock
@query = "sig=0123456789&amp;execute=http://127.0.0.1:4001/meteor/strike"
@query2 = "sig=1123456789&amp;execute=http://127.0.0.1:4001/meteor/strike"
end

def teardown
ShootingStar.stop
File.rm_f(@config.pid_file)
File.rm_f @config.log_file
@thread.join
end

def test_connection_with_invalid_method
client = TCPSocket.open('127.0.0.1', 8081)
assert_not_nil client
send(client, "GET", "test/channel", @query)
assert client.read.empty?
end

def test_connection
client = TCPSocket.open('127.0.0.1', 8081)
send(client, "POST", "test/channel", @query)
assert_not_nil result = client.read
assert_not_nil result.index('xhr.getResponseHeader')
assert_not_nil result.index('test\/channel')
client.close

mutex = Mutex.new
mutex.lock
Thread.new do
client = TCPSocket.open('127.0.0.1', 8081)
send(client, "POST", "test/channel", "#{@query}&amp;__t__=c")
mutex.unlock
end
mutex.lock
shooter = DRbObject.new_with_uri('druby://127.0.0.1:7124')
assert_not_nil shooter
shooter.shoot("test/channel", 12, [])
assert_not_nil result = client.read
assert_not_nil result.index('meteor/strike/12')
end

def test_multi_user_communication
client1 = TCPSocket.open('127.0.0.1', 8081)
client2 = TCPSocket.open('127.0.0.1', 8081)
assert_not_nil client1
assert_not_nil client2
shooter = DRbObject.new_with_uri('druby://127.0.0.1:7124')
assert_not_nil shooter
observer = TestObserver.new
assert_not_nil observer
shooter.observe('test/channel', observer)
mutex = Mutex.new
mutex.lock
assert_nil observer.params
Thread.new do
send(client1, "POST", "test/channel", "#{@query}&amp;__t__=c")
mutex.unlock
end
mutex.lock
assert_nil observer.params
Thread.new do
send(client2, "POST", "test/channel", "#{@query2}&amp;__t__=c")
mutex.unlock
end
mutex.lock
assert_not_nil observer.params
assert_equal :enter, observer.params[:event]
assert_not_nil result1 = client1.read
assert_not_nil result1.index('meteor/strike/event-')
shooter.shoot("test/channel", 12, [])
assert_not_nil result2 = client2.read
assert_not_nil result2.index('meteor/strike/12')
end

def test_xmlsocket_server
client = TCPSocket.open('127.0.0.1', 8081)
client.write("&lt;policy-file-request/&gt;")
assert_not_nil client.read.index('allow-access-from')
end

def test_shooter_exists
shooter = ShootingStar.shooter
assert_not_nil shooter
end

def test_c10k_problem
bin = File.join(RAILS_ROOT, 'bin/test_c10k_problem')
src = File.join(File.dirname(__FILE__), 'test_c10k_problem.c')
if !File.exist?(bin) || File.mtime(src) &gt; File.mtime(bin)
system "gcc #{src} -o #{bin}"
end
system bin
end

private
def send(client, method, path, body)
client.write "#{method} /#{path} HTTP/1.1\n\r" +
"Host: #{@config.server.host}:#{@config.server.port}\n\r" +
"Keep-Alive: 300\n\r" +
"Content-length: #{body.length}\n\r" +
"Connection: keep-alive\n\r\n\r#{body}"
end
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s