org.webbitserver.WebSocketConnection - java examples

Here are the examples of the java api org.webbitserver.WebSocketConnection taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

50 Examples 7

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketOutboundData(WebSocketConnection connection, byte[] data) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-OUT-HEX", toHex(data));
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketConnectionOpen(WebSocketConnection connection) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-OPEN", null);
}

19 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onMessage(WebSocketConnection connection, byte[] message) throws Throwable {
    logSink.webSocketInboundData(connection, message);
    handler.onMessage(loggingConnection, message);
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketOutboundPong(WebSocketConnection connection, byte[] msg) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-OUT-PONG", toHex(msg));
}

19 View Complete Implementation : ReconnectingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onMessage(WebSocketConnection connection, String msg) throws Throwable {
    handler.onMessage(connection, msg);
}

19 View Complete Implementation : Manager.java
Copyright GNU General Public License v3.0
Author : Kjos
public clreplaced Manager extends BaseWebSocketHandler {

    private Viewer viewer = null;

    private WebSocketConnection connection;

    private Input input;

    private Server server;

    public Manager(Server server) throws AWTException {
        input = new Input();
        this.server = server;
    }

    public void sendData(byte[] data) {
        if (connection == null)
            return;
        connection.send(data);
    }

    public void sendEmptyImage(int framestamp) {
        byte[] data = new byte[] { 0, 0, 0, 0, 0 };
        data[1] = (byte) (framestamp & 0xff);
        framestamp >>= 8;
        data[2] = (byte) (framestamp & 0xff);
        framestamp >>= 8;
        data[3] = (byte) (framestamp & 0xff);
        framestamp >>= 8;
        data[4] = (byte) (framestamp & 0xff);
        sendData(data);
    }

    private void closeConnection(WebSocketConnection conn) {
        if (conn != null) {
            conn.close();
        }
        viewer = null;
        connection = null;
    }

    public void onOpen(WebSocketConnection conn) {
        viewer = new Viewer();
        connection = conn;
        System.out.println("Connection opened");
    }

    public void onClose(WebSocketConnection conn) {
        closeConnection(conn);
        System.out.println("Connection closed");
    }

    public Viewer getViewer() {
        return viewer;
    }

    @Override
    public void onMessage(WebSocketConnection connection, String message) {
        if (viewer == null)
            return;
        if (message.startsWith(">")) {
            viewer.frameUpdate(Integer.parseInt(message.substring(1)));
        } else {
            JSONObject obj = new JSONObject(message);
            if (obj.has("window")) {
                JSONArray ar = obj.getJSONArray("window");
                viewer.clientWidth = ar.getInt(0);
                viewer.clientHeight = ar.getInt(1);
                viewer.reset();
                server.resize(viewer.clientWidth, viewer.clientHeight);
            }
            if (obj.has("screenSwitch")) {
                int screen = Config.get().SELECTED_SCREEN;
                screen++;
                screen %= Config.get().SCREENS.length;
                System.out.println("Switch to screen: " + screen);
                viewer.reset();
                server.resize(screen, viewer.clientWidth, viewer.clientHeight);
            }
            input.parseInput(obj);
        }
    }
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketConnectionClose(WebSocketConnection connection) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-CLOSE", null);
}

19 View Complete Implementation : AudioManager.java
Copyright GNU General Public License v3.0
Author : Kjos
public clreplaced AudioManager extends BaseWebSocketHandler {

    private WebSocketConnection connection;

    private AudioRecorder audioRecorder;

    public AudioManager() throws AWTException {
        audioRecorder = new AudioRecorder(this);
    }

    public void sendData(byte[] data, int len) {
        if (connection == null)
            return;
        connection.send(data, 0, len);
    }

    private void closeConnection(WebSocketConnection conn) {
        if (conn != null) {
            conn.close();
        }
        connection = null;
    }

    public void onOpen(WebSocketConnection conn) {
        connection = conn;
        System.out.println("Audio connection opened");
    }

    public void onClose(WebSocketConnection conn) {
        closeConnection(conn);
        System.out.println("Audio connection closed");
    }

    @Override
    public void onMessage(WebSocketConnection connection, String message) {
    }
}

