Sha256: 8e386650f8329dbea9352157e0690158358417c6036e017f7475158cd25c67e9

Contents?: true

Size: 1.48 KB

Versions: 14

Compression:

Stored size: 1.48 KB

Contents

require 'eventmachine'
require 'em-websocket'
require 'http/parser'

module Hyla
  class WebSocket < EventMachine::WebSocket::Connection

    def dispatch(data)
      parser = Http::Parser.new
      parser << data
      if parser.http_method != 'GET' || parser.upgrade?
        super #pass the request to websocket
      elsif parser.request_path == '/livereload.js'
        _serve_file(_livereload_js_file)
      elsif File.exist?(parser.request_path[1..-1])
        _serve_file(parser.request_path[1..-1]) # Strip leading slash
      else
        send_data("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n404 Not Found")
        close_connection_after_writing
      end
    end

    private

    def _serve_file(path)
      Hyla.logger.info "Serving file #{path}"
      send_data "HTTP/1.1 200 OK\r\nContent-Type: #{_content_type(path)}\r\nContent-Length: #{File.size path}\r\n\r\n"
      stream_file_data(path).callback { close_connection_after_writing }
    end

    def _content_type(path)
      case File.extname(path)
        when '.css' then
          'text/css'
        when '.js' then
          'application/ecmascript'
        when '.gif' then
          'image/gif'
        when '.jpeg', '.jpg' then
          'image/jpeg'
        when '.png' then
          'image/png'
        else
          ; 'text/plain'
      end
    end

    def _livereload_js_file
      File.expand_path("../../../data/js/livereload.js", __FILE__)
    end

  end # Class WebSocket
end # module Hyla

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hyla-1.0.7.pre.7 lib/hyla/websocket.rb
hyla-1.0.7.pre.6 lib/hyla/websocket.rb
hyla-1.0.7.pre.5 lib/hyla/websocket.rb
hyla-1.0.7.pre.3 lib/hyla/websocket.rb
hyla-1.0.7.pre.2 lib/hyla/websocket.rb
hyla-1.0.7.pre.1 lib/hyla/websocket.rb
hyla-1.0.6 lib/hyla/websocket.rb
hyla-1.0.5 lib/hyla/websocket.rb
hyla-1.0.5.pre.1 lib/hyla/websocket.rb
hyla-1.0.4 lib/hyla/websocket.rb
hyla-1.0.3 lib/hyla/websocket.rb
hyla-1.0.2 lib/hyla/websocket.rb
hyla-1.0.1 lib/hyla/websocket.rb
hyla-1.0 lib/hyla/websocket.rb