Class: Slack::RealTime::Concurrency::Celluloid::Socket

Inherits:
Socket
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid::IO, Celluloid::Internals::Logger
Defined in:
lib/slack/real_time/concurrency/celluloid.rb

Defined Under Namespace

Classes: Actor

Constant Summary collapse

BLOCK_SIZE =
4096

Instance Attribute Summary collapse

Attributes inherited from Socket

#driver, #options, #url

Instance Method Summary collapse

Methods inherited from Socket

#disconnect!, #send_data, #start_sync

Constructor Details

#initialize(*args) ⇒ Socket

Returns a new instance of Socket.



22
23
24
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 22

def initialize(*args)
  super
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



20
21
22
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 20

def socket
  @socket
end

Instance Method Details

#closeObject



44
45
46
47
48
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 44

def close
  @closing = true
  driver.close
  super
end

#connect!Object



26
27
28
29
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 26

def connect!
  super
  run_loop
end

#connected?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 74

def connected?
  !@connected.nil?
end

#handle_read(buffer) ⇒ Object



55
56
57
58
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 55

def handle_read(buffer)
  logger.debug("#{self.class}##{__method__}") { buffer }
  driver.parse buffer
end

#readObject



50
51
52
53
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 50

def read
  buffer = socket.readpartial(BLOCK_SIZE)
  async.handle_read(buffer)
end

#run_client_loopObject



70
71
72
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 70

def run_client_loop
  @client.run_loop
end

#run_loopObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 31

def run_loop
  @closing = false
  @socket = build_socket
  @connected = @socket.connect
  driver.start
  loop { read } if socket
rescue EOFError => e
  logger.debug("#{self.class}##{__method__}") { e }
  driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'server closed connection')) unless @closing
ensure
  current_actor.terminate if current_actor.alive? && current_actor.running?
end

#start_async(client) ⇒ Object



65
66
67
68
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 65

def start_async(client)
  @client = client
  Actor.new(future.run_client_loop)
end

#write(data) ⇒ Object



60
61
62
63
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 60

def write(data)
  logger.debug("#{self.class}##{__method__}") { data }
  socket.write(data)
end