# Hara [![Gem Version](https://badge.fury.io/rb/hara.png)](http://badge.fury.io/rb/hara) [![Build Status](https://travis-ci.org/jjyr/hara.png?branch=master)](https://travis-ci.org/jjyr/hara) Hara is a websocket based application framework, build upon [em-websocket](https://github.com/igrigorik/em-websocket). * Simple, easy to use. * OO, Actor model(Celluloid). * Event-io(em-websocket). ## Installation Add this line to your application's Gemfile: gem 'hara' And then execute: $ bundle Or install it yourself as: $ gem install hara ## Basic Usage *server* ```ruby #test.rb require 'hara' class Echo include Hara::App define_action :echo do |str| send_msg str end end Hara::Server.start 'localhost', '3000' ``` *client* ```javascript var msg = JSON.stringify({action: 'echo',args:['hello world']}) var ws = new WebSocket('ws://localhost:3000') ws.onmessage = function(msg){alert(msg.data)} //after a while ws.send(msg) //hello world ``` *start server* `ruby test.rb` `ruby test.rb -h` to view options ## Full Usages ```ruby require 'hara' class Echo #include Hara::App make your Echo class become Celluloid::Actor, #hara use actor per connection include Hara::App #Hara::App provide some callbacks def after_connect puts 'first called' p headers end def before_action action, *args puts 'called when action comming' end define_action :echo do |str| puts "#{client_ip} #{client_port}" #send message to client send_msg str end define_action :exit do puts "#{client_ip} exit" # close client connection close end def after_action action, *args puts 'called when action complete' end def action_missing action, *args send_msg 'error' super end def on_close close_info puts "#{client_ip} connection closed" end end # you can pass some options to start server_options = { #...some options, same as EM::Websocket.run } Hara::Server.start 'localhost', '3000', server_options # Hara::Filter # Filter can help you filter some connections before dispatched to app actor. # Example: use Filter to authentication require 'cgi/cookie' class Echo include Hara::App #..some code end #class name is not matter class Authentication include Hara::Filter # You must implement filter method, return value should be ture or false def filter # You can use some helper methods(headers, client_ip, send_msg...), just like Hara::App CGI::Cookie.parse(headers['cookie'])['foo'] == ['bar'] end end Hara::Server.start 'localhost', '3000' ``` ## Client js client is processing current format is JSON.stringify({action: 'echo',args:['hello world']}) ## Contributing 1. Fork it 2. Feel free to send pull requests