faye-websocket-0.10.7/0000755000175000017500000000000013425040132015237 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/lib/0000755000175000017500000000000013425040132016005 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/lib/faye/0000755000175000017500000000000013425040132016731 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/lib/faye/websocket/0000755000175000017500000000000013425040132020717 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/lib/faye/websocket/client.rb0000644000175000017500000000471313425040132022527 0ustar utkarsh2102utkarsh2102require 'forwardable' module Faye class WebSocket class Client extend Forwardable include API DEFAULT_PORTS = {'http' => 80, 'https' => 443, 'ws' => 80, 'wss' => 443} SECURE_PROTOCOLS = ['https', 'wss'] def_delegators :@driver, :headers, :status def initialize(url, protocols = nil, options = {}) @url = url super(options) { ::WebSocket::Driver.client(self, :max_length => options[:max_length], :protocols => protocols) } proxy = options.fetch(:proxy, {}) endpoint = URI.parse(proxy[:origin] || @url) port = endpoint.port || DEFAULT_PORTS[endpoint.scheme] @secure = SECURE_PROTOCOLS.include?(endpoint.scheme) @origin_tls = options.fetch(:tls, {}) @socket_tls = proxy[:origin] ? proxy.fetch(:tls, {}) : @origin_tls if proxy[:origin] @proxy = @driver.proxy(proxy[:origin]) if headers = proxy[:headers] headers.each { |name, value| @proxy.set_header(name, value) } end @proxy.on(:connect) do uri = URI.parse(@url) secure = SECURE_PROTOCOLS.include?(uri.scheme) @proxy = nil if secure origin_tls = {:sni_hostname => uri.host}.merge(@origin_tls) @stream.start_tls(origin_tls) end @driver.start end @proxy.on(:error) do |error| @driver.emit(:error, error) end end EventMachine.connect(endpoint.host, port, Connection) do |conn| conn.parent = self end rescue => error emit_error("Network error: #{url}: #{error.message}") finalize_close end private def on_connect(stream) @stream = stream if @secure socket_tls = {:sni_hostname => URI.parse(@url).host}.merge(@socket_tls) @stream.start_tls(socket_tls) end worker = @proxy || @driver worker.start end module Connection attr_accessor :parent def connection_completed parent.__send__(:on_connect, self) end def receive_data(data) parent.__send__(:parse, data) end def unbind(error = nil) parent.__send__(:emit_error, error) if error parent.__send__(:finalize_close) end def write(data) send_data(data) rescue nil end end end end end faye-websocket-0.10.7/lib/faye/websocket/api/0000755000175000017500000000000013425040132021470 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/lib/faye/websocket/api/event.rb0000644000175000017500000000201213425040132023131 0ustar utkarsh2102utkarsh2102module Faye::WebSocket::API class Event attr_reader :type, :bubbles, :cancelable attr_accessor :target, :current_target, :event_phase CAPTURING_PHASE = 1 AT_TARGET = 2 BUBBLING_PHASE = 3 def initialize(event_type, options) @type = event_type options.each { |key, value| instance_variable_set("@#{key}", value) } end def init_event(event_type, can_bubble, cancelable) @type = event_type @bubbles = can_bubble @cancelable = cancelable end def stop_propagation end def prevent_default end end class OpenEvent < Event end class MessageEvent < Event attr_reader :data end class CloseEvent < Event attr_reader :code, :reason end class ErrorEvent < Event attr_reader :message end TYPES = { 'open' => OpenEvent, 'message' => MessageEvent, 'close' => CloseEvent, 'error' => ErrorEvent } def Event.create(type, options = {}) TYPES[type].new(type, options) end end faye-websocket-0.10.7/lib/faye/websocket/api/event_target.rb0000644000175000017500000000275713425040132024517 0ustar utkarsh2102utkarsh2102module Faye::WebSocket::API module EventTarget include ::WebSocket::Driver::EventEmitter events = %w[open message error close] events.each do |event_type| define_method "on#{event_type}=" do |handler| EventMachine.next_tick do flush(event_type, handler) instance_variable_set("@on#{event_type}", handler) end end end def add_event_listener(event_type, listener, use_capture = false) add_listener(event_type, &listener) end def add_listener(event_type, callable = nil, &block) listener = callable || block EventMachine.next_tick do flush(event_type, listener) super(event_type, &listener) end end def remove_event_listener(event_type, listener, use_capture = false) remove_listener(event_type, &listener) end def dispatch_event(event) event.target = event.current_target = self event.event_phase = Event::AT_TARGET listener = instance_variable_get("@on#{ event.type }") count = listener_count(event.type) unless listener or count > 0 event_buffers[event.type].push(event) end listener.call(event) if listener emit(event.type, event) end private def flush(event_type, listener) if buffer = event_buffers.delete(event_type.to_s) buffer.each { |event| listener.call(event) } end end def event_buffers @event_buffers ||= Hash.new { |k,v| k[v] = [] } end end end faye-websocket-0.10.7/lib/faye/websocket/api.rb0000644000175000017500000001021213425040132022011 0ustar utkarsh2102utkarsh2102require File.expand_path('../api/event_target', __FILE__) require File.expand_path('../api/event', __FILE__) module Faye class WebSocket module API CONNECTING = 0 OPEN = 1 CLOSING = 2 CLOSED = 3 CLOSE_TIMEOUT = 30 include EventTarget extend Forwardable def_delegators :@driver, :version attr_reader :url, :ready_state, :buffered_amount def initialize(options = {}) @ready_state = CONNECTING super() ::WebSocket::Driver.validate_options(options, [:headers, :extensions, :max_length, :ping, :proxy, :tls]) @driver = yield if headers = options[:headers] headers.each { |name, value| @driver.set_header(name, value) } end [*options[:extensions]].each do |extension| @driver.add_extension(extension) end @ping = options[:ping] @ping_id = 0 @buffered_amount = 0 @close_params = @close_timer = @ping_timer = @proxy = @stream = nil @onopen = @onmessage = @onclose = @onerror = nil @driver.on(:open) { |e| open } @driver.on(:message) { |e| receive_message(e.data) } @driver.on(:close) { |e| begin_close(e.reason, e.code) } @driver.on(:error) do |error| emit_error(error.message) end if @ping @ping_timer = EventMachine.add_periodic_timer(@ping) do @ping_id += 1 ping(@ping_id.to_s) end end end def write(data) @stream.write(data) end def send(message) return false if @ready_state > OPEN case message when Numeric then @driver.text(message.to_s) when String then @driver.text(message) when Array then @driver.binary(message) else false end end def ping(message = '', &callback) return false if @ready_state > OPEN @driver.ping(message, &callback) end def close(code = nil, reason = nil) code ||= 1000 reason ||= '' unless code == 1000 or (code >= 3000 and code <= 4999) raise ArgumentError, "Failed to execute 'close' on WebSocket: " + "The code must be either 1000, or between 3000 and 4999. " + "#{code} is neither." end @ready_state = CLOSING unless @ready_state == CLOSED @driver.close(reason, code) @close_timer = EventMachine.add_timer(CLOSE_TIMEOUT) { begin_close('', 1006) } end def protocol @driver.protocol || '' end private def open return unless @ready_state == CONNECTING @ready_state = OPEN event = Event.create('open') event.init_event('open', false, false) dispatch_event(event) end def receive_message(data) return unless @ready_state == OPEN event = Event.create('message', :data => data) event.init_event('message', false, false) dispatch_event(event) end def emit_error(message) return if @ready_state >= CLOSING event = Event.create('error', :message => message) event.init_event('error', false, false) dispatch_event(event) end def begin_close(reason, code) return if @ready_state == CLOSED @ready_state = CLOSING @close_params = [reason, code] if @stream @stream.close_connection_after_writing else finalize_close end end def finalize_close return if @ready_state == CLOSED @ready_state = CLOSED EventMachine.cancel_timer(@close_timer) if @close_timer EventMachine.cancel_timer(@ping_timer) if @ping_timer reason = @close_params ? @close_params[0] : '' code = @close_params ? @close_params[1] : 1006 event = Event.create('close', :code => code, :reason => reason) event.init_event('close', false, false) dispatch_event(event) end def parse(data) worker = @proxy || @driver worker.parse(data) end end end end faye-websocket-0.10.7/lib/faye/websocket/adapter.rb0000644000175000017500000000055713425040132022673 0ustar utkarsh2102utkarsh2102module Faye class WebSocket module Adapter def websocket? e = defined?(@env) ? @env : env e && WebSocket.websocket?(e) end def eventsource? e = defined?(@env) ? @env : env e && EventSource.eventsource?(e) end def socket_connection? websocket? or eventsource? end end end end faye-websocket-0.10.7/lib/faye/rack_stream.rb0000644000175000017500000000366213425040132021560 0ustar utkarsh2102utkarsh2102module Faye class RackStream include EventMachine::Deferrable module Reader attr_accessor :stream def receive_data(data) stream.receive(data) end def unbind stream.fail end end def initialize(socket) @socket_object = socket @connection = socket.env['em.connection'] @stream_send = socket.env['stream.send'] @rack_hijack_io = @rack_hijack_io_reader = nil hijack_rack_socket @connection.socket_stream = self if @connection.respond_to?(:socket_stream) end def hijack_rack_socket return unless @socket_object.env['rack.hijack'] @socket_object.env['rack.hijack'].call @rack_hijack_io = @socket_object.env['rack.hijack_io'] queue = Queue.new EventMachine.schedule do begin EventMachine.attach(@rack_hijack_io, Reader) do |reader| reader.stream = self if @rack_hijack_io @rack_hijack_io_reader = reader else reader.close_connection_after_writing end end ensure queue.push(nil) end end queue.pop if EventMachine.reactor_running? end def clean_rack_hijack return unless @rack_hijack_io @rack_hijack_io_reader.close_connection_after_writing @rack_hijack_io = @rack_hijack_io_reader = nil end def close_connection clean_rack_hijack @connection.close_connection if @connection end def close_connection_after_writing clean_rack_hijack @connection.close_connection_after_writing if @connection end def each(&callback) @stream_send ||= callback end def fail end def receive(data) end def write(data) return @rack_hijack_io.write(data) if @rack_hijack_io return @stream_send.call(data) if @stream_send rescue => e fail if EOFError === e end end end faye-websocket-0.10.7/lib/faye/adapters/0000755000175000017500000000000013425040132020534 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/lib/faye/adapters/goliath.rb0000644000175000017500000000173013425040132022511 0ustar utkarsh2102utkarsh2102class Goliath::Connection attr_accessor :socket_stream alias :goliath_receive_data :receive_data def receive_data(data) if @serving == :websocket socket_stream.receive(data) if socket_stream else goliath_receive_data(data) socket_stream.receive(@parser.upgrade_data) if socket_stream @serving = :websocket if @api.websocket? end end def unbind super ensure socket_stream.fail if socket_stream end end class Goliath::API include Faye::WebSocket::Adapter alias :goliath_call :call def call(env) @env = env goliath_call(env) end end class Goliath::Request alias :goliath_process :process def process env['em.connection'] = conn goliath_process end end class Goliath::Response alias :goliath_head :head alias :goliath_headers_output :headers_output def head (status == 101) ? '' : goliath_head end def headers_output (status == 101) ? '' : goliath_headers_output end end faye-websocket-0.10.7/lib/faye/adapters/thin.rb0000644000175000017500000000372113425040132022026 0ustar utkarsh2102utkarsh2102# WebSocket extensions for Thin # Based on code from the Cramp project # http://github.com/lifo/cramp # Copyright (c) 2009-2011 Pratik Naik # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. class Thin::Connection attr_accessor :socket_stream alias :thin_process :process alias :thin_receive_data :receive_data def process @serving ||= nil if @serving != :websocket and @request.websocket? @serving = :websocket end if @request.socket_connection? @request.env['em.connection'] = self @response.persistent! @response.async = true end thin_process end def receive_data(data) @serving ||= nil return thin_receive_data(data) unless @serving == :websocket socket_stream.receive(data) if socket_stream end end class Thin::Request include Faye::WebSocket::Adapter end class Thin::Response attr_accessor :async alias :thin_head :head def head return '' if async and status == 101 thin_head end end faye-websocket-0.10.7/lib/faye/adapters/rainbows_client.rb0000644000175000017500000000422113425040132024242 0ustar utkarsh2102utkarsh2102# WebSocket extensions for Rainbows # Based on code from the Cramp project # http://github.com/lifo/cramp # Copyright (c) 2009-2011 Pratik Naik # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. module Faye class WebSocket class RainbowsClient < Rainbows::EventMachine::Client include Faye::WebSocket::Adapter attr_accessor :socket_stream def receive_data(data) return super unless @state == :websocket socket_stream.receive(data) if socket_stream end def app_call(*args) @env['em.connection'] = self if args.first == NULL_IO and @hp.content_length == 0 and websocket? prepare_request_body else super end end def on_read(data) if @state == :body and websocket? and @hp.body_eof? @state = :websocket @input.rewind app_call StringIO.new(@buf) else super end end def unbind super ensure socket_stream.fail if socket_stream end def write_headers(status, headers, *args) super unless socket_connection? and status == 101 end end end end faye-websocket-0.10.7/lib/faye/adapters/rainbows.rb0000644000175000017500000000255513425040132022714 0ustar utkarsh2102utkarsh2102# WebSocket extensions for Rainbows # Based on code from the Cramp project # http://github.com/lifo/cramp # Copyright (c) 2009-2011 Pratik Naik # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. module Faye class WebSocket autoload :RainbowsClient, File.expand_path('../rainbows_client', __FILE__) end end Rainbows::O[:em_client_class] = "Faye::WebSocket::RainbowsClient" faye-websocket-0.10.7/lib/faye/eventsource.rb0000644000175000017500000000571413425040132021627 0ustar utkarsh2102utkarsh2102require File.expand_path('../websocket', __FILE__) unless defined?(Faye::WebSocket) module Faye class EventSource include WebSocket::API::EventTarget attr_reader :env, :url, :ready_state DEFAULT_RETRY = 5 def self.eventsource?(env) return false unless env['REQUEST_METHOD'] == 'GET' accept = (env['HTTP_ACCEPT'] || '').split(/\s*,\s*/) accept.include?('text/event-stream') end def self.determine_url(env) WebSocket.determine_url(env, ['https', 'http']) end def initialize(env, options = {}) WebSocket.ensure_reactor_running super() @env = env @ping = options[:ping] @retry = (options[:retry] || DEFAULT_RETRY).to_f @url = EventSource.determine_url(env) @stream = Stream.new(self) @ready_state = WebSocket::API::CONNECTING headers = ::WebSocket::Driver::Headers.new if options[:headers] options[:headers].each { |k,v| headers[k] = v } end if callback = @env['async.callback'] callback.call([101, {}, @stream]) end @stream.write("HTTP/1.1 200 OK\r\n" + "Content-Type: text/event-stream\r\n" + "Cache-Control: no-cache, no-store\r\n" + "Connection: close\r\n" + headers.to_s + "\r\n" + "retry: #{ (@retry * 1000).floor }\r\n\r\n") EventMachine.next_tick { open } if @ping @ping_timer = EventMachine.add_periodic_timer(@ping) { ping } end end def last_event_id @env['HTTP_LAST_EVENT_ID'] || '' end def rack_response [ -1, {}, [] ] end private def open return unless @ready_state == WebSocket::API::CONNECTING @ready_state = WebSocket::API::OPEN event = WebSocket::API::Event.create('open') event.init_event('open', false, false) dispatch_event(event) end public def send(message, options = {}) return false if @ready_state > WebSocket::API::OPEN message = ::WebSocket::Driver.encode(message.to_s). gsub(/(\r\n|\r|\n)/, '\1data: ') frame = "" frame << "event: #{options[:event]}\r\n" if options[:event] frame << "id: #{options[:id]}\r\n" if options[:id] frame << "data: #{message}\r\n\r\n" @stream.write(frame) true end def ping(message = nil) return false if @ready_state > WebSocket::API::OPEN @stream.write(":\r\n\r\n") true end def close return if [WebSocket::API::CLOSING, WebSocket::API::CLOSED].include?(@ready_state) @ready_state = WebSocket::API::CLOSED EventMachine.cancel_timer(@ping_timer) @stream.close_connection_after_writing event = WebSocket::API::Event.create('close') event.init_event('close', false, false) dispatch_event(event) end class Stream < RackStream def fail @socket_object.close end end end end faye-websocket-0.10.7/lib/faye/websocket.rb0000644000175000017500000000535713425040132021256 0ustar utkarsh2102utkarsh2102# API references: # # * https://html.spec.whatwg.org/multipage/comms.html#network # * https://dom.spec.whatwg.org/#interface-eventtarget # * https://dom.spec.whatwg.org/#interface-event require 'forwardable' require 'stringio' require 'uri' require 'eventmachine' require 'websocket/driver' module Faye autoload :EventSource, File.expand_path('../eventsource', __FILE__) autoload :RackStream, File.expand_path('../rack_stream', __FILE__) class WebSocket root = File.expand_path('../websocket', __FILE__) autoload :Adapter, root + '/adapter' autoload :API, root + '/api' autoload :Client, root + '/client' ADAPTERS = { 'goliath' => :Goliath, 'rainbows' => :Rainbows, 'thin' => :Thin } def self.determine_url(env, schemes = ['wss', 'ws']) scheme = schemes[secure_request?(env) ? 0 : 1] host = env['HTTP_HOST'] path = env['PATH_INFO'] query = env['QUERY_STRING'].to_s scheme + '://' + host + path + (query.empty? ? '' : '?' + query) end def self.ensure_reactor_running Thread.new { EventMachine.run } unless EventMachine.reactor_running? Thread.pass until EventMachine.reactor_running? end def self.load_adapter(backend) const = Kernel.const_get(ADAPTERS[backend]) rescue nil require(backend) unless const path = File.expand_path("../adapters/#{backend}.rb", __FILE__) require(path) if File.file?(path) end def self.secure_request?(env) return true if env['HTTPS'] == 'on' return true if env['HTTP_X_FORWARDED_SSL'] == 'on' return true if env['HTTP_X_FORWARDED_SCHEME'] == 'https' return true if env['HTTP_X_FORWARDED_PROTO'] == 'https' return true if env['rack.url_scheme'] == 'https' return false end def self.websocket?(env) ::WebSocket::Driver.websocket?(env) end attr_reader :env include API def initialize(env, protocols = nil, options = {}) WebSocket.ensure_reactor_running @env = env @url = WebSocket.determine_url(@env) super(options) { ::WebSocket::Driver.rack(self, :max_length => options[:max_length], :protocols => protocols) } @driver_started = false @stream = Stream.new(self) if callback = @env['async.callback'] callback.call([101, {}, @stream]) end end def start_driver return if @driver.nil? || @driver_started @driver_started = true EventMachine.schedule { @driver.start } end def rack_response start_driver [ -1, {}, [] ] end class Stream < RackStream def fail @socket_object.__send__(:finalize_close) end def receive(data) @socket_object.__send__(:parse, data) end end end end faye-websocket-0.10.7/CHANGELOG.md0000644000175000017500000000771213425040132017057 0ustar utkarsh2102utkarsh2102### 0.10.7 / 2017-02-22 * Emit an error if `EventMachine::Connection#unbind` is called with an error ### 0.10.6 / 2017-01-22 * Forcibly close the I/O stream after a timeout if the peer does not respond after calling `close()` ### 0.10.5 / 2016-11-12 * Set the SNI hostname when making secure requests ### 0.10.4 / 2016-05-20 * Amend warnings issued when running with -W2 ### 0.10.3 / 2016-02-24 * Use `PATH_INFO` and `QUERY_STRING` rather than the non-standard `REQUEST_URI` from the Rack env ### 0.10.2 / 2015-11-26 * Fix the `headers` and `status` methods on `Client`, which were broken in the last release ### 0.10.1 / 2015-11-06 * Make sure errors can be safely emitted if creating the driver fails * Prevent a race condition when binding `EM.attach` to the socket ### 0.10.0 / 2015-07-08 * Add the standard `code` and `reason` parameters to the `close` method ### 0.9.2 / 2014-12-21 * Only emit `error` once, and don't emit it after `close` ### 0.9.1 / 2014-12-18 * Check that all options to the WebSocket constructor are recognized ### 0.9.0 / 2014-12-13 * Allow protocol extensions to be passed into websocket-extensions ### 0.8.0 / 2014-11-08 * Support connections via HTTP proxies ### 0.7.5 / 2014-10-04 * Allow sockets to be closed when they are in any state other than `CLOSED` ### 0.7.4 / 2014-07-06 * Stop using `define_method` to implement `Event` properties, since it blows the method cache * Stop setup errors masking URI errors in `Client#initialize` * Make the Goliath adapter compatible with goliath-1.0.4. ### 0.7.3 / 2014-04-24 * Remove an unneeded method override in the `WebSocket` class ### 0.7.2 / 2013-12-29 * Fix WebSocket detection in cases where the web server does not produce an `env` ### 0.7.1 / 2013-12-03 * Support the `max_length` websocket-driver option * Expose a `message` property on `error` events ### 0.7.0 / 2013-09-09 * Allow the server to send custom headers with EventSource responses ### 0.6.3 / 2013-08-04 * Stop implicitly depending on Rack 1.4 ### 0.6.2 / 2013-07-05 * Catch errors thrown by EventMachine and emit `error` and `close` events ### 0.6.1 / 2013-05-12 * Release a gem without log and pid files in it ### 0.6.0 / 2013-05-12 * Add support for custom headers ### 0.5.0 / 2013-05-05 * Extract the protocol handlers into the `websocket-driver` library * Support the `rack.hijack` API * Add support for Rainbows 4.5 and Puma * Officially support JRuby and Rubinius ### 0.4.7 / 2013-02-14 * Emit the `close` event if TCP is closed before CLOSE frame is acked * Treat the `Upgrade: websocket` header case-insensitively because of IE10 * Do not suppress headers in the Thin and Rainbows adapters unless the status is `101` ### 0.4.6 / 2012-07-09 * Add `Connection: close` to EventSource response ### 0.4.5 / 2012-04-06 * Add WebSocket error code `1011`. * Handle URLs with no path correctly by sending `GET /` ### 0.4.4 / 2012-03-16 * Fix installation on JRuby with a platform-specific gem ### 0.4.3 / 2012-03-12 * Make `extconf.rb` a no-op on JRuby ### 0.4.2 / 2012-03-09 * Port masking-function C extension to Java for JRuby ### 0.4.1 / 2012-02-26 * Treat anything other than an `Array` as a string when calling `send()` * Fix error loading UTF-8 validation code on Ruby 1.9 with `-Ku` flag ### 0.4.0 / 2012-02-13 * Add `ping()` method to server-side `WebSocket` and `EventSource` * Buffer `send()` calls until the draft-76 handshake is complete ### 0.3.0 / 2012-01-13 * Add support for `EventSource` connections * Support the Thin, Rainbows and Goliath web servers ### 0.2.0 / 2011-12-21 * Add support for `Sec-WebSocket-Protocol` negotiation * Support `hixie-76` close frames and 75/76 ignored segments * Improve performance of HyBi parsing/framing functions * Write masking function in C ### 0.1.2 / 2011-12-05 * Make `hixie-76` sockets work through HAProxy ### 0.1.1 / 2011-11-30 * Fix `add_event_listener()` interface methods ### 0.1.0 / 2011-11-27 * Initial release, based on WebSocket components from Faye faye-websocket-0.10.7/README.md0000644000175000017500000003754213425040132016531 0ustar utkarsh2102utkarsh2102# faye-websocket * Travis CI build: [![Build status](https://secure.travis-ci.org/faye/faye-websocket-ruby.svg)](http://travis-ci.org/faye/faye-websocket-ruby) * Autobahn tests: [server](http://faye.jcoglan.com/autobahn/servers/), [client](http://faye.jcoglan.com/autobahn/clients/) This is a general-purpose WebSocket implementation extracted from the [Faye](http://faye.jcoglan.com) project. It provides classes for easily building WebSocket servers and clients in Ruby. It does not provide a server itself, but rather makes it easy to handle WebSocket connections within an existing [Rack](http://rack.github.io/) application. It does not provide any abstraction other than the standard [WebSocket API](https://html.spec.whatwg.org/multipage/comms.html#network). It also provides an abstraction for handling [EventSource](https://html.spec.whatwg.org/multipage/comms.html#server-sent-events) connections, which are one-way connections that allow the server to push data to the client. They are based on streaming HTTP responses and can be easier to access via proxies than WebSockets. The following web servers are supported. Other servers that implement the `rack.hijack` API should also work. * [Goliath](http://postrank-labs.github.com/goliath/) * [Phusion Passenger](https://www.phusionpassenger.com/) >= 4.0 with nginx >= 1.4 * [Puma](http://puma.io/) >= 2.0 * [Rainbows](http://rainbows.bogomips.org/) * [Thin](http://code.macournoyer.com/thin/) ## Installation ``` $ gem install faye-websocket ``` ## Handling WebSocket connections in Rack You can handle WebSockets on the server side by listening for requests using the `Faye::WebSocket.websocket?` method, and creating a new socket for the request. This socket object exposes the usual WebSocket methods for receiving and sending messages. For example this is how you'd implement an echo server: ```ruby # app.rb require 'faye/websocket' App = lambda do |env| if Faye::WebSocket.websocket?(env) ws = Faye::WebSocket.new(env) ws.on :message do |event| ws.send(event.data) end ws.on :close do |event| p [:close, event.code, event.reason] ws = nil end # Return async Rack response ws.rack_response else # Normal HTTP request [200, {'Content-Type' => 'text/plain'}, ['Hello']] end end ``` Note that under certain circumstances (notably a draft-76 client connecting through an HTTP proxy), the WebSocket handshake will not be complete after you call `Faye::WebSocket.new` because the server will not have received the entire handshake from the client yet. In this case, calls to `ws.send` will buffer the message in memory until the handshake is complete, at which point any buffered messages will be sent to the client. If you need to detect when the WebSocket handshake is complete, you can use the `onopen` event. If the connection's protocol version supports it, you can call `ws.ping()` to send a ping message and wait for the client's response. This method takes a message string, and an optional callback that fires when a matching pong message is received. It returns `true` if and only if a ping message was sent. If the client does not support ping/pong, this method sends no data and returns `false`. ```ruby ws.ping 'Mic check, one, two' do # fires when pong is received end ``` ## Using the WebSocket client The client supports both the plain-text `ws` protocol and the encrypted `wss` protocol, and has exactly the same interface as a socket you would use in a web browser. On the wire it identifies itself as `hybi-13`. ```ruby require 'faye/websocket' require 'eventmachine' EM.run { ws = Faye::WebSocket::Client.new('ws://www.example.com/') ws.on :open do |event| p [:open] ws.send('Hello, world!') end ws.on :message do |event| p [:message, event.data] end ws.on :close do |event| p [:close, event.code, event.reason] ws = nil end } ``` The WebSocket client also lets you inspect the status and headers of the handshake response via its `status` and `headers` methods. To connect via a proxy, set the `proxy` option to the HTTP origin of the proxy, including any authorization information and custom headers you require: ```rb ws = Faye::WebSocket::Client.new('ws://www.example.com/', [], { :proxy => { :origin => 'http://username:password@proxy.example.com', :headers => {'User-Agent' => 'ruby'} } }) ``` ## Subprotocol negotiation The WebSocket protocol allows peers to select and identify the application protocol to use over the connection. On the client side, you can set which protocols the client accepts by passing a list of protocol names when you construct the socket: ```ruby ws = Faye::WebSocket::Client.new('ws://www.example.com/', ['irc', 'amqp']) ``` On the server side, you can likewise pass in the list of protocols the server supports after the other constructor arguments: ```ruby ws = Faye::WebSocket.new(env, ['irc', 'amqp']) ``` If the client and server agree on a protocol, both the client- and server-side socket objects expose the selected protocol through the `ws.protocol` property. ## Protocol extensions faye-websocket is based on the [websocket-extensions](https://github.com/faye/websocket-extensions-ruby) framework that allows extensions to be negotiated via the `Sec-WebSocket-Extensions` header. To add extensions to a connection, pass an array of extensions to the `:extensions` option. For example, to add [permessage-deflate](https://github.com/faye/permessage-deflate-ruby): ```rb require 'permessage_deflate' ws = Faye::WebSocket.new(env, [], :extensions => [PermessageDeflate]) ``` ## Initialization options Both the server- and client-side classes allow an options hash to be passed in at initialization time, for example: ```ruby ws = Faye::WebSocket.new(env, protocols, options) ws = Faye::WebSocket::Client.new(url, protocols, options) ``` `protocols` as an array of subprotocols as described above, or `nil`. `options` is an optional hash containing any of these keys: * `:extensions` - an array of [websocket-extensions](https://github.com/faye/websocket-extensions-ruby) compatible extensions, as described above * `:headers` - a hash containing key-value pairs representing HTTP headers to be sent during the handshake process * `:max_length` - the maximum allowed size of incoming message frames, in bytes. The default value is `2^26 - 1`, or 1 byte short of 64 MiB. * `:ping` - an integer that sets how often the WebSocket should send ping frames, measured in seconds * `:tls` - a hash containing key-value pairs for specifying TLS parameters. These are passed along to EventMachine and you can find [more details here](http://rubydoc.info/gems/eventmachine/EventMachine%2FConnection%3Astart_tls) ## WebSocket API Both the server- and client-side `WebSocket` objects support the following API: * `on(:open) { |event| }` fires when the socket connection is established. Event has no attributes. * `on(:message) { |event| }` fires when the socket receives a message. Event has one attribute, `data`, which is either a `String` (for text frames) or an `Array` of byte-sized integers (for binary frames). * `on(:error) { |event| }` fires when there is a protocol error due to bad data sent by the other peer. This event is purely informational, you do not need to implement error recovery. * `on(:close) { |event| }` fires when either the client or the server closes the connection. Event has two optional attributes, `code` and `reason`, that expose the status code and message sent by the peer that closed the connection. * `send(message)` accepts either a `String` or an `Array` of byte-sized integers and sends a text or binary message over the connection to the other peer; binary data must be encoded as an `Array`. * `ping(message, &callback)` sends a ping frame with an optional message and fires the callback when a matching pong is received. * `close(code, reason)` closes the connection, sending the given status code and reason text, both of which are optional. * `version` is a string containing the version of the `WebSocket` protocol the connection is using. * `protocol` is a string (which may be empty) identifying the subprotocol the socket is using. ## Handling EventSource connections in Rack EventSource connections provide a very similar interface, although because they only allow the server to send data to the client, there is no `onmessage` API. EventSource allows the server to push text messages to the client, where each message has an optional event-type and ID. ```ruby # app.rb require 'faye/websocket' App = lambda do |env| if Faye::EventSource.eventsource?(env) es = Faye::EventSource.new(env) p [:open, es.url, es.last_event_id] # Periodically send messages loop = EM.add_periodic_timer(1) { es.send('Hello') } es.on :close do |event| EM.cancel_timer(loop) es = nil end # Return async Rack response es.rack_response else # Normal HTTP request [200, {'Content-Type' => 'text/plain'}, ['Hello']] end end ``` The `send` method takes two optional parameters, `:event` and `:id`. The default event-type is `'message'` with no ID. For example, to send a `notification` event with ID `99`: ```ruby es.send('Breaking News!', :event => 'notification', :id => '99') ``` The `EventSource` object exposes the following properties: * `url` is a string containing the URL the client used to create the EventSource. * `last_event_id` is a string containing the last event ID received by the client. You can use this when the client reconnects after a dropped connection to determine which messages need resending. When you initialize an EventSource with `Faye::EventSource.new`, you can pass configuration options after the `env` parameter. Available options are: * `:headers` is a hash containing custom headers to be set on the EventSource response. * `:retry` is a number that tells the client how long (in seconds) it should wait after a dropped connection before attempting to reconnect. * `:ping` is a number that tells the server how often (in seconds) to send 'ping' packets to the client to keep the connection open, to defeat timeouts set by proxies. The client will ignore these messages. For example, this creates a connection that allows access from any origin, pings every 15 seconds and is retryable every 10 seconds if the connection is broken: ```ruby es = Faye::EventSource.new(es, :headers => {'Access-Control-Allow-Origin' => '*'}, :ping => 15, :retry => 10 ) ``` You can send a ping message at any time by calling `es.ping`. Unlike WebSocket the client does not send a response to this; it is merely to send some data over the wire to keep the connection alive. ## Running your socket application The following describes how to run a WebSocket application using all our supported web servers. ### Running the app with Thin If you use Thin to serve your application you need to include this line after loading `faye/websocket`: ```ruby Faye::WebSocket.load_adapter('thin') ``` Thin can be started via the command line if you've set up a `config.ru` file for your application: ``` $ thin start -R config.ru -p 9292 ``` Or, you can use `rackup`. In development mode, this adds middlewares that don't work with async apps, so you must start it in production mode: ``` $ rackup config.ru -s thin -E production -p 9292 ``` It can also be started using the `Rack::Handler` interface common to many Ruby servers. You can configure Thin further in a block passed to `run`: ```ruby require 'eventmachine' require 'rack' require 'thin' require './app' Faye::WebSocket.load_adapter('thin') thin = Rack::Handler.get('thin') thin.run(App, :Port => 9292) do |server| # You can set options on the server here, for example to set up SSL: server.ssl_options = { :private_key_file => 'path/to/ssl.key', :cert_chain_file => 'path/to/ssl.crt' } server.ssl = true end ``` ### Running the app with Passenger faye-websocket requires either Passenger for Nginx or Passenger Standalone. [Apache doesn't work well with WebSockets at this time](https://github.com/phusion/passenger/issues/1202). You do not need any special configuration to make faye-websocket work, it should work out of the box on Passenger provided you use at least Passenger 4.0. However, you do need to insert the following code in `config.ru` for optimal WebSocket performance in Passenger. This is [documented in the Passenger manual](https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#tuning_sse_websockets). ```ruby if defined?(PhusionPassenger) PhusionPassenger.advertised_concurrency_level = 0 end ``` Run your app on Passenger for Nginx by creating a virtual host entry which points to your app's "public" directory: ``` server { listen 9292; server_name yourdomain.local; root /path-to-your-app/public; passenger_enabled on; } ``` Or run your app on Passenger Standalone: ``` $ passenger start -p 9292 ``` More information can be found on [the Passenger website](https://www.phusionpassenger.com/support). ### Running the app with Puma Puma has a command line interface for starting your application: ``` $ puma config.ru -p 9292 ``` Or, you can use `rackup`. In development mode, this adds middlewares that don't work with async apps, so you must start it in production mode: ``` $ rackup config.ru -s puma -E production -p 9292 ``` ### Running the app with Rainbows If you're using version 4.4 or lower of Rainbows, you need to run it with the EventMachine backend and enable the adapter. Put this in your `rainbows.conf` file: ```ruby Rainbows! { use :EventMachine } ``` And make sure you load the adapter in your application: ```ruby Faye::WebSocket.load_adapter('rainbows') ``` Version 4.5 of Rainbows does not need this adapter. You can run your `config.ru` file from the command line. Again, `Rack::Lint` will complain unless you put the application in production mode. ``` $ rainbows config.ru -c path/to/rainbows.conf -E production -p 9292 ``` ### Running the app with Goliath If you use Goliath to server your application you need to include this line after loading `faye/websocket`: ```ruby Faye::WebSocket.load_adapter('goliath') ``` Goliath can be made to run arbitrary Rack apps by delegating to them from a `Goliath::API` instance. A simple server looks like this: ```ruby require 'goliath' require './app' Faye::WebSocket.load_adapter('goliath') class EchoServer < Goliath::API def response(env) App.call(env) end end ``` `Faye::WebSocket` can also be used inline within a Goliath app: ```ruby require 'goliath' require 'faye/websocket' Faye::WebSocket.load_adapter('goliath') class EchoServer < Goliath::API def response(env) ws = Faye::WebSocket.new(env) ws.on :message do |event| ws.send(event.data) end ws.rack_response end end ``` ## License (The MIT License) Copyright (c) 2010-2017 James Coglan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. faye-websocket-0.10.7/faye-websocket.gemspec0000644000175000017500000001036413425040132021520 0ustar utkarsh2102utkarsh2102######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: faye-websocket 0.10.7 ruby lib Gem::Specification.new do |s| s.name = "faye-websocket".freeze s.version = "0.10.7" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["James Coglan".freeze] s.date = "2017-02-22" s.email = "jcoglan@gmail.com".freeze s.extra_rdoc_files = ["README.md".freeze] s.files = ["CHANGELOG.md".freeze, "README.md".freeze, "examples/app.rb".freeze, "examples/autobahn_client.rb".freeze, "examples/client.rb".freeze, "examples/config.ru".freeze, "examples/haproxy.conf".freeze, "examples/proxy_server.rb".freeze, "examples/rainbows.conf".freeze, "examples/server.rb".freeze, "examples/sse.html".freeze, "examples/ws.html".freeze, "lib/faye/adapters/goliath.rb".freeze, "lib/faye/adapters/rainbows.rb".freeze, "lib/faye/adapters/rainbows_client.rb".freeze, "lib/faye/adapters/thin.rb".freeze, "lib/faye/eventsource.rb".freeze, "lib/faye/rack_stream.rb".freeze, "lib/faye/websocket.rb".freeze, "lib/faye/websocket/adapter.rb".freeze, "lib/faye/websocket/api.rb".freeze, "lib/faye/websocket/api/event.rb".freeze, "lib/faye/websocket/api/event_target.rb".freeze, "lib/faye/websocket/client.rb".freeze] s.homepage = "https://github.com/faye/faye-websocket-ruby".freeze s.licenses = ["MIT".freeze] s.rdoc_options = ["--main".freeze, "README.md".freeze, "--markup".freeze, "markdown".freeze] s.rubygems_version = "2.7.6".freeze s.summary = "Standards-compliant WebSocket server and client".freeze if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q.freeze, [">= 0.12.0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 4.0.0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, ["< 1.5.0"]) s.add_development_dependency(%q.freeze, [">= 2.0.0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, ["~> 4.4.0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0.2.0"]) s.add_development_dependency(%q.freeze, [">= 1.2.0"]) s.add_runtime_dependency(%q.freeze, [">= 0.5.1"]) else s.add_dependency(%q.freeze, [">= 0.12.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 4.0.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["< 1.5.0"]) s.add_dependency(%q.freeze, [">= 2.0.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["~> 4.4.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0.2.0"]) s.add_dependency(%q.freeze, [">= 1.2.0"]) s.add_dependency(%q.freeze, [">= 0.5.1"]) end else s.add_dependency(%q.freeze, [">= 0.12.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 4.0.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["< 1.5.0"]) s.add_dependency(%q.freeze, [">= 2.0.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["~> 4.4.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0.2.0"]) s.add_dependency(%q.freeze, [">= 1.2.0"]) s.add_dependency(%q.freeze, [">= 0.5.1"]) end end faye-websocket-0.10.7/examples/0000755000175000017500000000000013425040132017055 5ustar utkarsh2102utkarsh2102faye-websocket-0.10.7/examples/proxy_server.rb0000644000175000017500000000043313425040132022151 0ustar utkarsh2102utkarsh2102require 'rubygems' require 'bundler/setup' require 'eventmachine' require 'websocket/driver' require File.expand_path('../../spec/proxy_server', __FILE__) port = ARGV[0] secure = ARGV[1] == 'tls' EM.run { proxy = ProxyServer.new(:debug => true) proxy.listen(port, secure) } faye-websocket-0.10.7/examples/sse.html0000644000175000017500000000152513425040132020540 0ustar utkarsh2102utkarsh2102 EventSource test

