Code Monkey home page Code Monkey logo

em-xs's Introduction

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

em-xs's People

Contributors

schmurfy avatar

Stargazers

Luke Chamberlain avatar Chris Duncan avatar

Watchers

 avatar Luke Chamberlain avatar James Cloos avatar  avatar

em-xs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.