Class: Vertx::WebSocket

Inherits:
Object
  • Object
show all
Includes:
ReadStream, WriteStream
Defined in:
lib/vertx/http.rb

Overview

Represents a WebSocket.

Instances of this class are created by an HttpClient instance when a client succeeds in a WebSocket handshake with a server. Once an instance has been obtained it can be used to send or receive buffers of data from the connection, a bit like a TCP socket.

Author:

Instance Method Summary collapse

Methods included from ReadStream

#_to_read_stream, #data_handler, #end_handler, #exception_handler, #pause, #resume

Methods included from WriteStream

#_to_write_stream, #drain_handler, #exception_handler, #write, #write_queue_full?, #write_queue_max_size, #write_queue_max_size=

Constructor Details

#initialize(j_ws) ⇒ WebSocket

Returns a new instance of WebSocket.



485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/vertx/http.rb', line 485

def initialize(j_ws)
  @j_del = j_ws
  @binary_handler_id = EventBus.register_simple_handler { |msg|
    write_binary_frame(msg.body)
  }
  @text_handler_id = EventBus.register_simple_handler { |msg|
    write_text_frame(msg.body)
  }
  @j_del.closeHandler(Proc.new {
    EventBus.unregister_handler(@binary_handler_id)
    EventBus.unregister_handler(@text_handler_id)
    @close_handler.call if @close_handler
  })
end

Instance Method Details

#binary_handler_idObject

When a Websocket is created it automatically registers an event handler with the system, the ID of that handler is given by #binary_handler_id. Given this ID, a different event loop can send a binary frame to that event handler using the event bus. This allows you to write data to other WebSockets which are owned by different event loops.



523
524
525
# File 'lib/vertx/http.rb', line 523

def binary_handler_id
  @binary_handler_id
end

#closeObject

Close the WebSocket



515
516
517
# File 'lib/vertx/http.rb', line 515

def close
  @j_del.close
end

#close_handler(&hndlr) ⇒ Object

Set a closed handler on the WebSocket.

Parameters:

  • hndlr (Block)

    A block to be used as the handler



537
538
539
# File 'lib/vertx/http.rb', line 537

def close_handler(&hndlr)
  @close_handler = hndlr;
end

#text_handler_idObject

When a Websocket is created it automatically registers an event handler with the system, the ID of that handler is given by #text_handler_id. Given this ID, a different event loop can send a text frame to that event handler using the event bus. This allows you to write data to other WebSockets which are owned by different event loops.



531
532
533
# File 'lib/vertx/http.rb', line 531

def text_handler_id
  @text_handler_id
end

#write_binary_frame(buffer) ⇒ Object

Write data to the WebSocket as a binary frame

Parameters:

  • buffer. (Buffer)

    Data to write.



502
503
504
505
# File 'lib/vertx/http.rb', line 502

def write_binary_frame(buffer)
  @j_del.writeBinaryFrame(buffer._to_java_buffer)
  self
end

#write_text_frame(str) ⇒ Object

Write data to the WebSocket as a text frame

Parameters:

  • str. (String)

    String to write.



509
510
511
512
# File 'lib/vertx/http.rb', line 509

def write_text_frame(str)
  @j_del.writeTextFrame(str)
  self
end