19 View Complete Implementation : ReconnectingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onMessage(WebSocketConnection connection, byte[] msg) throws Throwable {
    handler.onMessage(connection, msg);
}

19 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onPong(WebSocketConnection connection, byte[] message) throws Throwable {
    logSink.webSocketInboundPong(connection, message);
    handler.onPong(loggingConnection, message);
}

19 View Complete Implementation : WebbitWSHandler.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onMessage(WebSocketConnection connection, String message) {
    onMessage(connection, message.getBytes());
}

19 View Complete Implementation : StubHttpControl.java
Copyright GNU General Public License v3.0
Author : Kjos
public StubHttpControl webSocketConnection(WebSocketConnection connection) {
    this.webSocketConnection = connection;
    return this;
}

19 View Complete Implementation : Manager.java
Copyright GNU General Public License v3.0
Author : Kjos
private void closeConnection(WebSocketConnection conn) {
    if (conn != null) {
        conn.close();
    }
    viewer = null;
    connection = null;
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketInboundData(WebSocketConnection connection, byte[] data) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-IN-HEX", toHex(data));
}

19 View Complete Implementation : Manager.java
Copyright GNU General Public License v3.0
Author : Kjos
public void onOpen(WebSocketConnection conn) {
    viewer = new Viewer();
    connection = conn;
    System.out.println("Connection opened");
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketInboundData(WebSocketConnection connection, String data) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-IN-STRING", data);
}

19 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onMessage(WebSocketConnection connection, String message) throws Throwable {
    logSink.webSocketInboundData(connection, message);
    handler.onMessage(loggingConnection, message);
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketInboundPong(WebSocketConnection connection, byte[] msg) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-IN-PONG", toHex(msg));
}

19 View Complete Implementation : AudioManager.java
Copyright GNU General Public License v3.0
Author : Kjos
private void closeConnection(WebSocketConnection conn) {
    if (conn != null) {
        conn.close();
    }
    connection = null;
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketInboundPing(WebSocketConnection connection, byte[] msg) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-IN-PING", toHex(msg));
}

19 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onPing(WebSocketConnection connection, byte[] message) throws Throwable {
    logSink.webSocketInboundPing(connection, message);
    handler.onPing(loggingConnection, message);
}

19 View Complete Implementation : ReconnectingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onPing(WebSocketConnection connection, byte[] msg) throws Throwable {
    handler.onPing(connection, msg);
}

19 View Complete Implementation : AudioManager.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onMessage(WebSocketConnection connection, String message) {
}

19 View Complete Implementation : ReconnectingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onPong(WebSocketConnection connection, byte[] msg) throws Throwable {
    handler.onPong(connection, msg);
}

19 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
clreplaced LoggingWebSocketHandler implements WebSocketHandler {

    private final LogSink logSink;

    private final WebSocketConnection loggingConnection;

    private final WebSocketHandler handler;

    LoggingWebSocketHandler(LogSink logSink, WebSocketConnection loggingConnection, WebSocketHandler handler) {
        this.logSink = logSink;
        this.loggingConnection = loggingConnection;
        this.handler = handler;
    }

    @Override
    public void onOpen(WebSocketConnection connection) throws Throwable {
        logSink.webSocketConnectionOpen(connection);
        handler.onOpen(loggingConnection);
    }

    @Override
    public void onClose(WebSocketConnection connection) throws Throwable {
        logSink.webSocketConnectionClose(connection);
        logSink.httpEnd(connection.httpRequest());
        handler.onClose(loggingConnection);
    }

    @Override
    public void onMessage(WebSocketConnection connection, String message) throws Throwable {
        logSink.webSocketInboundData(connection, message);
        handler.onMessage(loggingConnection, message);
    }

    @Override
    public void onMessage(WebSocketConnection connection, byte[] message) throws Throwable {
        logSink.webSocketInboundData(connection, message);
        handler.onMessage(loggingConnection, message);
    }

    @Override
    public void onPing(WebSocketConnection connection, byte[] message) throws Throwable {
        logSink.webSocketInboundPing(connection, message);
        handler.onPing(loggingConnection, message);
    }

    @Override
    public void onPong(WebSocketConnection connection, byte[] message) throws Throwable {
        logSink.webSocketInboundPong(connection, message);
        handler.onPong(loggingConnection, message);
    }
}

