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;
}
}
}
}
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());
}
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);
}
}
}
Aggregations