Skip to content

Commit 0e29e1b

Browse files
committed
Refactoring the sample such that byte[], ByteBuffer, and InputStream are tested
1 parent fb56b3d commit 0e29e1b

File tree

5 files changed

+213
-61
lines changed

5 files changed

+213
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.websocket.binary;
41+
42+
import java.io.IOException;
43+
import java.nio.ByteBuffer;
44+
import javax.websocket.OnMessage;
45+
import javax.websocket.Session;
46+
import javax.websocket.server.ServerEndpoint;
47+
48+
/**
49+
* @author Arun Gupta
50+
*/
51+
@ServerEndpoint("/bytearray")
52+
public class MyEndpointByteArray {
53+
@OnMessage
54+
public void echoBinary(byte[] data, Session session) throws IOException {
55+
System.out.println("echoBinary (byte[]): " + data);
56+
session.getBasicRemote().sendBinary(ByteBuffer.wrap(data));
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.websocket.binary;
41+
42+
import java.io.IOException;
43+
import java.nio.ByteBuffer;
44+
import javax.websocket.OnMessage;
45+
import javax.websocket.Session;
46+
import javax.websocket.server.ServerEndpoint;
47+
48+
/**
49+
* @author Arun Gupta
50+
*/
51+
@ServerEndpoint("/bytebuffer")
52+
public class MyEndpointByteBuffer {
53+
@OnMessage
54+
public void echoBinary(ByteBuffer data, Session session) throws IOException {
55+
System.out.println("echoBinary (ByteBuffer): " + data);
56+
session.getBasicRemote().sendBinary(data);
57+
}
58+
}

websocket/binary/src/main/java/org/javaee7/websocket/binary/MyEndpoint.java renamed to websocket/binary/src/main/java/org/javaee7/websocket/binary/MyEndpointInputStream.java

+10-39
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,24 @@
4040
package org.javaee7.websocket.binary;
4141

4242
import java.io.IOException;
43+
import java.io.InputStream;
4344
import java.nio.ByteBuffer;
44-
import java.util.logging.Level;
45-
import java.util.logging.Logger;
4645
import javax.websocket.OnMessage;
4746
import javax.websocket.Session;
4847
import javax.websocket.server.ServerEndpoint;
4948

5049
/**
5150
* @author Arun Gupta
5251
*/
53-
@ServerEndpoint("/websocket")
54-
public class MyEndpoint {
52+
@ServerEndpoint("/inputstream")
53+
public class MyEndpointInputStream {
54+
5555
@OnMessage
56-
public void echoBinary(ByteBuffer data, Session session) {
57-
System.out.println("echoBinary: " + data);
58-
for (byte b : data.array()) {
59-
System.out.print(b);
60-
}
61-
try {
62-
session.getBasicRemote().sendBinary(data);
63-
} catch (IOException ex) {
64-
Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
65-
}
56+
public void echoStream(InputStream stream, Session session) throws IOException {
57+
System.out.println("echoStream: " + stream);
58+
byte[] b = new byte[8];
59+
int n = stream.read(b);
60+
System.out.println("read " + n + " bytes");
61+
session.getBasicRemote().sendBinary(ByteBuffer.wrap(b));
6662
}
67-
68-
// @WebSocketMessage
69-
// public void echoBinary(byte[] data, Session session) throws IOException {
70-
// System.out.println("echoBinary: " + data);
71-
// StringBuilder builder = new StringBuilder();
72-
// for (byte b : data) {
73-
// builder.append(b);
74-
// }
75-
// System.out.println(builder);
76-
// session.getRemote().sendBytes(ByteBuffer.wrap(data));
77-
// }
78-
79-
// @WebSocketMessage
80-
// public void echoStream(InputStream stream, Session session) {
81-
// System.out.println("echoStream: " + stream);
82-
// byte[] b = new byte[20];
83-
// try {
84-
// int n = stream.read(b);
85-
// System.out.println("read " + n + " bytes");
86-
// System.out.println(new String(b));
87-
// session.getRemote().sendString(new String(b));
88-
// } catch (IOException ex) {
89-
// Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
90-
// }
91-
// }
9263
}

websocket/binary/src/main/webapp/index.jsp

+16-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,22 @@
5151
<body>
5252
<h1>WebSocket : Binary Messages</h1>
5353

54-
<div style="text-align: center;">
55-
<form action="">
56-
<h2>Data is sent/received using ArrayBuffer</h2>
57-
<input onclick="echoBinary()" value="Echo" type="button">
58-
<input id="myField" value="12345678" type="text"><br>
59-
</form>
60-
</div>
54+
<form action="">
55+
<h2>Data is sent/received using byte[]</h2>
56+
<input onclick="echoBinaryByteArray()" value="Echo" type="button">
57+
<input id="myField" value="12345678" type="text"><br>
58+
</form>
59+
<form action="">
60+
<h2>Data is sent/received using ByteBuffer</h2>
61+
<input onclick="echoBinaryByteBuffer()" value="Echo" type="button">
62+
<input id="myField" value="12345678" type="text"><br>
63+
</form>
64+
<form action="">
65+
<h2>Data is sent/received using InputStream</h2>
66+
<input onclick="echoBinaryInputStream()" value="Echo" type="button">
67+
<input id="myField" value="12345678" type="text"><br>
68+
</form>
69+
6170
<div id="output"></div>
6271
<script language="javascript" type="text/javascript" src="websocket.js">
6372
</script>

websocket/binary/src/main/webapp/websocket.js

+71-15
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,89 @@
3838
* holder.
3939
*/
4040

41-
var wsUri = "ws://" + document.location.host + document.location.pathname + "websocket";
42-
console.log("Connecting to " + wsUri);
43-
var websocket = new WebSocket(wsUri);
44-
websocket.binaryType = "arraybuffer";
45-
websocket.onopen = function(evt) { onOpen(evt) };
46-
websocket.onmessage = function(evt) { onMessage(evt) };
47-
websocket.onerror = function(evt) { onError(evt) };
41+
var wsByteArrayUri = "ws://" + document.location.host + document.location.pathname + "bytearray";
42+
var wsByteBufferUri = "ws://" + document.location.host + document.location.pathname + "bytebuffer";
43+
var wsInputStreamUri = "ws://" + document.location.host + document.location.pathname + "inputstream";
44+
45+
console.log("Connecting to " + wsByteArrayUri);
46+
47+
var websocketByteArray = new WebSocket(wsByteArrayUri);
48+
var websocketByteBuffer = new WebSocket(wsByteBufferUri);
49+
var websocketInputStream = new WebSocket(wsInputStreamUri);
50+
51+
websocketByteArray.binaryType = "arraybuffer";
52+
websocketByteBuffer.binaryType = "arraybuffer";
53+
websocketInputStream.binaryType = "arraybuffer";
54+
55+
websocketByteArray.onopen = function(evt) { onOpenByteArray(evt); };
56+
websocketByteArray.onmessage = function(evt) { onMessageByteArray(evt); };
57+
websocketByteArray.onerror = function(evt) { onError(evt); };
58+
59+
websocketByteBuffer.onopen = function(evt) { onOpenByteBuffer(evt); };
60+
websocketByteBuffer.onmessage = function(evt) { onMessageByteBuffer(evt); };
61+
websocketByteBuffer.onerror = function(evt) { onError(evt); };
62+
63+
websocketInputStream.onopen = function(evt) { onOpenInputStream(evt); };
64+
websocketInputStream.onmessage = function(evt) { onMessageInputStream(evt); };
65+
websocketInputStream.onerror = function(evt) { onError(evt); };
4866

4967
var output = document.getElementById("output");
5068

51-
function echoBinary() {
69+
function onOpenByteArray() {
70+
console.log("onOpen (byte])");
71+
writeToScreen("CONNECTED (byte[])");
72+
}
73+
74+
function onOpenByteBuffer() {
75+
console.log("onOpen (ByteBuffer)");
76+
writeToScreen("CONNECTED (ByteBuffer)");
77+
}
78+
79+
function onOpenInputStream() {
80+
console.log("onOpen (InputStream)");
81+
writeToScreen("CONNECTED (InputStream)");
82+
}
83+
84+
function echoBinaryByteArray() {
85+
var buffer = new ArrayBuffer(myField.value.length);
86+
var bytes = new Uint8Array(buffer);
87+
for (var i=0; i<bytes.length; i++) {
88+
bytes[i] = i;
89+
}
90+
websocketByteArray.send(buffer);
91+
writeToScreen("SENT (byte[]): " + buffer.byteLength + " bytes");
92+
}
93+
94+
function echoBinaryByteBuffer() {
95+
var buffer = new ArrayBuffer(myField.value.length);
96+
var bytes = new Uint8Array(buffer);
97+
for (var i=0; i<bytes.length; i++) {
98+
bytes[i] = i;
99+
}
100+
websocketByteBuffer.send(buffer);
101+
writeToScreen("SENT (ByteBuffer): " + buffer.byteLength + " bytes");
102+
}
103+
104+
function echoBinaryInputStream() {
52105
var buffer = new ArrayBuffer(myField.value.length);
53106
var bytes = new Uint8Array(buffer);
54107
for (var i=0; i<bytes.length; i++) {
55108
bytes[i] = i;
56109
}
57-
websocket.send(buffer);
58-
writeToScreen("SENT (binary): " + buffer.byteLength + " bytes");
110+
websocketInputStream.send(buffer);
111+
writeToScreen("SENT (InputStream): " + buffer.byteLength + " bytes");
112+
}
113+
114+
function onMessageByteArray(evt) {
115+
writeToScreen("RECEIVED (byte[]): " + evt.data);
59116
}
60117

61-
function onOpen() {
62-
console.log("onOpen");
63-
writeToScreen("CONNECTED");
118+
function onMessageByteBuffer(evt) {
119+
writeToScreen("RECEIVED (ByteBuffer): " + evt.data);
64120
}
65121

66-
function onMessage(evt) {
67-
writeToScreen("RECEIVED (binary): " + evt.data);
122+
function onMessageInputStream(evt) {
123+
writeToScreen("RECEIVED (InputStream): " + evt.data);
68124
}
69125

70126
function onError(evt) {

0 commit comments

Comments
 (0)