19 View Complete Implementation : WebbitWSClient.java
Copyright Apache License 2.0
Author : lujian213
public clreplaced WebbitWSClient extends BaseWebSocketHandler {

    private Object lock = new Object();

    private URI uri;

    private WebSocketConnection delegation;

    private WebSocketConnection connection;

    private boolean useSSL = false;

    private String keystore;

    private String preplacedwd;

    private WebSocketClient wsClient;

    private SimScript script;

    public WebbitWSClient(URI uri, WebSocketConnection delegation, SimScript script) {
        this.uri = uri;
        this.delegation = delegation;
        this.script = script;
        useSSL = script.getBooleanProperty(PROP_NAME_USE_SSL, false);
        if (useSSL) {
            keystore = script.getMandatoryProperty(PROP_NAME_KEYSTORE, "no keystore defined");
            preplacedwd = script.getMandatoryProperty(PROP_NAME_KS_PreplacedWD, "no keystore preplacedwd defined");
        }
    }

    protected WebSocketConnection getConnection() {
        synchronized (lock) {
            while (true) {
                if (connection != null)
                    return connection;
                else {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
    }

    public void send(byte[] msg) {
        getConnection().send(msg);
    }

    public void start() throws IOException {
        wsClient = new WebSocketClient(uri, this);
        if (useSSL) {
            try (InputStream is = new FileInputStream(keystore)) {
                wsClient.setupSsl(is, preplacedwd);
            }
        }
        try {
            wsClient.start().get();
        } catch (ExecutionException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void stop() {
        getConnection().close();
        try {
            wsClient.stop().get();
        } catch (InterruptedException | ExecutionException e) {
            SimLogger.getLogger().error("error when stop ws client", e);
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        ((ExecutorService) wsClient.getExecutor()).shutdown();
    }

    @Override
    public void onOpen(WebSocketConnection connection) throws Exception {
        SimUtils.setThreadContext(script);
        SimLogger.getLogger().info("proxy connection established");
        synchronized (lock) {
            this.connection = connection;
            lock.notifyAll();
        }
    }

    @Override
    public void onClose(WebSocketConnection connection) throws Exception {
        SimUtils.setThreadContext(script);
        SimLogger.getLogger().info("proxy close");
        if (connection != null) {
            connection.close();
        }
    }

    @Override
    public void onMessage(WebSocketConnection connection, String msg) throws Throwable {
        SimUtils.setThreadContext(script);
        SimLogger.getLogger().info("proxy message:[" + msg + "]");
        delegation.send(msg);
    }
}

19 View Complete Implementation : WebSocketConnectionWrapper.java
Copyright GNU General Public License v3.0
Author : Kjos
public WebSocketConnectionWrapper underlyingControl(WebSocketConnection control) {
    this.connection = control;
    return this;
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketOutboundData(WebSocketConnection connection, String data) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-OUT-STRING", data);
}

19 View Complete Implementation : WebbitWSSimRequest.java
Copyright Apache License 2.0
Author : lujian213
public clreplaced WebbitWSSimRequest extends AbstractSimRequest {

    private static final String HEADER_LINE_FORMAT = "%s: %s";

    private static final String TOP_LINE_FORMAT = "%s %s %s";

    private String topLine;

    private String body;

    private WebbitWSHandler handler;

    private WebSocketConnection connection;

    private Map<String, String> headers = new HashMap<>();

    private ReqRespConvertor convertor;

    public WebbitWSSimRequest(WebbitWSHandler handler, WebSocketConnection connection, String channel, String type, byte[] message, ReqRespConvertor convertor) {
        this.handler = handler;
        this.connection = connection;
        this.convertor = convertor;
        this.topLine = SimUtils.formatString(TOP_LINE_FORMAT, type, channel, HTTP1_1);
        try {
            this.body = convertor.rawRequestToBody(message);
        } catch (IOException e) {
        }
    }

    protected WebbitWSSimRequest() {
    }

    @Override
    public ReqRespConvertor getReqRespConvertor() {
        return this.convertor;
    }

    public WebSocketConnection getConnection() {
        return connection;
    }

    public String getTopLine() {
        return this.topLine;
    }

    public String getHeaderLine(String header) {
        String value = headers.get(header);
        if (value == null) {
            return null;
        } else {
            return SimUtils.formatString(HEADER_LINE_FORMAT, header, value);
        }
    }

    public String getAuthenticationLine() {
        return null;
    }

    public String getBody() {
        return this.body;
    }

    @Override
    protected void doFillResponse(SimResponse resp) throws IOException {
        String actualChannel = (String) resp.getHeaders().get(HEADER_NAME_CHANNEL);
        String channelID = (String) resp.getHeaders().get(HEADER_NAME_CHANNEL_ID);
        if (channelID != null) {
            headers.put(HEADER_NAME_CHANNEL_ID, channelID);
        }
        if (actualChannel != null) {
            handler.sendResponse(actualChannel, resp);
        } else {
            convertor.fillRawResponse(connection, resp);
        }
    }

    @Override
    public List<String> getAllHeaderNames() {
        return new ArrayList<>(headers.keySet());
    }

    @Override
    public String getRemoteAddress() {
        if (connection != null) {
            String host = ((InetSocketAddress) connection.httpRequest().remoteAddress()).getAddress().getHostAddress();
            int port = ((InetSocketAddress) connection.httpRequest().remoteAddress()).getPort();
            return host + ":" + port;
        } else {
            return super.getRemoteAddress();
        }
    }
}

19 View Complete Implementation : AudioManager.java
Copyright GNU General Public License v3.0
Author : Kjos
public void onClose(WebSocketConnection conn) {
    closeConnection(conn);
    System.out.println("Audio connection closed");
}

19 View Complete Implementation : AudioManager.java
Copyright GNU General Public License v3.0
Author : Kjos
public void onOpen(WebSocketConnection conn) {
    connection = conn;
    System.out.println("Audio connection opened");
}

19 View Complete Implementation : Manager.java
Copyright GNU General Public License v3.0
Author : Kjos
public void onClose(WebSocketConnection conn) {
    closeConnection(conn);
    System.out.println("Connection closed");
}

19 View Complete Implementation : SimpleLogSink.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void webSocketOutboundPing(WebSocketConnection connection, byte[] msg) {
    custom(connection.httpRequest(), "WEB-SOCKET-" + connection.version() + "-OUT-PING", toHex(msg));
}

19 View Complete Implementation : WebSocketConnectionWrapper.java
Copyright GNU General Public License v3.0
Author : Kjos
public clreplaced WebSocketConnectionWrapper implements WebSocketConnection {

    private WebSocketConnection connection;

    public WebSocketConnectionWrapper(WebSocketConnection connection) {
        this.connection = connection;
    }

    public WebSocketConnection underlyingControl() {
        return connection;
    }

    public WebSocketConnectionWrapper underlyingControl(WebSocketConnection control) {
        this.connection = control;
        return this;
    }

    public WebSocketConnection originalControl() {
        if (connection instanceof WebSocketConnectionWrapper) {
            WebSocketConnectionWrapper wrapper = (WebSocketConnectionWrapper) connection;
            return wrapper.originalControl();
        } else {
            return connection;
        }
    }

    @Override
    public HttpRequest httpRequest() {
        return connection.httpRequest();
    }

    @Override
    public WebSocketConnectionWrapper send(String message) {
        connection.send(message);
        return this;
    }

    @Override
    public WebSocketConnectionWrapper send(byte[] message) {
        connection.send(message);
        return this;
    }

    @Override
    public WebSocketConnectionWrapper ping(byte[] msg) {
        connection.ping(msg);
        return this;
    }

    @Override
    public WebSocketConnectionWrapper send(byte[] message, int offset, int length) {
        connection.send(message, offset, length);
        return this;
    }

    @Override
    public WebSocketConnectionWrapper pong(byte[] msg) {
        connection.pong(msg);
        return this;
    }

    @Override
    public WebSocketConnectionWrapper close() {
        connection.close();
        return this;
    }

    @Override
    public Map<String, Object> data() {
        return connection.data();
    }

    @Override
    public Object data(String key) {
        return connection.data(key);
    }

    @Override
    public WebSocketConnectionWrapper data(String key, Object value) {
        connection.data(key, value);
        return this;
    }

    @Override
    public Set<String> dataKeys() {
        return connection.dataKeys();
    }

    @Override
    public Executor handlerExecutor() {
        return connection.handlerExecutor();
    }

    @Override
    public String version() {
        return connection.version();
    }

    @Override
    public void execute(Runnable command) {
        connection.execute(command);
    }
}

18 View Complete Implementation : Manager.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onMessage(WebSocketConnection connection, String message) {
    if (viewer == null)
        return;
    if (message.startsWith(">")) {
        viewer.frameUpdate(Integer.parseInt(message.substring(1)));
    } else {
        JSONObject obj = new JSONObject(message);
        if (obj.has("window")) {
            JSONArray ar = obj.getJSONArray("window");
            viewer.clientWidth = ar.getInt(0);
            viewer.clientHeight = ar.getInt(1);
            viewer.reset();
            server.resize(viewer.clientWidth, viewer.clientHeight);
        }
        if (obj.has("screenSwitch")) {
            int screen = Config.get().SELECTED_SCREEN;
            screen++;
            screen %= Config.get().SCREENS.length;
            System.out.println("Switch to screen: " + screen);
            viewer.reset();
            server.resize(screen, viewer.clientWidth, viewer.clientHeight);
        }
        input.parseInput(obj);
    }
}

18 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onClose(WebSocketConnection connection) throws Throwable {
    logSink.webSocketConnectionClose(connection);
    logSink.httpEnd(connection.httpRequest());
    handler.onClose(loggingConnection);
}

18 View Complete Implementation : LoggingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onOpen(WebSocketConnection connection) throws Throwable {
    logSink.webSocketConnectionOpen(connection);
    handler.onOpen(loggingConnection);
}

18 View Complete Implementation : ReconnectingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onOpen(WebSocketConnection connection) throws Throwable {
    handler.onOpen(connection);
}

18 View Complete Implementation : ReconnectingWebSocketHandler.java
Copyright GNU General Public License v3.0
Author : Kjos
@Override
public void onClose(WebSocketConnection connection) throws Throwable {
    handler.onClose(connection);
    scheduleReconnect();
}

18 View Complete Implementation : StubHttpControl.java
Copyright GNU General Public License v3.0
Author : Kjos
public clreplaced StubHttpControl implements HttpControl {

    private HttpRequest request;

    private HttpResponse response;

    private WebSocketHandler webSocketHandler;

    private WebSocketConnection webSocketConnection;

    public StubHttpControl() {
    }

    public StubHttpControl(HttpRequest request, HttpResponse response) {
        this.request = request;
        this.response = response;
    }

    public StubHttpControl(WebSocketConnection connection) {
        this.webSocketConnection = connection;
    }

    public StubHttpControl(HttpRequest request, HttpResponse response, WebSocketConnection connection) {
        this.request = request;
        this.response = response;
        this.webSocketConnection = connection;
    }

    public HttpRequest request() {
        return request;
    }

    public HttpResponse response() {
        return response;
    }

    public StubHttpControl request(HttpRequest request) {
        this.request = request;
        return this;
    }

    public StubHttpControl response(HttpResponse response) {
        this.response = response;
        return this;
    }

    @Override
    public void nextHandler() {
        nextHandler(request, response, this);
    }

    @Override
    public void nextHandler(HttpRequest request, HttpResponse response) {
        nextHandler(request, response, this);
    }

    @Override
    public void nextHandler(HttpRequest request, HttpResponse response, HttpControl control) {
        response.status(404).end();
    }

    @Override
    public WebSocketConnection upgradeToWebSocketConnection(WebSocketHandler handler) {
        this.webSocketHandler = handler;
        return this.webSocketConnection;
    }

    @Override
    public WebSocketConnection webSocketConnection() {
        return this.webSocketConnection;
    }

    @Override
    public EventSourceConnection upgradeToEventSourceConnection(EventSourceHandler handler) {
        throw new UnsupportedOperationException();
    // this.webSocketHandler = handler;
    // return webSocketConnection;
    }

    @Override
    public EventSourceConnection eventSourceConnection() {
        throw new UnsupportedOperationException();
    // return this.webSocketConnection;
    }

    public StubHttpControl webSocketConnection(WebSocketConnection connection) {
        this.webSocketConnection = connection;
        return this;
    }

    public WebSocketHandler webSocketHandler() {
        return webSocketHandler;
    }

    @Override
    public Executor handlerExecutor() {
        return this;
    }

    @Override
    public void execute(Runnable command) {
        command.run();
    }
}

18 View Complete Implementation : WebbitWSHandler.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onOpen(WebSocketConnection connection) {
    SimUtils.setThreadContext(script);
    SimLogger.getLogger().info("on open ...");
    WebbitWSSimRequest request = new WebbitWSSimRequest(this, connection, channel, TYPE_OPEN, null, convertor);
    List<SimResponse> respList = new ArrayList<>();
    try {
        respList = script.genResponse(request);
        SimUtils.logIncomingMessage(request.getRemoteAddress(), simulator.getName(), request);
        bundle.addConnection(bundle.new ConnectionWithDelegator(connection));
    } catch (Exception e) {
        if (proxy) {
            try {
                URL pURL = new URL(proxyURL);
                URI uri = new URI("http".equalsIgnoreCase(pURL.getProtocol()) ? "ws" : "wss", null, pURL.getHost(), pURL.getPort(), connection.httpRequest().uri(), null, null);
                SimLogger.getLogger().info("proxy url: " + uri);
                WebbitWSClient wsClient = new WebbitWSClient(uri, connection, script);
                wsClient.start();
                bundle.addConnection(bundle.new ConnectionWithDelegator(connection, wsClient));
                respList.add(new SimResponse("Unknown due to proxy mechanism"));
            } catch (IOException | URISyntaxException e1) {
                SimLogger.getLogger().error("proxy error", e1);
            }
        } else {
            SimLogger.getLogger().error("error when open WS [" + channel + "]", e);
        }
    } finally {
        handleIDChange(request);
        simulatorListener.onHandleMessage(simulator.getName(), request, respList, !respList.isEmpty());
    }
}

17 View Complete Implementation : WebbitWSClient.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onOpen(WebSocketConnection connection) throws Exception {
    SimUtils.setThreadContext(script);
    SimLogger.getLogger().info("proxy connection established");
    synchronized (lock) {
        this.connection = connection;
        lock.notifyAll();
    }
}

17 View Complete Implementation : WebbitWSClient.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onMessage(WebSocketConnection connection, String msg) throws Throwable {
    SimUtils.setThreadContext(script);
    SimLogger.getLogger().info("proxy message:[" + msg + "]");
    delegation.send(msg);
}

17 View Complete Implementation : WebbitWSClient.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onClose(WebSocketConnection connection) throws Exception {
    SimUtils.setThreadContext(script);
    SimLogger.getLogger().info("proxy close");
    if (connection != null) {
        connection.close();
    }
}

17 View Complete Implementation : WebbitWSHandler.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onMessage(WebSocketConnection connection, byte[] message) {
    SimUtils.setThreadContext(script);
    WebbitWSSimRequest request = new WebbitWSSimRequest(this, connection, channel, TYPE_MESSAGE, message, convertor);
    List<SimResponse> respList = new ArrayList<>();
    ConnectionWithDelegator conn = bundle.findConnection(connection);
    try {
        SimUtils.logIncomingMessage(request.getRemoteAddress(), simulator.getName(), request);
        respList = script.genResponse(request);
    } catch (Exception e) {
        if (proxy) {
            WebbitWSClient wsClient = conn.getDelegator();
            wsClient.send(message);
            respList.add(new SimResponse("Unknown due to proxy mechanism"));
        } else {
            SimLogger.getLogger().error("error when handle message in WS [" + channel + "]", e);
        }
    } finally {
        handleIDChange(request);
        simulatorListener.onHandleMessage(simulator.getName(), request, respList, !respList.isEmpty());
    }
}

17 View Complete Implementation : WebbitWSHandler.java
Copyright Apache License 2.0
Author : lujian213
@Override
public void onClose(WebSocketConnection connection) {
    SimUtils.setThreadContext(script);
    WebbitWSSimRequest request = new WebbitWSSimRequest(this, connection, channel, TYPE_CLOSE, null, convertor);
    List<SimResponse> respList = new ArrayList<>();
    ConnectionWithDelegator conn = bundle.findConnection(connection);
    try {
        SimUtils.logIncomingMessage(request.getRemoteAddress(), simulator.getName(), request);
        respList = script.genResponse(request);
    } catch (IOException e) {
        if (proxy) {
            WebbitWSClient wsClient = conn.getDelegator();
            wsClient.stop();
            respList.add(new SimResponse("Unknown due to proxy mechanism"));
        } else {
            SimLogger.getLogger().error("error when close WS [" + channel + "]", e);
        }
    } finally {
        bundle.removeConnection(conn);
        connection.close();
        simulatorListener.onHandleMessage(simulator.getName(), request, respList, !respList.isEmpty());
    }
}

16 View Complete Implementation : Monitor.java
Copyright GNU Affero General Public License v3.0
Author : agentcontest
private void broadcast(String message) {
    Lock lock = poolLock.readLock();
    lock.lock();
    try {
        for (WebSocketConnection client : this.pool) {
            client.send(message);
        }
    } finally {
        lock.unlock();
    }
}

16 View Complete Implementation : EventSink.java
Copyright GNU Affero General Public License v3.0
Author : agentcontest
@Override
public void onOpen(WebSocketConnection client) {
    Lock lock = poolLock.writeLock();
    lock.lock();
    try {
        pool.add(client);
        if (latestStatic != null)
            client.send(latestStatic);
        if (latestDynamic != null)
            client.send(latestDynamic);
        System.out.println(String.format("[ MONITOR ] %s: %d connection(s)", name, pool.size()));
    } finally {
        lock.unlock();
    }
}

16 View Complete Implementation : EventSink.java
Copyright GNU Affero General Public License v3.0
Author : agentcontest
public void broadcast(String message, boolean dynamic) {
    if (dynamic)
        this.latestDynamic = message;
    else
        this.latestStatic = message;
    Lock lock = poolLock.readLock();
    lock.lock();
    try {
        for (WebSocketConnection client : pool) {
            client.send(message);
        }
    } finally {
        lock.unlock();
    }
}

15 View Complete Implementation : EventSink.java
Copyright GNU Affero General Public License v3.0
Author : agentcontest
@Override
public void onClose(WebSocketConnection client) {
    Lock lock = poolLock.writeLock();
    lock.lock();
    try {
        pool.remove(client);
        System.out.println(String.format("[ MONITOR ] %s: %d connection(s)", name, pool.size()));
    } finally {
        lock.unlock();
    }
}