Skip to content
This repository was archived by the owner on Jan 30, 2019. It is now read-only.

Commit ade2dd8

Browse files
author
Arun Gupta
committed
Adding a simple sample for interface-based client endpoint
Former-commit-id: f22ac02762a97558f5ef8ff30bd02a56b97e686b
1 parent a90597e commit ade2dd8

File tree

5 files changed

+359
-0
lines changed

5 files changed

+359
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<parent>
5+
<groupId>org.sample</groupId>
6+
<artifactId>websocket</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<groupId>org.sample</groupId>
14+
<artifactId>websocket-client-programmatic</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<packaging>war</packaging>
17+
18+
<name>websocket-client-programmatic</name>
19+
20+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.sample.client;
41+
42+
import java.io.IOException;
43+
import java.util.logging.Level;
44+
import java.util.logging.Logger;
45+
import javax.websocket.CloseReason;
46+
import javax.websocket.Endpoint;
47+
import javax.websocket.EndpointConfig;
48+
import javax.websocket.MessageHandler;
49+
import javax.websocket.OnError;
50+
import javax.websocket.OnMessage;
51+
import javax.websocket.Session;
52+
53+
/**
54+
* @author Arun Gupta
55+
*/
56+
public class MyClient extends Endpoint {
57+
@Override
58+
public void onOpen(final Session session, EndpointConfig ec) {
59+
session.addMessageHandler(new MessageHandler.Whole<String>() {
60+
61+
@Override
62+
public void onMessage(String text) {
63+
System.out.println("Received response in client from endpoint: " + text);
64+
}
65+
});
66+
System.out.println("Connected to endpoint: " + session.getBasicRemote());
67+
try {
68+
String name = "Duke";
69+
System.out.println("Sending message from client -> endpoint: " + name);
70+
session.getBasicRemote().sendText(name);
71+
} catch (IOException ex) {
72+
Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
73+
}
74+
}
75+
76+
@Override
77+
public void onClose(Session session, CloseReason closeReason) {
78+
super.onClose(session, closeReason);
79+
}
80+
81+
@Override
82+
public void onError(Session session, Throwable t) {
83+
t.printStackTrace();
84+
}
85+
}
Lines changed: 58 additions & 0 deletions
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.sample.client;
41+
42+
import javax.websocket.OnMessage;
43+
import javax.websocket.server.ServerEndpoint;
44+
45+
46+
/**
47+
* @author Arun Gupta
48+
*/
49+
@ServerEndpoint(value="/websocket")
50+
public class MyEndpoint {
51+
52+
@OnMessage
53+
public String sayHello(String name) {
54+
System.out.println("Received message in endpoint : " + name);
55+
return "Hello " + name;
56+
}
57+
58+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.sample.client;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import java.net.URI;
45+
import java.util.logging.Level;
46+
import java.util.logging.Logger;
47+
import javax.servlet.ServletException;
48+
import javax.servlet.annotation.WebServlet;
49+
import javax.servlet.http.HttpServlet;
50+
import javax.servlet.http.HttpServletRequest;
51+
import javax.servlet.http.HttpServletResponse;
52+
import javax.websocket.ClientEndpointConfig;
53+
import javax.websocket.ContainerProvider;
54+
import javax.websocket.DeploymentException;
55+
import javax.websocket.WebSocketContainer;
56+
57+
/**
58+
* @author Arun Gupta
59+
*/
60+
@WebServlet(urlPatterns = {"/TestClient"})
61+
public class TestClient extends HttpServlet {
62+
63+
/**
64+
* Processes requests for both HTTP
65+
* <code>GET</code> and
66+
* <code>POST</code> methods.
67+
*
68+
* @param request servlet request
69+
* @param response servlet response
70+
* @throws ServletException if a servlet-specific error occurs
71+
* @throws IOException if an I/O error occurs
72+
*/
73+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
74+
throws ServletException, IOException {
75+
response.setContentType("text/html;charset=UTF-8");
76+
try (PrintWriter out = response.getWriter()) {
77+
out.println("<html>");
78+
out.println("<head>");
79+
out.println("<title>Servlet TestServlet</title>");
80+
out.println("</head>");
81+
out.println("<body>");
82+
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
83+
84+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
85+
String uri = "ws://localhost:8080" + request.getContextPath() + "/websocket";
86+
out.println("Connecting to " + uri);
87+
container.connectToServer(MyClient.class,
88+
null,
89+
URI.create(uri));
90+
out.println("<br><br>Look in server.log for message exchange between client/server.");
91+
92+
out.println("</body>");
93+
out.println("</html>");
94+
} catch (DeploymentException ex) {
95+
Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
96+
}
97+
}
98+
99+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
100+
/**
101+
* Handles the HTTP
102+
* <code>GET</code> method.
103+
*
104+
* @param request servlet request
105+
* @param response servlet response
106+
* @throws ServletException if a servlet-specific error occurs
107+
* @throws IOException if an I/O error occurs
108+
*/
109+
@Override
110+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
111+
throws ServletException, IOException {
112+
processRequest(request, response);
113+
}
114+
115+
/**
116+
* Handles the HTTP
117+
* <code>POST</code> method.
118+
*
119+
* @param request servlet request
120+
* @param response servlet response
121+
* @throws ServletException if a servlet-specific error occurs
122+
* @throws IOException if an I/O error occurs
123+
*/
124+
@Override
125+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
126+
throws ServletException, IOException {
127+
processRequest(request, response);
128+
}
129+
130+
/**
131+
* Returns a short description of the servlet.
132+
*
133+
* @return a String containing servlet description
134+
*/
135+
@Override
136+
public String getServletInfo() {
137+
return "Short description";
138+
}// </editor-fold>
139+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
/*
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4+
*
5+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
6+
*
7+
* The contents of this file are subject to the terms of either the GNU
8+
* General Public License Version 2 only ("GPL") or the Common Development
9+
* and Distribution License("CDDL") (collectively, the "License"). You
10+
* may not use this file except in compliance with the License. You can
11+
* obtain a copy of the License at
12+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13+
* or packager/legal/LICENSE.txt. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*
16+
* When distributing the software, include this License Header Notice in each
17+
* file and include the License file at packager/legal/LICENSE.txt.
18+
*
19+
* GPL Classpath Exception:
20+
* Oracle designates this particular file as subject to the "Classpath"
21+
* exception as provided by Oracle in the GPL Version 2 section of the License
22+
* file that accompanied this code.
23+
*
24+
* Modifications:
25+
* If applicable, add the following below the License Header, with the fields
26+
* enclosed by brackets [] replaced by your own identifying information:
27+
* "Portions Copyright [year] [name of copyright owner]"
28+
*
29+
* Contributor(s):
30+
* If you wish your version of this file to be governed by only the CDDL or
31+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
32+
* elects to include this software in this distribution under the [CDDL or GPL
33+
* Version 2] license." If you don't indicate a single choice of license, a
34+
* recipient has the option to distribute your version of this file under
35+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
36+
* its licensees as provided above. However, if you add GPL Version 2 code
37+
* and therefore, elected the GPL Version 2 license, then the option applies
38+
* only if the new code is made subject to such option by the copyright
39+
* holder.
40+
*/
41+
-->
42+
43+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
44+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
45+
"http://www.w3.org/TR/html4/loose.dtd">
46+
47+
<html>
48+
<head>
49+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50+
<title>WebSocket Client - Programmatic</title>
51+
</head>
52+
<body>
53+
<h1>WebSocket Client - Programmatic</h1>
54+
55+
<a href="${pageContext.request.contextPath}/TestClient">Call endpoint</a><br/>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)