Skip to content

Commit 611075c

Browse files
committed
2.2.3
1 parent 4c0a411 commit 611075c

4 files changed

Lines changed: 24 additions & 158 deletions

File tree

src/org/miktim/websocket/WebSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public WsConnection connect(String uri, WsHandler handler) throws Exception {
121121
connection.setConnectionSoTimeout(connectionSoTimeout, pingPong);
122122
connection.setMaxMessageLength(maxMessageLength, streamingEnabled);
123123
connection.setSubprotocol(subprotocols);
124-
connection.connect();
124+
connection.connectSocket();
125125
connection.start();
126126
return connection;
127127
}

src/org/miktim/websocket/WsConnection.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
import java.security.InvalidParameterException;
3131
import java.security.MessageDigest;
3232
import java.security.NoSuchAlgorithmException;
33+
import java.util.ArrayDeque;
3334
import java.util.Arrays;
3435
import java.util.Random;
3536
//import java.security.SecureRandom;
3637
import javax.net.ssl.SSLSocket;
3738
import javax.net.ssl.SSLSocketFactory;
38-
//import java.util.Base64; // java 1.8+
39+
//import java.util.Base64; // java 8+
3940

4041
public class WsConnection extends Thread {
4142

@@ -206,7 +207,7 @@ public String getPath() {
206207
}
207208
return null;
208209
}
209-
210+
210211
public String getQuery() {
211212
if (requestURI != null) {
212213
return this.requestURI.getQuery();
@@ -385,11 +386,11 @@ void open() throws IOException {
385386
if (!isClientSide || this.socket.isConnected()) {
386387
throw new SocketException();
387388
}
388-
connect();
389+
connectSocket();
389390
this.start();
390391
}
391392
*/
392-
void connect() throws IOException {
393+
void connectSocket() throws IOException {
393394
try {
394395
int port = requestURI.getPort();
395396
if (port < 0) {
@@ -417,8 +418,8 @@ private void handshakeServer() throws IOException, URISyntaxException {
417418
host = (new URI(host)).toASCIIString();
418419
String requestLine = "GET " + path + " HTTP/1.1";
419420
Headers headers = new Headers();
420-
headers.add("Host", host);
421-
// headers.add("Origin", requestURI.getScheme() + "://" + host);
421+
headers.add("Host", host);
422+
headers.add("Origin", requestURI.getScheme() + "://" + host);
422423
headers.add("Upgrade", "websocket");
423424
headers.add("Connection", "Upgrade,keep-alive");
424425
headers.add("Sec-WebSocket-Key", key);
@@ -522,7 +523,7 @@ public void run() {
522523
this.listenInputStream();
523524
} catch (Exception e) {
524525
this.handler.onError(this, e);
525-
e.printStackTrace();
526+
// e.printStackTrace();
526527
this.closeSocket();
527528
}
528529
}
@@ -549,6 +550,8 @@ void listenInputStream() throws IOException {
549550
int opData = 0;
550551
byte[] message = EMPTY_PAYLOAD;
551552
long messageLen = 0;
553+
554+
ArrayDeque<byte[]> payloadQueue = new ArrayDeque<byte[]>();
552555

553556
byte[] service = new byte[8]; //buffer for frame header elements
554557
boolean done = false;
@@ -658,12 +661,22 @@ void listenInputStream() throws IOException {
658661
switch (b1) {
659662
case OP_CONTINUATION:
660663
if (this.closeCode == 0) {
661-
message = combine(message, framePayload);
664+
payloadQueue.addLast(framePayload);
665+
// message = combine(message, framePayload);
662666
}
663667
break;
664668
case OP_FINAL: {
665669
if (this.closeCode == 0) {
666-
message = combine(message, framePayload);
670+
payloadQueue.addLast(framePayload);
671+
message = new byte[(int) messageLen];
672+
framePayloadLen = 0;
673+
while (!payloadQueue.isEmpty()) {
674+
framePayload = payloadQueue.pollFirst();
675+
System.arraycopy(
676+
framePayload, 0, message, (int) framePayloadLen, framePayload.length);
677+
framePayloadLen += framePayload.length;
678+
}
679+
// message = combine(message, framePayload);
667680
if (opData == OP_BINARY) {
668681
handler.onMessage(this, message);
669682
} else {
@@ -674,6 +687,7 @@ void listenInputStream() throws IOException {
674687
opData = 0;
675688
message = EMPTY_PAYLOAD;
676689
messageLen = 0;
690+
payloadQueue.clear();
677691
break;
678692
}
679693
case OP_PONG: {

src/org/miktim/websocket/WsParameters.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

src/org/miktim/websocket/WsStatus.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)