Class SocketHelper


  • public class SocketHelper
    extends VertxHelperBase
    A helper class for the socket module in JavaScript. You should use getOrCreateHelper(Object, ScriptObjectMirror) to create exactly one instance of this helper per actor. Pass the actor as an argument. A confusing aspect of this design is the socket client will have exactly one socket associated with it, whereas a socket server can have any number of sockets associated with it. In any case, there should be only one instance of this class associated with any actor. This ensures that all the socket actions and callbacks managed by this instance execute in a single verticle. This class supports transmission of strings, images, and binary numerical data of type byte, double, float, int, long, short, unsigned byte, unsigned int, and unsigned short, and arrays of all the numerical type. If you specify the send type to be "image", then a default JPG encoding will be used for transmission. Otherwise, the specific image type encoding that you specify will be used. For received images, any image type supported by the ImageIO readers can be accepted, so you just need to specify "image".
    Since:
    Ptolemy II 11.0
    Version:
    $Id$
    Author:
    Edward A. Lee, Contributor: Hokeun Kim
    Pt.AcceptedRating:
    Red (eal)
    Pt.ProposedRating:
    Yellow (eal)
    • Constructor Detail

      • SocketHelper

        public SocketHelper​(java.lang.Object actor,
                            jdk.nashorn.api.scripting.ScriptObjectMirror helping)
        Constructor for SocketHelper for the specified actor.
        Parameters:
        actor - The actor that this will help.
        helping - The JavaScript object that this is helping.
    • Method Detail

      • openClientSocket

        public void openClientSocket​(jdk.nashorn.api.scripting.ScriptObjectMirror socketClient,
                                     int port,
                                     java.lang.String host,
                                     java.util.Map<java.lang.String,​java.lang.Object> options)
        Create a client-side socket on behalf of the specified JavaScript SocketClient object. After this is called, the specified socketClient will emit the following events: * open: Emitted when the connection has been established with the server. This will not be passed any arguments. * data: Emitted when data is received on the socket. The received data will be an argument to the event. * close: Emitted when a socket is closed. This will not be passed any arguments. * error: Emitted when an error occurs. This will be passed an error message.
        Parameters:
        socketClient - The JavaScript SocketClient instance.
        port - The remote port to connect to.
        host - The remote host to connect to.
        options - The options (see the socket.js JavaScript module).
      • getOrCreateHelper

        public static SocketHelper getOrCreateHelper​(java.lang.Object actor,
                                                     jdk.nashorn.api.scripting.ScriptObjectMirror helping)
        Get or create a helper for the specified actor. If one has been created before and has not been garbage collected, return that one. Otherwise, create a new one.
        Parameters:
        actor - Either a JavaScript actor or a RestrictedJavaScriptInterface.
        helping - The JavaScript object that this is helping.
        Returns:
        The SocketHelper.
      • supportedReceiveTypes

        public static java.lang.String[] supportedReceiveTypes()
        Return an array of the types supported by the current host for receiveType arguments.
        Returns:
        An array of types.
      • supportedSendTypes

        public static java.lang.String[] supportedSendTypes()
        Return an array of the types supported by the current host for sendType arguments.
        Returns:
        An array of types.
      • startServer

        public void startServer​(jdk.nashorn.api.scripting.ScriptObjectMirror socketServer,
                                java.util.Map<java.lang.String,​java.lang.Object> options)
        Create a server that can accept socket connection requests on behalf of the specified JavaScript SocketServer object. After this is called, the specified JavaScript SocketServer object will emit the following events:
        • listening: Emitted when the server is listening. This will be passed the port number that the server is listening on (this is useful if the port is specified to be 0).
        • connection: Emitted when a new connection is established after a request from (possibly remote) client. This will be passed an instance of the JavaScript Socket class that is defined in the socket.js module. That instance has a send() and close() function that can be used to send data or to close the socket. It is also an event emitter that emits 'close', 'data', and 'error' events.
        • error: If this server fails to start listening. An error message will be passed to any event handler.
        Parameters:
        socketServer - The JavaScript SocketServer instance.
        options - The options (see the socket.js JavaScript module).