Module: Slanger::WebSocketServer

Extended by:
WebSocketServer
Included in:
WebSocketServer
Defined in:
lib/slanger/web_socket_server.rb

Instance Method Summary collapse

Instance Method Details

#runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/slanger/web_socket_server.rb', line 6

def run
  case
  when EM.epoll? then EM.epoll
  when EM.kqueue? then EM.kqueue
  end

  EM.run do
    options = {
      host: Slanger::Config[:websocket_host],
      port: Slanger::Config[:websocket_port],
      debug: Slanger::Config[:debug],
      app_key: Slanger::Config[:app_key],
    }

    if Slanger::Config[:tls_options]
      options.merge! secure: true,
                     tls_options: Slanger::Config[:tls_options]
    end

    EM::WebSocket.start options do |ws|
      # Keep track of handler instance in instance of EM::Connection to ensure a unique handler instance is used per connection.
      ws.class_eval { attr_accessor :connection_handler }
      # Delegate connection management to handler instance.
      ws.onopen { |handshake| ws.connection_handler = Slanger::Config.socket_handler.new ws, handshake }
      ws.onmessage { |msg| ws.connection_handler.onmessage msg }
      ws.onclose { ws.connection_handler.onclose }
    end
  end
end