README.md in hara-0.2.2 vs README.md in hara-0.3.0
- old
+ new
@@ -1,18 +1,16 @@
# 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 simple framework, help you build async-io & concurrent websocket server.
+Hara is a websocket based application framework, build upon [em-websocket](https://github.com/igrigorik/em-websocket).
-Hara's async-io ability from [em-websocket](https://github.com/igrigorik/em-websocket),
+* Simple, easy to use.
+* OO, Actor model(Celluloid).
+* Event-io(em-websocket).
-Hara's concurrent ability from [celluloid](https://github.com/celluloid/celluloid).
-
-Seems eventmachine and celluloid work well together :smile:
-
## Installation
Add this line to your application's Gemfile:
gem 'hara'
@@ -23,30 +21,32 @@
Or install it yourself as:
$ gem install hara
-## BasicUsage
+## Basic Usage
*server*
```ruby
#test.rb
require 'hara'
-class Echo
+class Echo
include Hara::App
define_action :echo do |str|
- socket.send 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:3210')
+var ws = new WebSocket('ws://localhost:3000')
ws.onmessage = function(msg){alert(msg.data)}
//after a while
ws.send(msg)
//hello world
@@ -58,15 +58,15 @@
`ruby test.rb -h` to view options
## Full Usages
```ruby
-require 'hara/base'
+require 'hara'
-class Echo
- #include Hara::App, make you Echo class become Celluloid::Actor,
- # and per actor handle a socket
+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
@@ -78,12 +78,12 @@
puts 'called when action comming'
end
define_action :echo do |str|
puts "#{client_ip} #{client_port}"
- #send message to socket
- socket.send str
+ #send message to client
+ send_msg str
end
define_action :exit do
puts "#{client_ip} exit"
# close client connection
@@ -93,19 +93,46 @@
def after_action action, *args
puts 'called when action complete'
end
def action_missing action, *args
- socket.send 'error'
+ send_msg 'error'
super
end
def on_close close_info
puts "#{client_ip} connection closed"
end
end
-#if you require 'hara/base' you need start server you self, like below
+# 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