Search in sources :

Example 1 with WebSocket

use of org.java_websocket.WebSocket in project quorrabot by GloriousEggroll.

the class EventWebSocketServer method sendToAll.

@Subscribe
public void sendToAll(Event event) {
    Collection<WebSocket> con = connections();
    synchronized (con) {
        for (WebSocket c : con) {
            try {
                Method output = event.getClass().getMethod("toEventSocket", (Class<?>[]) null);
                c.send(event.getClass().getSimpleName() + ":" + output.invoke(event, (Object[]) null));
            } catch (Exception ex) {
                return;
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) WebSocket(org.java_websocket.WebSocket) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with WebSocket

use of org.java_websocket.WebSocket in project quorrabot by GloriousEggroll.

the class WebSocketClient method run.

public void run() {
    try {
        if (socket == null) {
            socket = new Socket(proxy);
        } else if (socket.isClosed()) {
            throw new IOException();
        }
        if (!socket.isBound()) {
            socket.connect(new InetSocketAddress(uri.getHost(), getPort()), connectTimeout);
        }
        istream = socket.getInputStream();
        ostream = socket.getOutputStream();
        sendHandshake();
    } catch (/*IOException | SecurityException | UnresolvedAddressException | InvalidHandshakeException | ClosedByInterruptException | SocketTimeoutException */
    Exception e) {
        onWebsocketError(engine, e);
        engine.closeConnection(CloseFrame.NEVER_CONNECTED, e.getMessage());
        return;
    }
    writeThread = new Thread(new WebsocketWriteThread());
    writeThread.start();
    byte[] rawbuffer = new byte[WebSocketImpl.RCVBUF];
    int readBytes;
    try {
        while (!isClosed() && (readBytes = istream.read(rawbuffer)) != -1) {
            engine.decode(ByteBuffer.wrap(rawbuffer, 0, readBytes));
        }
        engine.eot();
    } catch (IOException e) {
        engine.eot();
    } catch (RuntimeException e) {
        // this catch case covers internal errors only and indicates a bug in this websocket implementation
        onError(e);
        engine.closeConnection(CloseFrame.ABNORMAL_CLOSE, e.getMessage());
    }
    assert (socket.isClosed());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Socket(java.net.Socket) WebSocket(org.java_websocket.WebSocket) IOException(java.io.IOException) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException) NotYetConnectedException(java.nio.channels.NotYetConnectedException)

Example 3 with WebSocket

use of org.java_websocket.WebSocket in project quorrabot by GloriousEggroll.

the class WebSocketServer method stop.

/**
     * Closes all connected clients sockets, then closes the underlying
     * ServerSocketChannel, effectively killing the server socket
     * selectorthread, freeing the port the server was bound to and stops all
     * internal workerthreads.
     *
     * If this method is called before the server is started it will never
     * start.
     *
     * @param timeout Specifies how many milliseconds the overall close
     * handshaking may take altogether before the connections are closed without
     * proper close handshaking.<br>
     *
     * @throws InterruptedException
     */
public void stop(int timeout) throws InterruptedException {
    if (!isclosed.compareAndSet(false, true)) {
        // this also makes sure that no further connections will be added to this.connections
        return;
    }
    List<WebSocket> socketsToClose = null;
    // copy the connections in a list (prevent callback deadlocks)
    synchronized (connections) {
        socketsToClose = new ArrayList<WebSocket>(connections);
    }
    for (WebSocket ws : socketsToClose) {
        ws.close(CloseFrame.GOING_AWAY);
    }
    synchronized (this) {
        if (selectorthread != null && selectorthread != Thread.currentThread()) {
            selector.wakeup();
            selectorthread.interrupt();
            selectorthread.join(timeout);
        }
    }
}
Also used : WebSocket(org.java_websocket.WebSocket)

Aggregations

WebSocket (org.java_websocket.WebSocket)3 Subscribe (com.google.common.eventbus.Subscribe)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 InvalidHandshakeException (org.java_websocket.exceptions.InvalidHandshakeException)1