Hara
Hara is a simple framework, help you build async-io & concurrent websocket server.
Hara's async-io ability from em-websocket,
Hara's concurrent ability from celluloid.
Seems eventmachine and celluloid work well together :smile:
Installation
Add this line to your application's Gemfile:
gem 'hara'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hara
BasicUsage
server
#test.rb
require 'hara'
class Echo
include Hara::App
define_action :echo do |str|
socket.send str
end
end
client
var msg = JSON.stringify({action: 'echo',args:['hello world']})
var ws = new WebSocket('ws://localhost:3210')
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
require 'hara/base'
class Echo
#include Hara::App, make you Echo class become Celluloid::Actor,
# and per actor handle a socket
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 socket
socket.send 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
socket.send '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
Hara::Server.start 'localhost', '3000'
Client
js client is processing
current format is JSON.stringify('echo',args:['hello world'])
Contributing
- Fork it
- Feel free to send pull requests