Skip to content

Commit e3965ec

Browse files
committed
2.4.0
1 parent 50b7965 commit e3965ec

File tree

7 files changed

+23
-27
lines changed

7 files changed

+23
-27
lines changed

readme

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Release notes:
66
- supported WebSocket version: 13;
77
- WebSocket extensions not supported;
88
- supports TLS connections (without client auth and tunneling);
9-
- stream-oriented messaging.
9+
- stream-based messaging.
1010

1111
Overview:
1212
Constructors:
@@ -61,8 +61,8 @@ Overview:
6161
// - incoming messages are available until the closing handshake completed.
6262
WsStatus getStatus();
6363
String getSubProtocol(); // returns null or handshaked WebSocket subprotocol
64-
boolean isOpen(); // connection is open
65-
boolean isSecure(); // is TLS connection
64+
boolean isOpen(); // connection is open
65+
boolean isSecure(); // is TLS connection
6666
String getPeerHost(); // returns remote host name
6767
int getPort() // returns bound/connected port
6868
String getPath(); // returns http request path or null

src/org/miktim/websocket/WebSocket.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*
22
* WebSocket. MIT (c) 2020-2021 miktim@mail.ru
33
*
4-
* Prepare ServerSocket/Socket (bind, connect).
5-
* Create and start listener/connection threads.
4+
* Creates ServerSocket/Socket (bind, connect).
5+
* Creates and starts listener/connection threads.
66
*
77
* Release notes:
88
* - Java SE 7+, Android compatible;
99
* - RFC-6455: https://tools.ietf.org/html/rfc6455;
1010
* - WebSocket protocol version: 13;
1111
* - WebSocket extensions not supported;
12-
* - plain socket/TLS connections;
13-
* - stream-oriented messaging.
12+
* - supports plain socket/TLS connections;
13+
* - stream-based messaging.
1414
*
1515
* Created: 2020-06-06
1616
*/
@@ -36,14 +36,9 @@
3636
import javax.net.ssl.SSLServerSocketFactory;
3737
import javax.net.ssl.SSLSocket;
3838
import javax.net.ssl.SSLSocketFactory;
39-
//import javax.net.ssl.SSLSocket;
40-
//import javax.net.ssl.TrustManagerFactory;
4139

