Module: NIO::WebSocket
- Defined in:
- lib/nio/websocket.rb,
lib/nio/websocket/adapter.rb,
lib/nio/websocket/reactor.rb,
lib/nio/websocket/version.rb,
lib/nio/websocket/raw_adapter.rb,
lib/nio/websocket/adapter/proxy.rb,
lib/nio/websocket/adapter/client.rb,
lib/nio/websocket/adapter/server.rb
Defined Under Namespace
Classes: Adapter, RawAdapter, Reactor
Constant Summary collapse
- SERVER_ADAPTER =
NIO::WebSocket::Adapter::Server
- CLIENT_ADAPTER =
NIO::WebSocket::Adapter::Client
- PROXY_ADAPTER =
NIO::WebSocket::Adapter::Proxy
- VERSION =
"0.7.0".freeze
Class Attribute Summary collapse
-
.logger ⇒ Logger
Returns the current logger, or creates one at level ERROR if one has not been assigned.
Class Method Summary collapse
-
.connect(url, options = {}, io = nil) {|::WebSocket::Driver| ... } ⇒ ::WebSocket::Driver
Create and return a websocket client that communicates either over the given IO object (upgrades the connection), or we’ll create a new connection to url if io is not supplied.
-
.listen(options = {}, server = nil) {|::WebSocket::Driver| ... } ⇒ Object
Start handling new connections, passing each through the supplied block.
-
.log_traffic=(enable) ⇒ Object
Should raw traffic be logged through the logger? Disabled by default for security reasons.
-
.log_traffic? ⇒ Boolean
Should raw traffic be logged through the logger? Disabled by default for security reasons.
-
.proxy(remote, options = {}) {|::WebSocket::Driver| ... } ⇒ Object
Establish a proxy host listening on the given port and address, that marshalls all data to/from a new connection on remote.
-
.reset ⇒ Object
Resets this API to a fresh state.
Class Attribute Details
.logger ⇒ Logger
Returns the current logger, or creates one at level ERROR if one has not been assigned
18 19 20 21 22 23 24 |
# File 'lib/nio/websocket.rb', line 18 def logger @logger ||= begin logger = Logger.new(STDERR, progname: "WebSocket", level: Logger::ERROR) logger.level = Logger::ERROR logger end end |
Class Method Details
.connect(url, options = {}, io = nil) {|::WebSocket::Driver| ... } ⇒ ::WebSocket::Driver
Create and return a websocket client that communicates either over the given IO object (upgrades the connection), or we’ll create a new connection to url if io is not supplied
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/nio/websocket.rb', line 49 def connect(url, = {}, io = nil) io ||= open_socket(url, ) adapter = CLIENT_ADAPTER.new(url, io, ) yield(adapter.driver, adapter) if block_given? Reactor.queue_task do adapter.add_to_reactor end Reactor.start logger.info "Client #{io} connected to #{url}" adapter.driver end |
.listen(options = {}, server = nil) {|::WebSocket::Driver| ... } ⇒ Object
Start handling new connections, passing each through the supplied block
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/nio/websocket.rb', line 100 def listen( = {}, server = nil) server ||= create_server() Reactor.queue_task do monitor = Reactor.selector.register(server, :r) monitor.value = proc do accept_socket server, do |io| # this next block won't run until ssl (if enabled) has started adapter = SERVER_ADAPTER.new(io, ) yield(adapter.driver, adapter) if block_given? Reactor.queue_task do adapter.add_to_reactor end logger.info "Host accepted client connection #{io} on port #{[:port]}" end end end Reactor.start logger.info "Host listening for new connections on port " + [:port].to_s server end |
.log_traffic=(enable) ⇒ Object
Should raw traffic be logged through the logger? Disabled by default for security reasons
30 31 32 33 |
# File 'lib/nio/websocket.rb', line 30 def log_traffic=(enable) @log_traffic = enable logger.level = Logger::DEBUG if enable end |
.log_traffic? ⇒ Boolean
Should raw traffic be logged through the logger? Disabled by default for security reasons
36 37 38 |
# File 'lib/nio/websocket.rb', line 36 def log_traffic? @log_traffic end |
.proxy(remote, options = {}) {|::WebSocket::Driver| ... } ⇒ Object
Establish a proxy host listening on the given port and address, that marshalls all data to/from a new connection on remote
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/nio/websocket.rb', line 69 def proxy(remote, = {}) server = create_server() host, port, extra = remote.split(":", 3) raise "Specify the remote parameter in 'hostname_or_ip:port' format" if extra || port.to_i == 0 || host.empty? Reactor.queue_task do monitor = Reactor.selector.register(server, :r) monitor.value = proc do accept_socket server, do |client| srv = open_socket "tcp://#{remote}", adapter = PROXY_ADAPTER.new(srv, client, ) Reactor.queue_task do adapter.add_to_reactor end logger.info "Proxy connection established between #{srv} and #{client}" end end end logger.info "Proxy Host listening for new connections on port " + [:port].to_s Reactor.start server end |