Class SocketHelper.SocketWrapper

  • Enclosing class:
    SocketHelper

    public class SocketHelper.SocketWrapper
    extends java.lang.Object
    Wrapper for connected TCP sockets. An instance of this class handles socket events from the Vert.x NetSocket object and translates them into JavaScript events emitted by the eventEmitter specified in the constructor. The events emitted are:
    • close: Emitted when the socket closes. It has no arguments.
    • error: Emitted when an error occurs. It is passed an error message.
    • data: Emitted when the socket received data. It is passed the data.
    This wrapper supports message framing, which ensures that data is emitted only when a complete message has arrived. To accomplish this, each message is prepended with a length, in bytes. To minimize overhead for short messages, then if the length of the message is less than 255 bytes, the length is encoded in a single byte. Otherwise, it is encoded as a byte with value 255 followed by an int (four bytes). This will only work if both ends of the socket connection are using this same protocol (e.g. if both ends are implemented using this same class).
    • Constructor Summary

      Constructors 
      Constructor Description
      SocketWrapper​(jdk.nashorn.api.scripting.ScriptObjectMirror eventEmitter, java.lang.Object socket, java.lang.String sendType, java.lang.String receiveType, boolean rawBytes, boolean emitBatchDataAsAvailable)
      Construct a handler for connections established.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the socket and remove all event listeners on the associated event emitter.
      void send​(java.lang.Object data)
      Send data over the socket.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SocketWrapper

        public SocketWrapper​(jdk.nashorn.api.scripting.ScriptObjectMirror eventEmitter,
                             java.lang.Object socket,
                             java.lang.String sendType,
                             java.lang.String receiveType,
                             boolean rawBytes,
                             boolean emitBatchDataAsAvailable)
        Construct a handler for connections established. This constructor must be called in the verticle thread, not in the director thread.
        Parameters:
        eventEmitter - The JavaScript object that will emit socket events.
        socket - The Vertx socket object.
        sendType - The send type.
        receiveType - The receive type.
        rawBytes - If true, send and received raw bytes, with no message framing. If false, then prepend each sent item with a length, and for received items, assume received data is prepended with a length and emit received data only when a complete message has arrived.
        emitBatchDataAsAvailable - Whether to emit all data available when the TCP stream is received and when rawBytes is true. This parameter is intended for socket.js module and this SocketHelper. Thus, this parameter will not be exposed to TCP socket accessors. Set this true only when the TCP stream is going to be handled by upper layer protocols to avoid non-deterministic behavior.
    • Method Detail

      • close

        public void close()
        Close the socket and remove all event listeners on the associated event emitter. This will be done asynchronously in the vertx thread.
      • send

        public void send​(java.lang.Object data)
        Send data over the socket.
        Parameters:
        data - The data to send.