Class: Patch::IO::Websocket::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/patch/io/websocket/socket.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocket

Returns a new instance of Socket.



15
16
17
# File 'lib/patch/io/websocket/socket.rb', line 15

def initialize
  @onmessage = []
end

Class Method Details

.start(config) ⇒ Object



9
10
11
12
13
# File 'lib/patch/io/websocket/socket.rb', line 9

def self.start(config)
  socket = new
  socket.start(config)
  socket
end

Instance Method Details

#active?Boolean

Is the socket active?

Returns:

  • (Boolean)


58
59
60
# File 'lib/patch/io/websocket/socket.rb', line 58

def active?
  !@socket.nil?
end

#disableBoolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/patch/io/websocket/socket.rb', line 24

def disable
  @socket.onmessage = nil
  @onmessage.clear
  true
end

#on_message(&callback) ⇒ Boolean

Parameters:

  • callback (Proc)

    callback to fire when events are received

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/patch/io/websocket/socket.rb', line 32

def on_message(&callback)
  if @socket.nil?
    @onmessage << callback
  else
    @socket.onmessage { |data| yield(data) }
  end
  true
end

#puts(data) ⇒ Object



19
20
21
# File 'lib/patch/io/websocket/socket.rb', line 19

def puts(data)
  @socket.send(data)
end

#start(config, &block) ⇒ Boolean

Start the websocket

Parameters:

  • config (Hash)

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/patch/io/websocket/socket.rb', line 44

def start(config, &block)
  EM::WebSocket.run(config) do |websocket|
    ::Thread.current.abort_on_exception = true
    begin
      enable(websocket)
    rescue Exception => exception
      ::Thread.main.raise(exception)
    end
  end
  true
end