Skip to content

Commit 4e96208

Browse files
author
Arun Gupta
committed
Added support for different primitive types
1 parent 1c011ae commit 4e96208

File tree

6 files changed

+227
-45
lines changed

6 files changed

+227
-45
lines changed

websocket/endpoint-javatypes/src/main/java/org/javaee7/websocket/endpoint/javatypes/MyEndpoint.java

+3-34
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
*/
4040
package org.javaee7.websocket.endpoint.javatypes;
4141

42-
import java.io.IOException;
43-
import java.io.Reader;
44-
import java.nio.CharBuffer;
45-
import java.util.logging.Level;
46-
import java.util.logging.Logger;
4742
import javax.websocket.OnMessage;
4843
import javax.websocket.server.ServerEndpoint;
4944

@@ -53,35 +48,9 @@
5348
@ServerEndpoint("/websocket")
5449
public class MyEndpoint {
5550

56-
// @WebSocketMessage
57-
// public String echoText(String text) {
58-
// System.out.println("echoText");
59-
// return text;
60-
// }
61-
//
62-
// @WebSocketMessage
63-
// public int echoInt(int i) {
64-
// System.out.println("echoInt");
65-
// return i;
66-
// }
67-
//
68-
// @WebSocketMessage
69-
// public float echoFloat(Float f) {
70-
// System.out.println("echoFloat");
71-
// return f;
72-
// }
73-
7451
@OnMessage
75-
public String echoReader(Reader reader) {
76-
System.out.println("echoReader");
77-
CharBuffer buffer = CharBuffer.allocate(20);
78-
try {
79-
reader.read(buffer);
80-
} catch (IOException ex) {
81-
Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
82-
return null;
83-
}
84-
85-
return new String(buffer.array());
52+
public String echoText(String text) {
53+
System.out.println("echoText");
54+
return text;
8655
}
8756
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.endpoint.javatypes;
41+
42+
import javax.websocket.OnMessage;
43+
import javax.websocket.server.ServerEndpoint;
44+
45+
/**
46+
* @author Arun Gupta
47+
*/
48+
@ServerEndpoint("/websocket-float")
49+
public class MyEndpointFloat {
50+
51+
@OnMessage
52+
public float echoFloat(Float f) {
53+
System.out.println("echoFloat");
54+
return f;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.endpoint.javatypes;
41+
42+
import javax.websocket.OnMessage;
43+
import javax.websocket.server.ServerEndpoint;
44+
45+
/**
46+
* @author Arun Gupta
47+
*/
48+
@ServerEndpoint("/websocket-int")
49+
public class MyEndpointInt {
50+
51+
@OnMessage
52+
public int echoInt(int i) {
53+
System.out.println("echoInt");
54+
return i;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.endpoint.javatypes;
41+
42+
import java.io.IOException;
43+
import java.io.Reader;
44+
import java.nio.CharBuffer;
45+
import java.util.logging.Level;
46+
import java.util.logging.Logger;
47+
import javax.websocket.OnMessage;
48+
import javax.websocket.server.ServerEndpoint;
49+
50+
/**
51+
* @author Arun Gupta
52+
*/
53+
@ServerEndpoint("/websocket-reader")
54+
public class MyEndpointReader {
55+
56+
@OnMessage
57+
public String echoReader(Reader reader) {
58+
System.out.println("echoReader");
59+
CharBuffer buffer = CharBuffer.allocate(20);
60+
try {
61+
reader.read(buffer);
62+
} catch (IOException ex) {
63+
Logger.getLogger(MyEndpointReader.class.getName()).log(Level.SEVERE, null, ex);
64+
return null;
65+
}
66+
67+
return new String(buffer.array());
68+
}
69+
}

websocket/endpoint-javatypes/src/main/webapp/index.jsp

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@
4545
<html>
4646
<head>
4747
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
48-
<title>Getting Started with JSR 356 - Java Types</title>
48+
<title>WebSocket : Java Types</title>
4949

5050
</head>
5151
<body>
52-
<h1>Getting Started with JSR 356 - Java Types</h1>
52+
<h1>WebSocket : Java Types</h1>
5353

5454
<div style="text-align: center;">
5555
<form action="">
56-
<h2>Text Data</h2>
57-
<input onclick="echoText();" value="Echo" type="button">
56+
<input onclick="echoText();" value="Echo Text" type="button">
57+
<input onclick="echoInt();" value="Echo Int" type="button">
58+
<input onclick="echoFloat();" value="Echo Float" type="button">
59+
<input onclick="echoReader();" value="Echo Reader" type="button">
5860
<input id="myField" value="WebSocket" type="text"><br>
5961
</form>
6062
</div>

websocket/endpoint-javatypes/src/main/webapp/websocket.js

+37-7
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,48 @@
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.onopen = function(evt) { onOpen(evt) };
45-
websocket.onmessage = function(evt) { onMessage(evt) };
46-
websocket.onerror = function(evt) { onError(evt) };
41+
var wsUri = "ws://" + document.location.host + document.location.pathname;
42+
var websocket = new WebSocket(wsUri + "websocket");
43+
var websocket_int = new WebSocket(wsUri + "websocket-int");
44+
var websocket_float = new WebSocket(wsUri + "websocket-float");
45+
var websocket_reader = new WebSocket(wsUri + "websocket-reader");
46+
47+
websocket.onopen = function(evt) { onOpen(evt); };
48+
websocket.onmessage = function(evt) { onMessage(evt); };
49+
websocket.onerror = function(evt) { onError(evt); };
50+
51+
websocket_int.onopen = function(evt) { onOpen(evt); };
52+
websocket_int.onmessage = function(evt) { onMessage(evt); };
53+
websocket_int.onerror = function(evt) { onError(evt); };
54+
55+
websocket_float.onopen = function(evt) { onOpen(evt); };
56+
websocket_float.onmessage = function(evt) { onMessage(evt); };
57+
websocket_float.onerror = function(evt) { onError(evt); };
58+
59+
websocket_reader.onopen = function(evt) { onOpen(evt); };
60+
websocket_reader.onmessage = function(evt) { onMessage(evt); };
61+
websocket_reader.onerror = function(evt) { onError(evt); };
4762

4863
var output = document.getElementById("output");
4964

5065
function echoText() {
5166
websocket.send(myField.value);
52-
writeToScreen("SENT: " + myField.value);
67+
writeToScreen("SENT (echoText): " + myField.value);
68+
}
69+
70+
function echoInt() {
71+
websocket_int.send(myField.value);
72+
writeToScreen("SENT (echoInt): " + myField.value);
73+
}
74+
75+
function echoFloat() {
76+
websocket_float.send(myField.value);
77+
writeToScreen("SENT (echoFloat): " + myField.value);
78+
}
79+
80+
function echoReader() {
81+
websocket_reader.send(myField.value);
82+
writeToScreen("SENT (echoReader): " + myField.value);
5383
}
5484

5585
function onOpen() {

0 commit comments

Comments
 (0)