Class: WebSocketServer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/helper/websocket_server.rb

Overview

WebSocket Server where the USMF messages will be published

Author:

  • Daniel Machado Fernandez

Version:

  • 1.0

Constant Summary

Constants included from Logging

Logging::KermitPFC

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initializeWebSocketServer

Configures the WebSocket to works in test mode (path controversia)



18
19
20
21
22
# File 'lib/helper/websocket_server.rb', line 18

def initialize

  logger.info("Starting WebSocketServer...")

end

Instance Method Details

#startObject

Starts the Websocket server and listen the new clients to broadcast the messages



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/helper/websocket_server.rb', line 25

def start
  @redis = Redis.new(:host => "#{Settings.redis.host}", :port => Settings.redis.port)

  
  Thread.new do
    EventMachine.run do
    	puts '='*80, "Starting websockets server at ws://#{Settings.websocket.host}:#{Settings.websocket.port}", '='*80
      
      EventMachine::WebSocket.start(:host => "#{Settings.websocket.host}", :port => Settings.websocket.port) do |ws|
        ws.onopen do
          
          puts "#{Time.now.strftime('%H:%M:%S')} : Client connected", '-'*80
          SOCKETS << ws
        end

        ws.onclose do
          
          puts "#{Time.now.strftime('%H:%M:%S')} : Client disconnected", '-'*80
          SOCKETS.delete ws
        end
      end
    end
  end

  
  Thread.new do
    @redis.subscribe('ws') do |on|
      
      on.message do |chan, msg|
      
       puts "#{msg}"
       SOCKETS.each {|s| s.send msg} 
      
      end
    end
  end

  sleep
  
end