EventSource test

    faye-websocket-0.10.7/examples/haproxy.conf0000644000175000017500000000054713425040132021424 0ustar utkarsh2102utkarsh2102defaults mode http timeout client 5s timeout connect 5s timeout server 5s frontend all 0.0.0.0:3000 mode http timeout client 120s option forwardfor option http-server-close option http-pretend-keepalive default_backend sockets backend sockets balance uri depth 2 timeout server 120s server socket1 127.0.0.1:7000 faye-websocket-0.10.7/examples/client.rb0000644000175000017500000000132113425040132020655 0ustar utkarsh2102utkarsh2102require 'rubygems' require 'bundler/setup' require 'faye/websocket' require 'eventmachine' require 'permessage_deflate' EM.run { url = ARGV[0] proxy = ARGV[1] ws = Faye::WebSocket::Client.new(url, [], :proxy => {:origin => proxy, :headers => {'User-Agent' => 'Echo'}}, :headers => {'Origin' => 'http://faye.jcoglan.com'}, :extensions => [PermessageDeflate] ) ws.onopen = lambda do |event| p [:open, ws.headers] ws.send('mic check') 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| p [:message, message.data] end } faye-websocket-0.10.7/examples/rainbows.conf0000644000175000017500000000004513425040132021547 0ustar utkarsh2102utkarsh2102Rainbows! do use :EventMachine end faye-websocket-0.10.7/examples/ws.html0000644000175000017500000000221113425040132020370 0ustar utkarsh2102utkarsh2102 WebSocket test

    WebSocket test

      faye-websocket-0.10.7/examples/server.rb0000644000175000017500000000235113425040132020711 0ustar utkarsh2102utkarsh2102require 'rubygems' require 'bundler/setup' require 'rack/content_length' require 'rack/chunked' port = ARGV[0] || 7000 secure = ARGV[1] == 'tls' engine = ARGV[2] || 'thin' spec = File.expand_path('../../spec', __FILE__) require File.expand_path('../app', __FILE__) Faye::WebSocket.load_adapter(engine) case engine when 'goliath' class WebSocketServer < Goliath::API def response(env) App.call(env) end end when 'puma' require 'puma/binder' require 'puma/events' events = Puma::Events.new($stdout, $stderr) binder = Puma::Binder.new(events) binder.parse(["tcp://0.0.0.0:#{port}"], App) server = Puma::Server.new(App, events) server.binder = binder server.run.join when 'rainbows' rackup = Unicorn::Configurator::RACKUP rackup[:port] = port rackup[:set_listener] = true options = rackup[:options] options[:config_file] = File.expand_path('../rainbows.conf', __FILE__) Rainbows::HttpServer.new(App, options).start.join when 'thin' thin = Rack::Handler.get('thin') thin.run(App, :Port => port) do |server| if secure server.ssl_options = { :private_key_file => spec + '/server.key', :cert_chain_file => spec + '/server.crt' } server.ssl = true end end end faye-websocket-0.10.7/examples/config.ru0000644000175000017500000000052713425040132020676 0ustar utkarsh2102utkarsh2102# Run using your favourite server: # # thin start -R examples/config.ru -p 7000 # rainbows -c examples/rainbows.conf -E production examples/config.ru -p 7000 require 'rubygems' require 'bundler/setup' require File.expand_path('../app', __FILE__) Faye::WebSocket.load_adapter('thin') Faye::WebSocket.load_adapter('rainbows') run App faye-websocket-0.10.7/examples/autobahn_client.rb0000644000175000017500000000221313425040132022537 0ustar utkarsh2102utkarsh2102require 'rubygems' require 'bundler/setup' require 'cgi' require 'faye/websocket' require 'permessage_deflate' require 'progressbar' EM.run { host = 'ws://localhost:9001' ruby = RUBY_PLATFORM =~ /java/ ? 'jruby' : 'cruby' agent = CGI.escape("#{ruby}-#{RUBY_VERSION}") cases = 0 options = {:extensions => [PermessageDeflate]} socket = Faye::WebSocket::Client.new("#{host}/getCaseCount") progress = nil socket.onmessage = lambda do |event| puts "Total cases to run: #{event.data}" cases = event.data.to_i progress = ProgressBar.create(:title => 'Autobahn', :total => cases) end run_case = lambda do |n| if n > cases socket = Faye::WebSocket::Client.new("#{host}/updateReports?agent=#{agent}") socket.onclose = lambda { |e| EM.stop } next end url = "#{host}/runCase?case=#{n}&agent=#{agent}" socket = Faye::WebSocket::Client.new(url, [], options) socket.onmessage = lambda do |event| socket.send(event.data) end socket.on :close do |event| progress.increment run_case[n + 1] end end socket.onclose = lambda do |event| run_case[1] end } faye-websocket-0.10.7/examples/app.rb0000644000175000017500000000220113425040132020155 0ustar utkarsh2102utkarsh2102require 'faye/websocket' require 'permessage_deflate' require 'rack' static = Rack::File.new(File.dirname(__FILE__)) options = {:extensions => [PermessageDeflate], :ping => 5} App = lambda do |env| if Faye::WebSocket.websocket?(env) ws = Faye::WebSocket.new(env, ['irc', 'xmpp'], options) p [:open, ws.url, ws.version, ws.protocol] ws.onmessage = lambda do |event| ws.send(event.data) end ws.onclose = lambda do |event| p [:close, event.code, event.reason] ws = nil end ws.rack_response elsif Faye::EventSource.eventsource?(env) es = Faye::EventSource.new(env) time = es.last_event_id.to_i p [:open, es.url, es.last_event_id] loop = EM.add_periodic_timer(2) do time += 1 es.send("Time: #{time}") EM.add_timer(1) do es.send('Update!!', :event => 'update', :id => time) if es end end es.send("Welcome!\n\nThis is an EventSource server.") es.onclose = lambda do |event| EM.cancel_timer(loop) p [:close, es.url] es = nil end es.rack_response else static.call(env) end end def App.log(message) end