ブライアン、em-http-requestライブラリ(https://github.com/igrigorik/em-http-request)を使用できます。これにより、A。同じサーバーで実行されている特定のEventMachineアプリケーションを参照できます。B 。別のサーバー、またはC.本当に必要な場所。
require 'eventmachine'
require 'em-http-request'
require 'sinatra/base'
require 'thin'
class ServerClass < EventMachine::Connection
def initialize(*args)
# ruby singleton - store channel data in global hash
($channels ||= [])
end
def post_init
puts "initialized"
$cb.call("initialized");
end
def receive_data(data)
# got information from client connection
end
def channel_send(msg,channel)
$channels[channel].send_data(msg)
end
def channels_send(msg)
$channels.each{|channel| channel.send_data(msg)}
end
def unbind
# puts user left
end
end
EventMachine.run do
$cb = EM.callback {|msg| puts msg #do something creative}
$ems = EventMachine::start_server('0.0.0.0',ServerClass,args)
class App < Sinatra::Base
set :public, File.dirname(__FILE__) + '/public'
get '/' do
erb :index
end
end
App.run!({:port => 3000})
end
上記は基本的なワイヤーフレームです。データの送信方法に応じて、WebSocket(em-websocket)を使用して、ログイン時に各ユーザーをバインドするか(ログインシステムを追加する必要があります)、これを何にでも使用できます。Eventmachineオブジェクト(接続、WebSocket、チャネル)へのグローバル参照がある限り、アプリケーション内からメッセージを渡すことができます。
ところで-Thinはとにかくこれを行うので、EventMachine.run do;....endループを追加することはオプションです。しかし、それがどのように機能するかを知るのに役立ちます。
幸運を