Class: WebSocketClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/websocket_client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, handshake_class = WebSocketClient::DEFAULT_HANDSHAKE, &block) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
27
28
29
# File 'lib/websocket_client/client.rb', line 21

def initialize(uri,handshake_class=WebSocketClient::DEFAULT_HANDSHAKE, &block)
  @handshake_class       = handshake_class
  @on_message_handler    = nil
  @on_disconnect_handler = nil
  @handler_thread        = nil
  @uri                   = cleanse_uri( uri )

  block.call( self ) if block
end

Instance Attribute Details

#sinkObject (readonly)

Returns the value of attribute sink.



19
20
21
# File 'lib/websocket_client/client.rb', line 19

def sink
  @sink
end

#sourceObject (readonly)

Returns the value of attribute source.



18
19
20
# File 'lib/websocket_client/client.rb', line 18

def source
  @source
end

#uriObject (readonly)

Returns the value of attribute uri.



16
17
18
# File 'lib/websocket_client/client.rb', line 16

def uri
  @uri
end

Instance Method Details

#cleanse_uri(uri) ⇒ Object



31
32
33
34
# File 'lib/websocket_client/client.rb', line 31

def cleanse_uri(uri)
  return URI.parse( uri ) if ( String === uri )
  uri
end

#connect(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/websocket_client/client.rb', line 36

def connect(&block)
  socket = TCPSocket.open(uri.host, uri.port)
  @source = BufferedByteSource.new( SocketByteSource.new( socket ) )
  @sink   = SocketByteSink.new( socket )
  @handshake = @handshake_class.new( uri, @source, @sink )

  internal_connect(source, sink)
  run_client( &block )
end

#disconnect(wait_for = false) ⇒ Object



63
64
65
66
# File 'lib/websocket_client/client.rb', line 63

def disconnect(wait_for=false)
  @frame_writer.write_close_frame() 
  wait_for_disconnect if wait_for
end

#internal_connect(source, sink) ⇒ Object



46
47
48
49
# File 'lib/websocket_client/client.rb', line 46

def internal_connect(source, sink)
  @frame_reader = FrameReader.new( source )
  @frame_writer = FrameWriter.new( sink )
end

#on_connect(&block) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/websocket_client/client.rb', line 76

def on_connect(&block)
  if ( block )
    @on_connect_handler = block
  else
    @on_connect_handler.call() if @on_connect_handler
  end
end

#on_disconnect(&block) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/websocket_client/client.rb', line 92

def on_disconnect(&block)
  if ( block )
    @on_disconnect_handler = block
  else
    @on_disconnect_handler.call() if @on_disconnect_handler
  end
end

#on_message(msg = nil, &block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/websocket_client/client.rb', line 84

def on_message(msg=nil,&block)
  if ( block ) 
    @on_message_handler = block
  else
    @on_message_handler.call( msg ) if @on_message_handler
  end
end

#run_client(&block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/websocket_client/client.rb', line 51

def run_client(&block)
  on_connect
  start_handler
  if ( block )
    begin
        block.call( self ) 
    ensure
      disconnect()
    end
  end
end

#run_handler_loopObject



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

def run_handler_loop
  while ( ! @frame_reader.eof? )
    frame = @frame_reader.read_frame
    case ( frame.type )
      when :text
        on_message( frame.text )
      when :close
        break
    end
  end
  on_disconnect
end

#send(text) ⇒ Object



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

def send(text)
  @frame_writer.write_as_text_frame( text )
end

#start_handlerObject



100
101
102
103
104
# File 'lib/websocket_client/client.rb', line 100

def start_handler
  @handler_thread = Thread.new() do
    run_handler_loop
  end
end

#wait_for_disconnectObject



72
73
74
# File 'lib/websocket_client/client.rb', line 72

def wait_for_disconnect()
  @handler_thread.join
end