![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/lib/gems/2.5.0/gems/eventmachine-1.2.7/examples/old/ |
Upload File : |
require File.dirname(__FILE__) + '/helper' EM.run do # Create a channel to push data to, this could be stocks... RandChannel = EM::Channel.new # The server simply subscribes client connections to the channel on connect, # and unsubscribes them on disconnect. class Server < EM::Connection def self.start(host = '127.0.0.1', port = 8000) EM.start_server(host, port, self) end def post_init @sid = RandChannel.subscribe { |m| send_data "#{m.inspect}\n" } end def unbind RandChannel.unsubscribe @sid end end Server.start # Two client connections, that just print what they receive. 2.times do EM.connect('127.0.0.1', 8000) do |c| c.extend EM::P::LineText2 def c.receive_line(line) puts "Subscriber: #{signature} got #{line}" end EM.add_timer(2) { c.close_connection } end end # This part of the example is more fake, but imagine sleep was in fact a # long running calculation to achieve the value. 40.times do EM.defer lambda { v = sleep(rand * 2); RandChannel << [Time.now, v] } end EM.add_timer(5) { EM.stop } end