Module: WebSocket::InstanceMethods

Included in:
Server
Defined in:
lib/websocket/instance_methods.rb

Overview

The InstanceMethods module

Instance Method Summary collapse

Instance Method Details

#<<(handler) ⇒ Object



96
97
98
# File 'lib/websocket/instance_methods.rb', line 96

def <<(handler)
  channel_initializer.handlers << handler
end

#add_listener(*listener) ⇒ Object



100
101
102
# File 'lib/websocket/instance_methods.rb', line 100

def add_listener(*listener)
  channel_initializer.add_listener(*listener)
end

#bootstrapObject



35
36
37
38
39
40
41
# File 'lib/websocket/instance_methods.rb', line 35

def bootstrap
  @bootstrap = ServerBootstrap.new
  @bootstrap.group(boss_group, worker_group)
  @bootstrap.channel(Server::CHANNEL_TYPE)
  @bootstrap.handler(logging_handler) if @options[:log_requests]
  @bootstrap.childHandler(channel_initializer)
end

#boss_groupObject



47
48
49
# File 'lib/websocket/instance_methods.rb', line 47

def boss_group
  @boss_group ||= NioEventLoopGroup.new(2)
end

#channel_groupObject



55
56
57
# File 'lib/websocket/instance_methods.rb', line 55

def channel_group
  @channel_group ||= DefaultChannelGroup.new('server_channels', GlobalEventExecutor::INSTANCE)
end

#channel_initializerObject



43
44
45
# File 'lib/websocket/instance_methods.rb', line 43

def channel_initializer
  @channel_initializer ||= ::WebSocket::ChannelInitializer.new(channel_group, @options)
end

#configure_handlers(&block) ⇒ Object



30
31
32
33
# File 'lib/websocket/instance_methods.rb', line 30

def configure_handlers(&block)
  add_listener(self)
  channel_initializer << block if block_given?
end

#logging_handlerObject



59
60
61
# File 'lib/websocket/instance_methods.rb', line 59

def logging_handler
  LoggingHandler.new(LogLevel::INFO)
end

#portObject

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength



82
83
84
# File 'lib/websocket/instance_methods.rb', line 82

def port
  @port ||= (@options[:ssl] ? @options[:ssl_port] : @options[:port]).to_i
end

#run(params = {}) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/websocket/instance_methods.rb', line 65

def run(params = {})
  @options.merge!(params)
  channel = bootstrap.bind(port).sync().channel()
  channel_group.add(channel)
  ::WebSocket::ShutdownHook.new(self)
  log.info "Listening on #{channel.local_address}"
  channel.closeFuture().sync()
rescue java.net.BindException => e
  raise "Bind error: #{e.message}: #{@options[:host]}:#{port}"
rescue java.net.SocketException => e
  raise "Socket error: #{e.message}: #{@options[:host]}:#{port}"
ensure
  stop
end

#shutdownObject



86
87
88
89
# File 'lib/websocket/instance_methods.rb', line 86

def shutdown
  channel_group.disconnect().awaitUninterruptibly()
  channel_group.close().awaitUninterruptibly()
end

#stopObject



91
92
93
94
# File 'lib/websocket/instance_methods.rb', line 91

def stop
  boss_group&.shutdownGracefully()
  worker_group&.shutdownGracefully()
end

#worker_groupObject



51
52
53
# File 'lib/websocket/instance_methods.rb', line 51

def worker_group
  @worker_group ||= NioEventLoopGroup.new
end