Class: XRBP::WebSocket::Client

Inherits:
Object
  • Object
show all
Includes:
EventEmitter, Terminatable
Defined in:
lib/xrbp/websocket/client.rb

Overview

Managed socket connection lifecycle and read/write operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Terminatable

#terminate!, #terminate?, #terminate_queue

Constructor Details

#initialize(url, options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
# File 'lib/xrbp/websocket/client.rb', line 14

def initialize(url, options={})
  @url = url
  @options = options

  @handshaked = false
  @closed = true
  @completed = true
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/xrbp/websocket/client.rb', line 12

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/xrbp/websocket/client.rb', line 12

def url
  @url
end

Instance Method Details

#add_work(&bl) ⇒ Object

Add job to internal thread pool.



37
38
39
# File 'lib/xrbp/websocket/client.rb', line 37

def add_work(&bl)
  pool.post &bl
end

#async_close(err = nil) ⇒ Object

Allow close to be run via seperate thread so as not to block caller



57
58
59
# File 'lib/xrbp/websocket/client.rb', line 57

def async_close(err=nil)
  Thread.new { close(err)  }
end

#close(err = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xrbp/websocket/client.rb', line 61

def close(err=nil)
  return if closed?

  # XXX set closed true first incase callbacks need to check this
  @closed = true
  @handshake = nil
  @handshaked = false

  terminate!

  send_data nil, :type => :close unless socket.pipe_broken
  emit_signal :close, err

  socket.close if socket
  @socket = nil

  pool.shutdown
  pool.wait_for_termination
  @pool = nil

  @completed = true
  emit :completed
  self
end

#closed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/xrbp/websocket/client.rb', line 47

def closed?
  !!@closed
end

#completed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/xrbp/websocket/client.rb', line 51

def completed?
  !!@completed
end

#connectObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xrbp/websocket/client.rb', line 23

def connect
  emit_signal :connecting

  @closed = false
  @completed = false
  socket.connect
  handshake!

  start_read

  self
end

#open?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/xrbp/websocket/client.rb', line 43

def open?
  handshake.finished? and !closed?
end

#send_data(data, opt = {:type => :text}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xrbp/websocket/client.rb', line 128

def send_data(data, opt={:type => :text})
  return if !handshaked? || closed?

  begin
    frame = data_frame(data, opt[:type])
    socket.write_nonblock(frame.to_s)

  rescue Errno::EPIPE, OpenSSL::SSL::SSLError => e
    async_close(e)
  end
end