/
WebSocketConnection.java
110 lines (89 loc) · 4.24 KB
/
WebSocketConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
package com.ubergeek42.weechat.relay.connection;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import javax.net.ssl.SSLSocketFactory;
public class WebSocketConnection extends AbstractConnection {
protected static Logger logger = LoggerFactory.getLogger("WebSocketConnection");
private WebSocketClient client;
private PipedOutputStream outputToInStream;
public WebSocketConnection(String server, int port, String path, SSLSocketFactory sslSocketFactory) throws URISyntaxException, IOException {
// can throw URISyntaxException
URI uri = new URI(sslSocketFactory == null ? "ws" : "wss", null, server, port, "/" + path, null, null);
// can throw IOException
in = new PipedInputStream();
outputToInStream = new PipedOutputStream();
outputToInStream.connect((PipedInputStream) in);
client = new MyWebSocket(uri, new Draft_17());
if (sslSocketFactory != null)
client.setSocket(sslSocketFactory.createSocket(server, port));
}
@Override protected void doConnect() throws Exception {
if (!client.connectBlocking()) throw (exception != null) ?
exception : new Exception("Could not connect using WebSocket");
}
// now this is one shady method
// client.close() does not close the socket. see TooTallNate/Java-WebSocket#346
// getConnection().closeConnection() seems to work, but i don't know if using it is right
@Override protected void doDisconnect() {
super.doDisconnect();
client.close();
client.getConnection().closeConnection(1000, "force closing");
try{outputToInStream.close();} catch (Exception e) {e.printStackTrace();}
}
// we don't need writer
@Override protected void startWriter(int i) {}
// because we have our own sendMessage()
@Override public void sendMessage(String string) {
try {client.send(string.getBytes());}
catch (WebsocketNotConnectedException ignored) {}
}
////////////////////////////////////////////////////////////////////////////////////////////////
private Exception exception = null;
private class MyWebSocket extends WebSocketClient {
public MyWebSocket(URI serverUri, Draft draft) {
super(serverUri, draft);
}
@Override public void onOpen(ServerHandshake ignored) {
logger.debug("WebSocket.onOpen(), readyState = {}", this.getReadyState());
}
@Override public void onMessage(String message) {
logger.debug("WebSocket.onMessage(string = {})", message);
throw new RuntimeException("Unexpected string message from websocket");
}
@Override public void onMessage(ByteBuffer bytes) {
logger.debug("WebSocket.onMessage({} bytes)", bytes.array().length);
try {
outputToInStream.write(bytes.array());
outputToInStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override public void onClose(int code, String reason, boolean remote) {
logger.debug("WebSocket.onClose(code = {}, reason = {})", code, reason);
try {outputToInStream.close();} catch (IOException e) {e.printStackTrace();}
}
// when connecting via SSL and when the connection is abruptly closed,
// onError() with `java.lang.NullPointerException: ssl == null` is thrown
@Override public void onError(Exception e) {
logger.error("WebSocket.onError({}: {})", e.getClass().getSimpleName(), e.getMessage());
exception = e;
try {outputToInStream.close();} catch (IOException ignored) {}
}
}
}