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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/websocket_client/client.rb', line 28

def initialize(uri,handshake_class=WebSocketClient::DEFAULT_HANDSHAKE, &block)
  @handshake_class = handshake_class
  @socket = nil
  @on_message_handler = nil
  @on_disconnect_handler = nil
  @handler_thread = nil
  @close_state = nil
  case ( uri )
    when String
      @uri = URI.parse( uri )
    else
      @uri = uri
  end
  block.call( self ) if block
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



26
27
28
# File 'lib/websocket_client/client.rb', line 26

def uri
  @uri
end

Instance Method Details

#connect(&block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/websocket_client/client.rb', line 44

def connect(&block)
  @socket = TCPSocket.open(uri.host, uri.port)
  puts "> handshaking"
  @handshake = @handshake_class.new( uri, @socket )
  puts "> handshook"
  
  start_handler
  
  if ( block )
    begin
      block.call( self ) 
    ensure
      disconnect()
    end
  end
end

#disconnectObject



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

def disconnect
  @socket.puts 0xFF
  @socket.putc 0x00
  @close_state = :requested
end

#on_disconnect(&block) ⇒ Object



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

def on_disconnect(&block)
  if ( block )
    @on_disconnect_handler = block
  else
    puts "> on_disconnect"
    @on_disconnect_handler.call( msg ) if @on_disconnect_handler
  end
end

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



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

def on_message(msg=nil,&block)
  if ( block ) 
    @on_message_handler = block
  else
    puts "> on_message #{msg}"
    @on_message_handler.call( msg ) if @on_message_handler
  end
end

#send(msg) ⇒ Object



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

def send(msg)
  @handshake.encode_text_message( msg )
  @socket.flush
end

#start_handlerObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/websocket_client/client.rb', line 94

def start_handler
  @handler_thread = Thread.new(@socket) do |socket|
    msg = ''
    msg_state = :none
    while ( ! socket.eof? )
      c = nil
      if ( socket.respond_to?( :getbyte ) )
        c = socket.getbyte
      else
        c = socket.getc
      end
      puts "> #{c} #{c.class} #{c == 0xff}"
      if ( c == 0x00 ) 
        if ( msg_state == :half_closed )
          puts "> full-closed by server"
          socket.close
          break
        else
          if ( @close_state == :half_closed )
            socket.close
            break
          else
            msg_state = :none
          end
        end
      elsif ( c == 0xFF ) 
        if ( msg_state == :none )
          msg_state = :half_closed
          puts "> half-closed by server"
        else
          if ( @close_state == :requested )
            @close_state = :half_closed
          else
            on_message( msg ) 
            msg = ''
            msg_state = :none
          end
        end
      else
        if ( @close_state != nil )
          @close_state = nil
        end
        msg_state = :in_progress
        msg << c
      end
    end
    on_disconnect
  end
end

#wait_foreverObject



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

def wait_forever()
  @handler_thread.join
end