2
我需要實現一個只寫入數據的服務器,不會收到它。我發現的所有eventmachine服務器示例總是讓服務器先接收數據,然後用數據進行響應。我需要它在客戶端連接後纔開始將數據寫入客戶端。如何讓一個eventmachine服務器只寫數據?
我試着只是在post_init中放置一個循環,但似乎沒有工作...客戶端連接,服務器寫入,但客戶端似乎從來沒有收到任何東西。建議?
測試服務器:
require 'rubygems'
require 'eventmachine'
require 'time'
module TestServer
def post_init
puts "-- client connected, sending data --"
while true do
send_data "Hello from TestServer\n"
puts "sent #{Time.now.iso8601}"
end
end
end
EventMachine::run {
EventMachine::start_server "127.0.0.1", 4001, TestServer
puts 'running test server on 4001'
}
測試客戶端:
require 'rubygems'
require 'eventmachine'
module Forwarder
def post_init
puts "-- connected to server --"
end
def receive_data data
# none of the following is ever output
puts "in receive_data"
puts data
end
end
EventMachine::run {
EventMachine::connect '127.0.0.1', 4001, Forwarder
}
謝謝...