Skip to content

Instantly share code, notes, and snippets.

@carlesso
Created June 27, 2015 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlesso/a61b158a33f450c92c79 to your computer and use it in GitHub Desktop.
Save carlesso/a61b158a33f450c92c79 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'bundler/setup'
require 'faye/websocket'
require 'eventmachine'
require 'json'
require 'pry'
EM.run {
url = ARGV[0]
headers = {'Origin' => 'http://faye.jcoglan.com'}
proxy = {:origin => ARGV[1], :headers => {'User-Agent' => 'Echo'}}
ws = Faye::WebSocket::Client.new(url, nil, :headers => headers, :proxy => proxy)
client_id = nil
ws.onopen = lambda do |event|
p [:open, ws.headers]
ws.send(['daemon.status', {id: 1234, channel: 'daemon', data: {test: 'forse'}}].to_json)
end
ws.onclose = lambda do |close|
p [:close, close.code, close.reason]
EM.stop
end
ws.onerror = lambda do |error|
p [:error, error.message]
end
ws.onmessage = lambda do |message|
json_msg = JSON.parse(message.data) rescue nil
if json_msg
puts
puts 'JSON message is'
p json_msg
puts
if json_msg.first.first == 'client_connected'
client_id = json_msg.first.last['data']['connection_id']
puts "client id is #{client_id}"
end
if json_msg.first.first == 'websocket_rails.ping'
puts "Sending #{['pong', {}].to_json}"
ws.send(['pong', {}].to_json)
end
end
p [:message, message.data]
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment