Class: Faye::Transport::WebSocket

Inherits:
Faye::Transport show all
Includes:
EventMachine::Deferrable
Defined in:
lib/faye/transport/web_socket.rb

Constant Summary collapse

UNCONNECTED =
1
CONNECTING =
2
CONNECTED =
3

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary

Attributes inherited from Faye::Transport

#cookies, #endpoint, #headers

Attributes included from Logging

#log_level

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Faye::Transport

#connection_type, #flush, get, #initialize, #receive, register, #retry_block, #send

Methods included from Faye::Timeouts

#add_timeout, #remove_timeout

Methods included from Publisher

#bind, #count_listeners, #trigger, #unbind

Methods included from Logging

#log

Constructor Details

This class inherits a constructor from Faye::Transport

Class Method Details

.create(client, endpoint) ⇒ Object



14
15
16
17
# File 'lib/faye/transport/web_socket.rb', line 14

def self.create(client, endpoint)
  sockets = client.transports[:websocket] ||= {}
  sockets[endpoint] ||= new(client, endpoint)
end

.usable?(client, endpoint, &callback) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/faye/transport/web_socket.rb', line 10

def self.usable?(client, endpoint, &callback)
  create(client, endpoint).usable?(&callback)
end

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/faye/transport/web_socket.rb', line 19

def batching?
  false
end

#closeObject



37
38
39
40
41
42
43
44
# File 'lib/faye/transport/web_socket.rb', line 37

def close
  return unless @socket
  @socket.onclose = @socket.onerror = nil
  @socket.close
  @socket = nil
  set_deferred_status(:deferred)
  @state = UNCONNECTED
end

#connectObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/faye/transport/web_socket.rb', line 46

def connect
  @state ||= UNCONNECTED
  return unless @state == UNCONNECTED

  @state = CONNECTING

  @socket = Faye::WebSocket::Client.new(@endpoint.gsub(/^http(s?):/, 'ws\1:'))

  @socket.onopen = lambda do |*args|
    @state = CONNECTED
    @ever_connected = true
    set_deferred_status(:succeeded, @socket)
    trigger(:up)
  end

  @socket.onmessage = lambda do |event|
    messages = MultiJson.load(event.data)
    next if messages.nil?
    messages = [messages].flatten
    messages.each { |message| @messages.delete(message['id']) }
    receive(messages)
  end

  @socket.onclose = @socket.onerror = lambda do |*args|
    was_connected = (@state == CONNECTED)
    set_deferred_status(:deferred)
    @state = UNCONNECTED

    close

    next resend if was_connected
    next set_deferred_status(:failed) unless @ever_connected

    EventMachine.add_timer(@client.retry) { connect }
    trigger(:down)
  end
end

#request(messages, timeout = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/faye/transport/web_socket.rb', line 29

def request(messages, timeout = nil)
  return if messages.empty?
  @messages ||= {}
  messages.each { |message| @messages[message['id']] = message }
  callback { |socket| socket.send(Faye.to_json(messages)) }
  connect
end

#resendObject



84
85
86
87
# File 'lib/faye/transport/web_socket.rb', line 84

def resend
  return unless @messages
  request(@messages.values)
end

#usable?(&callback) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/faye/transport/web_socket.rb', line 23

def usable?(&callback)
  self.callback { callback.call(true) }
  self.errback { callback.call(false) }
  connect
end