Class: Goliath::WebSocket

Inherits:
API
  • Object
show all
Includes:
Constants
Defined in:
lib/goliath/websocket.rb

Constant Summary

Constants included from Constants

Constants::ASYNC_BODY, Constants::ASYNC_CALLBACK, Constants::ASYNC_CLOSE, Constants::ASYNC_HEADERS, Constants::CONFIG, Constants::CONNECTION, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::FRAGMENT, Constants::GOLIATH_ENV, Constants::GOLIATH_SIGNATURE, Constants::HTTP_PREFIX, Constants::HTTP_VERSION, Constants::INITIAL_BODY, Constants::LOCALHOST, Constants::OPTIONS, Constants::PATH_INFO, Constants::QUERY_STRING, Constants::RACK_ERRORS, Constants::RACK_EXCEPTION, Constants::RACK_INPUT, Constants::RACK_LOGGER, Constants::RACK_MULTIPROCESS, Constants::RACK_MULTITHREAD, Constants::RACK_RUN_ONCE, Constants::RACK_URL_SCHEME, Constants::RACK_VERSION, Constants::RACK_VERSION_NUM, Constants::REMOTE_ADDR, Constants::REQUEST_METHOD, Constants::REQUEST_PATH, Constants::REQUEST_URI, Constants::SCRIPT_NAME, Constants::SERVER, Constants::SERVER_NAME, Constants::SERVER_PORT, Constants::SERVER_SOFTWARE, Constants::STATUS, Constants::STREAM_CLOSE, Constants::STREAM_SEND, Constants::STREAM_START, Constants::UPGRADE_DATA

Instance Method Summary collapse

Methods inherited from API

#call, #chunked_streaming_response, #env, inherited, #initialize, #method_missing, middlewares, #options_parser, plugin, plugins, #respond_to_missing?, #streaming_response, use

Methods included from Rack::Validator

safely, validation_error

Constructor Details

This class inherits a constructor from Goliath::API

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Goliath::API

Instance Method Details

#on_body(env, data) ⇒ Object



16
17
18
# File 'lib/goliath/websocket.rb', line 16

def on_body(env, data)
  env.handler.receive_data(data)
end

#on_close(env) ⇒ Object



9
# File 'lib/goliath/websocket.rb', line 9

def on_close(env) ; end

#on_error(env, error) ⇒ Object



10
# File 'lib/goliath/websocket.rb', line 10

def on_error(env, error) ; end

#on_headers(env, headers) ⇒ Object



12
13
14
# File 'lib/goliath/websocket.rb', line 12

def on_headers(env, headers)
  env['goliath.request-headers'] = headers
end

#on_message(env, msg) ⇒ Object



8
# File 'lib/goliath/websocket.rb', line 8

def on_message(env, msg) ; end

#on_open(env) ⇒ Object



7
# File 'lib/goliath/websocket.rb', line 7

def on_open(env) ; end

#response(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/goliath/websocket.rb', line 20

def response(env)
  request = {}

  # em-websockets expects the keys to all be lowercase so we need to convert
  env['goliath.request-headers'].each_pair do |key, value|
    request[key.downcase] = value
  end

  request['path'] = env[REQUEST_PATH]
  request['method'] = env[REQUEST_METHOD]
  request['query'] = env[QUERY_STRING]

  old_stream_send = env[STREAM_SEND]
  old_stream_close = env[STREAM_CLOSE]
  env[STREAM_SEND]  = proc do |data|
    env.handler.send_text_frame(data.to_s.force_encoding("BINARY"))
  end
  env[STREAM_CLOSE] = proc { |code, body| env.handler.close_websocket(code, body) }
  env[STREAM_START] = proc { }

  conn = Class.new do
    attr_writer :max_frame_size

    def initialize(env, parent, stream_send, connection_close)
      @env = env
      @parent = parent
      @stream_send = stream_send
      @connection_close = connection_close
    end

    def trigger_on_open
      @parent.on_open(@env)
    end

    def trigger_on_close
      @parent.on_close(@env)
    end

    def trigger_on_message(msg)
      @parent.on_message(@env, msg)
    end

    def close_connection
      @env[STREAM_CLOSE].call
    end

    def close_connection_after_writing
      @connection_close.call
    end

    def send_data(data)
      @stream_send.call(data)
    end

    def max_frame_size
      @max_frame_size || EM::WebSocket.max_frame_size
    end
  end.new(env, self, old_stream_send, old_stream_close)

  upgrade_data = env[UPGRADE_DATA]
  begin
    env['handler'] = EM::WebSocket::HandlerFactory.build_with_request(conn, request,
        upgrade_data, false, false)
  rescue Exception => e
    env.logger.error("#{e.message}\n#{e.backtrace.join("\n")}")
    return [500, {}, {:error => "Upgrade failed"}]
  end
  env['handler'].run

  Goliath::Connection::AsyncResponse
end