Module: WebSocket::ClientInitializationMethods

Included in:
Client
Defined in:
lib/websocket_client.rb

Overview

The ClientInitializationMethods module

Instance Method Summary collapse

Instance Method Details

#bootstrapObject



72
73
74
75
76
77
78
# File 'lib/websocket_client.rb', line 72

def bootstrap
  @bootstrap = Bootstrap.new
  @bootstrap.group(client_group)
  @bootstrap.channel(::WebSocket::CHANNEL_TYPE)
  @bootstrap.handler(logging_handler) if @options[:log_requests]
  @bootstrap.handler(channel_initializer)
end

#channel_initializerObject



88
89
90
# File 'lib/websocket_client.rb', line 88

def channel_initializer
  @channel_initializer ||= ::WebSocket::ClientChannelInitializer.new(@host, @port, @scheme, @options)
end

#client_groupObject



80
81
82
# File 'lib/websocket_client.rb', line 80

def client_group
  @client_group ||= NioEventLoopGroup.new
end

#configure_from_uriObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/websocket_client.rb', line 96

def configure_from_uri
  @uri = @options[:uri]
  @scheme = validate_scheme(@uri.getScheme()&.downcase || 'ws')
  @host = @uri.getHost() || '127.0.0.1'
  @port = @uri.getPort() || case @scheme
  when /ws/i then 80
  when /wss/i then 443
  else -1
  end
end

#configure_handlers(*handlers, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/websocket_client.rb', line 107

def configure_handlers(*handlers, &block)
  channel_initializer.default_handler.add_listener(self)
  channel_initializer.default_handler.listeners.addAll(handlers)
  @user_app = block
  @application_handler = lambda do |ctx, msg|
    if @user_app.nil? || @user_app.arity == 1
      @queue.add(msg.chomp)
    else
      @user_app.call(ctx, msg)
    end
  end
end

#init(options) ⇒ Object



66
67
68
69
70
# File 'lib/websocket_client.rb', line 66

def init(options)
  @options = options
  configure_from_uri
  @queue = java.util.concurrent.LinkedBlockingQueue.new
end

#logging_handlerObject



84
85
86
# File 'lib/websocket_client.rb', line 84

def logging_handler
  @logging_handler ||= LoggingHandler.new(LogLevel::INFO)
end

#validate_scheme(scheme) ⇒ Object



92
93
94
# File 'lib/websocket_client.rb', line 92

def validate_scheme(scheme)
  raise 'Only WS(S) is supported' unless ::WebSocket::SUPPORTED_SCHEMES.include?(scheme)
end