Class: WebSocketClient::Client
- Inherits:
-
Object
- Object
- WebSocketClient::Client
- Defined in:
- lib/websocket_client/client.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #connect(&block) ⇒ Object
- #disconnect ⇒ Object
-
#initialize(uri, handshake_class = WebSocketClient::DEFAULT_HANDSHAKE, &block) ⇒ Client
constructor
A new instance of Client.
- #on_disconnect(&block) ⇒ Object
- #on_message(msg = nil, &block) ⇒ Object
- #send(msg) ⇒ Object
- #start_handler ⇒ Object
- #wait_forever ⇒ Object
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 = 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
#uri ⇒ Object (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 |
#disconnect ⇒ Object
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 (msg=nil,&block) if ( block ) = block else puts "> on_message #{msg}" .call( msg ) if end end |
#send(msg) ⇒ Object
67 68 69 70 |
# File 'lib/websocket_client/client.rb', line 67 def send(msg) @handshake.( msg ) @socket.flush end |
#start_handler ⇒ Object
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 ( 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_forever ⇒ Object
72 73 74 |
# File 'lib/websocket_client/client.rb', line 72 def wait_forever() @handler_thread.join end |