This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
NvWebSocket.java
98 lines (86 loc) · 3.29 KB
/
NvWebSocket.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
package com.github.czyzby.websocket.impl;
import com.github.czyzby.websocket.data.WebSocketCloseCode;
import com.github.czyzby.websocket.data.WebSocketException;
import com.github.czyzby.websocket.data.WebSocketState;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketFactory;
/** Default web socket implementation for desktop and mobile platforms.
*
* @author MJ */
public class NvWebSocket extends AbstractWebSocket {
private final WebSocketFactory webSocketFactory = new WebSocketFactory();
private WebSocket webSocket;
public NvWebSocket(final String url) {
super(url);
}
@Override
public void connect() throws WebSocketException {
try {
dispose();
final WebSocket currentWebSocket = webSocket = webSocketFactory.createSocket(getUrl());
currentWebSocket.addListener(new NvWebSocketListener(this));
currentWebSocket.connect();
} catch (final Throwable exception) {
throw new WebSocketException("Unable to connect.", exception);
}
}
/** Removes current web socket instance. */
protected void dispose() {
final WebSocket currentWebSocket = webSocket;
if (currentWebSocket != null && currentWebSocket.isOpen()) {
try {
currentWebSocket.disconnect(WebSocketCloseCode.AWAY.getCode());
} catch (final Exception exception) {
postErrorEvent(exception);
}
}
}
@Override
public WebSocketState getState() {
final WebSocket currentWebSocket = webSocket;
return currentWebSocket == null ? WebSocketState.CLOSED : convertState(currentWebSocket.getState());
}
private static WebSocketState convertState(final com.neovisionaries.ws.client.WebSocketState state) {
switch (state) {
case CLOSED:
case CREATED:
return WebSocketState.CLOSED;
case CLOSING:
return WebSocketState.CLOSING;
case CONNECTING:
return WebSocketState.CONNECTING;
case OPEN:
return WebSocketState.OPEN;
}
return WebSocketState.CLOSED;
}
@Override
public boolean isSecure() {
final WebSocket currentWebSocket = webSocket;
return currentWebSocket != null && "wss".equalsIgnoreCase(currentWebSocket.getURI().getScheme());
}
@Override
public boolean isOpen() {
final WebSocket currentWebSocket = webSocket;
return currentWebSocket != null && currentWebSocket.isOpen();
}
@Override
public void close(final WebSocketCloseCode code, final String reason) throws WebSocketException {
final WebSocket currentWebSocket = webSocket;
if (currentWebSocket != null) {
try {
currentWebSocket.disconnect(code.getCode(), reason);
} catch (final Throwable exception) {
throw new WebSocketException("Unable to close the web socket.", exception);
}
}
}
@Override
protected void sendBinary(final byte[] packet) throws Exception {
webSocket.sendBinary(packet);
}
@Override
protected void sendString(final String packet) throws Exception {
webSocket.sendText(packet);
}
}