[英]cannot instantiate a WebSocket in my grizzly-project under use of createSocket-Method
I am trying to enable WebSockets in a Grizzly based server and make a server-push but I am getting this error: 我试图在基于Grizzly的服务器中启用WebSockets并进行服务器推送,但出现此错误:
WARNING: GRIZZLY0013: Exception during FilterChain execution
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletResponse
.
.
.
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletResponse
In my Code, I am not using Servlets, So I do not know, why I am getting this error. 在我的代码中,我没有使用Servlet,所以我不知道为什么会收到此错误。 Here is my whole code, which is based on this example https://jaxenter.com/tutorial-pushing-browser-updates-using-websockets-in-glassfish-105970.html
这是我的整个代码,该代码基于此示例https://jaxenter.com/tutorial-pushing-browser-updates-using-websockets-in-glassfish-105970.html
1) The Agent-Class that starts my grizzly-server. 1)启动我的灰熊服务器的代理程序类。 Here I registred my EchoApp without using Servlets.
在这里,我没有使用Servlet就注册了EchoApp。
public class Agent{
public void runServer() throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException,
IOException, InterruptedException {
HttpServer server = HttpServer.createSimpleServer();
final WebSocketAddOn webSocketAddon = new WebSocketAddOn();
for (NetworkListener listener : server.getListeners()) {
listener.setChunkingEnabled(false);
listener.registerAddOn(webSocketAddon);
}
EchoApp echoApplication = new EchoApp();
WebSocketEngine.getEngine().register(echoApplication);
// start the server
server.start();
Thread.currentThread().join();
}
public static void main(String args []){
Agent agent = new Agent();
try {
agent.runServer();
} catch (UnrecoverableKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2) Here is my EchoApplication which extends WebSocketApplication: 2)这是我的EchoApplication,它扩展了WebSocketApplication:
public class EchoApp extends WebSocketApplication{
/** The logger. */
private static final Logger logger = Logger.getLogger(EchoApp.class.getName());
@Override
public boolean isApplicationRequest(HttpRequestPacket request) {
if (request.getRequestURI().toString().endsWith("/echo"))
{
return true;
}
else
{
return false;
}
}
@Override
public WebSocket createSocket(ProtocolHandlerhandler,HttpRequestPacket requestPacket,
WebSocketListener... listeners) {
System.out.println("Websocket will be Created");
return new EchoSocket(handler, requestPacket,listeners);//The exception will be thrown here!!!!!!!!!
}
}
3) Here is my EchoSocket-class: 3)这是我的EchoSocket类:
class EchoSocket extends DefaultWebSocket implements Runnable {
private Thread myThread;
private boolean connected = false;
//handler
EchoSocket (ProtocolHandler handler,HttpRequestPacket requestPacket, WebSocketListener... listeners){
super(handler,requestPacket,listeners);
System.out.println("instantiate");
}
@Override
public void onConnect() {
System.out.println("connected");
myThread = new Thread(this);
connected = true;
myThread.start();
super.onConnect();
}
public void run() {
if(connected) {
System.out.println("connected");
send("push");
}
}
}
Here is the dependencies that I need for building my project: 这是构建项目所需的依赖项:
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-websockets</artifactId>
<version>2.3.22</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<version>2.3.22</version>
</dependency>
<dependency> //I added this dependency when I get the problem but it does not solve it
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet</artifactId>
<version>2.3.22</version>
</dependency>
I never used Servlets and I do not know if I need them in my Code? 我从未使用过Servlet,也不知道我的代码中是否需要它们?
If I need Servlets: Where can I use them in my Code 如果我需要Servlet:在代码中的哪里可以使用它们?
Grizzly has only one module for websocket support, that includes both the client- and the server-side API. Grizzly只有一个用于websocket支持的模块,其中包括客户端API和服务器端API。 The server-side API in its turn has a dependency on servlet-api to be present on the classpath.
而服务器端API则依赖于要在类路径中显示的servlet-api。 So please include this maven dependency:
因此,请包括以下Maven依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.