4240
public class WebSocket {
4341

44-
// private int handshakeSoTimeout = WsConnection.DEFAULT_HANDSHAKE_SO_TIMEOUT; // open/close handshake
45-
// private int connectionSoTimeout = WsConnection.DEFAULT_CONNECTION_SO_TIMEOUT;
46-
// private boolean pingPong = true;
4742
private WsParameters wsp = new WsParameters();
4843
private InetAddress bindAddress;
4944
private final long wsId = (new Thread()).getId();

src/org/miktim/websocket/WsConnection.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public class WsConnection extends Thread {
4646
public void send(InputStream is) throws IOException {
4747
syncSend(is, false);
4848
}
49-
// send UTF-8 text
5049

50+
// send UTF-8 text
5151
public void send(Reader rd) throws IOException {
5252
syncSend(new ReaderInputStream(rd), true);
5353
}
@@ -189,7 +189,7 @@ private synchronized void syncSend(InputStream is, boolean isText)
189189
sendFrame(op, buf, buf.length);
190190
op = OP_CONTINUATION;
191191
}
192-
// be sure to send the final frame even if eof is detected!
192+
// be sure to send the final frame even if eof is detected (payload length = 0)!
193193
sendFrame(op | OP_FINAL, buf, len >= 0 ? len : 0);
194194
} catch (IOException e) {
195195
closeDueTo(WsStatus.INTERNAL_ERROR, e);
@@ -201,7 +201,8 @@ private synchronized void syncSend(InputStream is, boolean isText)
201201
// - the closing code outside 1000-4999 is replaced by 1005 (NO_STATUS)
202202
// and the reason is ignored;
203203
// - a reason that is longer than 123 bytes is truncated;
204-
// - closing the connection blocks sending messages (send methods throws IOException)
204+
// - closing the connection blocks outgoing messages (send methods throws IOException);
205+
// - incoming messages are available until the closing handshake completed.
205206
public void close() {
206207
close(WsStatus.NO_STATUS, "");
207208
}
@@ -428,7 +429,7 @@ public static String base64Encode(byte[] b) {
428429
static final byte[] PING_PAYLOAD = "PingPong".getBytes();
429430
static final byte[] EMPTY_PAYLOAD = new byte[0];
430431

431-
// variables are filled with waitDataFrame()
432+
// variables are filled with waitDataFrame() function
432433
private int opData; // data frame opcode
433434
private final byte[] payloadMask = new byte[8]; // mask & temp buffer for payloadLength
434435
private long payloadLength = 0;
@@ -449,8 +450,8 @@ private void startMessaging() throws IOException {
449450
handler.onClose(this, getStatus());
450451
}
451452

452-
// Sets WsConnection properties: opData, payloadMask, payloadLegth
453-
// Returns true when a data frame arrives
453+
// Sets WsConnection properties: opData, payloadMask, payloadLegth
454+
// Returns true when a data frame arrives
454455
private boolean waitDataFrame() {
455456
boolean maskedPayload;
456457
while (!status.clean) { // !closing hanshake completed
@@ -516,7 +517,7 @@ private boolean waitDataFrame() {
516517
payloadLength += (payloadMask[i] & 0xFF);
517518
}
518519
}
519-
// get mask
520+
// get payload mask
520521
maskedPayload = (b2 & MASKED_DATA) != 0;
521522
if (maskedPayload) {
522523
toRead = 4;
@@ -718,7 +719,7 @@ public void close() throws IOException {
718719
this.state = IS_CLOSED;
719720
}
720721
}
721-
/* ????
722+
/* ??? can speed up
722723
@Override
723724
public int read(byte[] b, int off, int len) throws IOException {
724725
System.arraycopy
@@ -773,7 +774,7 @@ private void umaskPayload(byte[] mask, byte[] payload, int len) {
773774

774775
private synchronized void sendFrame(int opFrame, byte[] payload, int len)
775776
throws IOException {
776-
if (status.code != 0) { // || !isSocketOpen()) {
777+
if (status.code != 0) { // !isOpen()
777778
// WebSocket closed or close handshake in progress
778779
throw new SocketException("WebSocket closed");
779780
}

src/org/miktim/websocket/WsHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public interface WsHandler {
1717
public void onOpen(WsConnection conn, String subProtocol);
1818

1919
// onMessage:
20-
// - WebSocket message presented by input stream of binary data or UTF-8 characters;
21-
// - exiting the handler closes the stream.
20+
// - the WebSocket message is represented by an input stream of binary data or UTF-8 characters;
21+
// - exiting the handler closes the stream.
2222
public void onMessage(WsConnection conn, InputStream is, boolean isUTF8Text);
2323

2424
// onError:

src/org/miktim/websocket/WsListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* WsListener. WebSocket listener, MIT (c) 2020-2021 miktim@mail.ru
33
*
4-
* Accept sockets, create and start connection threads.
4+
* Accepts sockets, creates and starts connection threads.
55
*
66
* Created: 2020-03-09
77
*/

test/WssClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* WebSocket client test. (c) websocketstest.com
2+
* Secure WebSocket client test. (c) websocketstest.com
33
* Adapted by miktim@mail.ru, march 2021
44
*/
55

@@ -17,7 +17,7 @@
1717
public class WssClientTest {
1818

1919
static final int MAX_MESSAGE_LENGTH = 10000; //
20-
static final int WEBSOCKET_SHUTDOWN_TIMEOUT = 10000; //10sec
20+
static final int WEBSOCKET_SHUTDOWN_TIMEOUT = 15000; //10sec
2121
static final String REMOTE_CONNECTION = "wss://websocketstest.com:443/service";//
2222

2323
static String fragmentTest = randomString(512);

test/WssConnectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.miktim.websocket.WsListener;
1414
import org.miktim.websocket.WsParameters;
1515
import org.miktim.websocket.WsStatus;
16-
16+
1717
public class WssConnectionTest {
1818

1919
static final int MAX_MESSAGE_LENGTH = 1000000; //~1MB

0 commit comments

Comments
 (0)