Project

em-xs

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
EventMachine wrapper for crossroads
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 1.0.0.beta4
 Project Readme

em-xs

Crossroads EventMachine library, I used em-zeromq as base but turned it into something slightly different.

Prerequesites

You need to have libxs installed, on mac os x you can use homebrew:

brew install crossroads

Example

I decided to go for a long example to be closer than a "real" application, here it is:
you can view the source here too

require 'rubygems'
require 'bundler/setup'

require 'em-xs'

ENDPOINT = 'inproc://socket'


class Client
  def initialize(context, endpoint, delay)
    @context = context
    @endpoint = endpoint
    @delay = delay
    
    @socket = @context.socket(XS::REQ, self)
    if @socket.connect(endpoint) == -1
      raise XS::Util.error_string
    end
    
    send_query
  end
  
  def on_readable(s, parts)
    puts "Client received: #{parts}"
    send_query
  end
  
private
  def send_query
    EM::add_timer(@delay) do
      @socket.send_msg("ping")
    end
  end
  
end


class EchoServer
  def initialize(context, endpoint)
    @context = context
    @socket = @context.socket(XS::REP, self)
    if @socket.bind(endpoint) == -1
      raise XS::Util.error_string
    end
  end
  
  def on_readable(s, parts)
    puts "Server received: #{parts}"
    s.send_msg("re: #{parts[0]}")
  end
  
end


EM::run do
  context = EM::XS::Context.new
  
  EchoServer.new(context, ENDPOINT)
  Client.new(context, ENDPOINT, 1)
end

Contributing

first install dependencies (thanks bundler !)

bundle

then you can run the specs:

bundle exec rake

and if you can to do some changes you can start guard to
watch the files and restart associated specs in the background:

bundle exec guard