Created
April 11, 2012 13:27
-
-
Save pechorin/2359289 to your computer and use it in GitHub Desktop.
faye em
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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