Class: Utopia::WebSocket::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/websocket.rb

Constant Summary collapse

READ_BUFFER_SIZE =
1024*8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, io) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(env, io)
	@env = env
	@io = io
	
	scheme = Rack::Request.new(env).ssl? ? 'wss:' : 'ws:'
	@url = scheme + '//' + env['HTTP_HOST'] + env['REQUEST_URI']
	
	@driver = ::WebSocket::Driver.rack(self)
	@running = false
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



33
34
35
# File 'lib/utopia/websocket.rb', line 33

def env
  @env
end

#urlObject (readonly)

Returns the value of attribute url.



33
34
35
# File 'lib/utopia/websocket.rb', line 33

def url
  @url
end

Instance Method Details

#readObject



50
51
52
# File 'lib/utopia/websocket.rb', line 50

def read
	@driver.parse(@io.readpartial(READ_BUFFER_SIZE))
end

#run(&handler) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/utopia/websocket.rb', line 54

def run(&handler)
	@running = true
	
	@driver.on(:close) do
		@running = false
	end
	
	@driver.on(:open) do
		yield @driver if block_given?
	end
	
	@driver.start
	
	while @running
		self.read
	end
ensure
	@io.close
end

#write(string) ⇒ Object



46
47
48
# File 'lib/utopia/websocket.rb', line 46

def write(string)
	@io.write(string)
end