Class: Async::WebSocket::Connection

Inherits:
Protocol::WebSocket::Connection
  • Object
show all
Includes:
Protocol::WebSocket::Headers
Defined in:
lib/async/websocket/connection.rb

Overview

This is a basic synchronous websocket client:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(framer, protocol = nil, response: nil, **options) ⇒ Connection



46
47
48
49
50
51
# File 'lib/async/websocket/connection.rb', line 46

def initialize(framer, protocol = nil, response: nil, **options)
  super(framer, **options)
  
  @protocol = protocol
  @response = response
end

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



62
63
64
# File 'lib/async/websocket/connection.rb', line 62

def protocol
  @protocol
end

Class Method Details

.call(framer, protocol = [], **options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/websocket/connection.rb', line 34

def self.call(framer, protocol = [], **options)
  instance = self.new(framer, Array(protocol).first, **options)
  
  return instance unless block_given?
  
  begin
    yield instance
  ensure
    instance.close
  end
end

Instance Method Details

#callObject



82
83
84
# File 'lib/async/websocket/connection.rb', line 82

def call
  self.close
end

#closeObject



53
54
55
56
57
58
59
60
# File 'lib/async/websocket/connection.rb', line 53

def close
  super
  
  if @response
    @response.finish
    @response = nil
  end
end

#dump(object) ⇒ Object



78
79
80
# File 'lib/async/websocket/connection.rb', line 78

def dump(object)
  JSON.dump(object)
end

#parse(buffer) ⇒ Object



74
75
76
# File 'lib/async/websocket/connection.rb', line 74

def parse(buffer)
  JSON.parse(buffer, symbolize_names: true)
end

#readObject



64
65
66
67
68
# File 'lib/async/websocket/connection.rb', line 64

def read
  if buffer = super
    parse(buffer)
  end
end

#write(object) ⇒ Object



70
71
72
# File 'lib/async/websocket/connection.rb', line 70

def write(object)
  super(dump(object))
end