Git Product home page Git Product logo

em_json_connection's Introduction

em_json_connection

This tiny gem adds a JSON layer to a plain Eventmachine socket connection. Normally when you use socket connections it’s your job to implement a protocol that defines a) where messages end and b) how messages are encoded. em_json_connection adds Yajl powered JSON en- and decoding to the game so all you do is writing stuff to the socket thats JSON encodable (Strings, Arrays, Hashes) and implement a callback on the other side that’s called each time Yajl has decoded a complete JSON entity.

See example.rb for… well… an example. Note the minimal setup for client and server classes:

Server:

module Server
  include EM::JsonConnection::Server
  
  def json_parsed(hash)
    puts "Client sent: #{hash}"
    
    send_data hash
  end
end

Sending data is simply done with the known Eventmachines #send_data method.

Client:

module Client
  include EM::JsonConnection::Client
  
  def json_parsed(hash)
    puts "Server responded: #{hash}"
  end
end

Running the server:

Then you start the server in your Eventmachine eventloop like this:

EM.run do
  Server.start_at '/tmp/foo'
  
  EM.add_timer(5) do
    puts 'stopping server'
    EM.stop
  end
end

Running the client:

EM.run do
  c = Client.connect_to '/tmp/foo'
  
  EM.add_periodic_timer 1 do
    c.send_data({:pid => Process.pid})
  end
  
  EM.add_timer(5) do
    puts 'stopping client'
    EM.stop
  end
end

Easy as pie! Have fun!

em_json_connection's People

Contributors

niko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.