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

Returns a new instance of Connection.


46
47
48
49
50
# 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


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

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


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

def call
	self.close
end

#closeObject


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

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

#dump(object) ⇒ Object


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

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

#parse(buffer) ⇒ Object


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

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

#readObject


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

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

#write(object) ⇒ Object


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

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