Class: Wamp::Router::Server
- Inherits:
-
Object
- Object
- Wamp::Router::Server
- Defined in:
- lib/wamp/router/server.rb
Overview
Connection Handler
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
- #accept_connection ⇒ Object
- #create_connection(client) ⇒ Object
- #create_tcp_server ⇒ Object
-
#initialize(router, options = {}) ⇒ Server
constructor
A new instance of Server.
- #options_message ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(router, options = {}) ⇒ Server
Returns a new instance of Server.
14 15 16 17 18 19 |
# File 'lib/wamp/router/server.rb', line 14 def initialize(router, = {}) = @selector = NIO::Selector.new @router = router # @router.add_realm(options.fetch(:realm, "realm1")) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/wamp/router/server.rb', line 12 def end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
12 13 14 |
# File 'lib/wamp/router/server.rb', line 12 def selector @selector end |
Instance Method Details
#accept_connection ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/wamp/router/server.rb', line 45 def accept_connection selector.select do |monitor| case monitor.io when TCPServer create_connection(monitor.io.accept_nonblock) when TCPSocket monitor.value.call end end end |
#create_connection(client) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/wamp/router/server.rb', line 56 def create_connection(client) monitor = selector.register(client, :r) connection = Connection.new(client) do |conn| selector.deregister(monitor) @router.detach_client(conn) end connection.router = @router monitor.value = proc do connection.listen end end |
#create_tcp_server ⇒ Object
40 41 42 43 |
# File 'lib/wamp/router/server.rb', line 40 def create_tcp_server server = TCPServer.new(.fetch(:host, "127.0.0.1"), .fetch(:port, 8080)) selector.register(server, :r) end |
#options_message ⇒ Object
33 34 35 36 37 38 |
# File 'lib/wamp/router/server.rb', line 33 def host = .fetch(:host, "127.0.0.1") port = .fetch(:port, 8080) realm = .fetch(:realm, "realm1") puts "Starting router on ws://#{host}:#{port}/ws and added Realm: #{realm}" end |
#run ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wamp/router/server.rb', line 21 def run trap("INT") { throw :ctrl_c } create_tcp_server catch :ctrl_c do loop do accept_connection end end end |