Skip to content

Instantly share code, notes, and snippets.

@pechorin
Created April 11, 2012 13:27
Show Gist options
  • Save pechorin/2359289 to your computer and use it in GitHub Desktop.
Save pechorin/2359289 to your computer and use it in GitHub Desktop.
faye em
# server at 8081
require 'rubygems'
require 'faye/websocket'
app = lambda do |env|
ws = Faye::WebSocket.new(env)
ws.onmessage = lambda do |event|
puts event.data
if event.data == 'started'
ws.send('ping')
end
end
ws.onclose = lambda do |event|
puts "closed"
ws = nil
end
ws.rack_response
end
Faye::WebSocket.load_adapter('thin')
Rack::Handler::Thin.run(app, :Port => 8081)
# client
require 'faye/websocket'
require 'eventmachine'
EM.run {
ws = Faye::WebSocket::Client.new('ws://0.0.0.0:8081')
ws.onopen = lambda do |event|
ws.send("started")
end
ws.onmessage = lambda do |event|
puts event.data
end
ws.onclose = lambda do |event|
p [:close, event.code, event.reason]
ws = nil
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment