Sha256: d8865abd8ad2777c0add6bc0c3a6b6a13e2bce6a26e551a0ebd51cf5f891f205

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require_relative './json-websocket'

class PageWebSocket < WebSocketHelper

	def initialize(ws, options = {})
		super(ws)

		# todo: validate the url and the app instances
		@app = options[:app]
		@url = options[:url]
	end

	def on_open
		super

		uri = URI.parse(@url)
		path = uri.path
		method_name = @app.class._method_name('LIVE', path)

		if @app.respond_to? method_name
			@app.send(method_name, document)
		else
			# send an error back to the client
			self.send 'message', { :content => "no live handler for #{path}" }
		end

	end

	def on_close
		super
	end

	def document
		if @document.nil?
			@document = ClientDocument.new(self)
			@document.location = @url
		end

		return @document
	end
	
end

class ClientDocument
	attr_accessor :location

	def initialize(client)
		raise 'client must be a WebSocketHelper' unless client.is_a? WebSocketHelper
		@client = client
	end

	def element(selector)
		ClientElement.new(selector, @client)
	end

	def on_load
		# maybe later we can do something else with this, but for now
		# just call the load method straight away
		yield()
	end
end

class ClientElement
	attr_accessor :selector
	
	def initialize(selector, client)
		raise 'client must be a WebSocketHelper' unless client.is_a? WebSocketHelper

		@client = client
		self.selector = selector.to_s
	end

	def execute(method, content)
		@client.send('exec', {
			:selector => self.selector,
			:method => method,
			:content => content.to_s
		})
	end

	def text=(s)
		self.execute 'text', s
	end

	def html=(s)
		self.execute 'html', s
	end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-liveviews-0.5.0 lib/sinatra/liveviews/page-websocket.rb