Java 在 Websocket 服务器端点中获取错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21038100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting errors in Websocket server Endpoint
提问by Sandeep
This is file and I am getting compilation error in import javax.websocket
lines and in @serverEndpoint("/websocket")
. Why it is not taking the annotation?
这是文件,我在行import javax.websocket
和@serverEndpoint("/websocket")
. 为什么它不带注释?
package pack.exp;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class Hello
{
{
private static Set<Session> clients =
Collections.synchronizedSet(new HashSet<Session>());
@OnMessage
public void onMessage(String message, Session session)
throws IOException {
synchronized(clients){
// Iterate over the connected sessions
// and broadcast the received message
for(Session client : clients){
if (!client.equals(session)){
client.getBasicRemote().sendText(message);
}
}
}
}
@OnOpen
public void onOpen (Session session) {
// Add session to the connected sessions set
clients.add(session);
}
@OnClose
public void onClose (Session session) {
// Remove session from the connected sessions set
clients.remove(session);
}
}
}
Please Help me with this error. Are there some specific api which I have to implement in this code?
请帮我解决这个错误。我必须在这段代码中实现一些特定的 api 吗?
采纳答案by Wintermute
The missing classes are part of the java ee 7 api. If you are building your project with maven, take a look at the following repository
缺少的类是 java ee 7 api 的一部分。如果您使用 maven 构建项目,请查看以下存储库
http://mvnrepository.com/artifact/javax/javaee-api/7.0
http://mvnrepository.com/artifact/javax/javaee-api/7.0
and add this dependency to your project:
并将此依赖项添加到您的项目中:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
If you are not using maven, you can download the jar from the page above. http://repo1.maven.org/maven2/javax/javaee-api/7.0/javaee-api-7.0.jar
如果你不使用 maven,你可以从上面的页面下载 jar。 http://repo1.maven.org/maven2/javax/javaee-api/7.0/javaee-api-7.0.jar
Then you have the API.
然后你就有了 API。
回答by Joakim Erdfelt
The classes under the javax.websocket
are defined by the JSR-356 to be standalone. They can run on a completely standalone server or from within a Java EE 7 container.
javax.websocket
JSR-356 将下的类定义为独立的。它们可以在完全独立的服务器上运行,也可以在 Java EE 7 容器中运行。
If you are using just javax.websocket
and don't care about the rest of Java EE 7, then just use the official javax.websocket
artifacts in a provided scope.
如果您只使用javax.websocket
Java EE 7 的其余部分而不关心其余部分,那么只需javax.websocket
在提供的范围内使用官方工件。
Here's the search lookup directly to the artifact.
这是直接到工件的搜索查找。
https://search.maven.org/artifact/javax.websocket/javax.websocket-api/1.0/bundle
https://search.maven.org/artifact/javax.websocket/javax.websocket-api/1.0/bundle
That is the official search page for the maven central repository system, it also includes build system references to that artifact for maven, grails, ivy, buildr, grape, and sbt.
这是 maven 中央存储库系统的官方搜索页面,它还包括对 maven、grails、ivy、buildr、grape 和 sbt 的该工件的构建系统引用。
The maven pom reference would be:
Maven pom 参考将是:
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
Make sure you do notinclude this artifact in your *.war
file, as this functionality will be provided by whatever container you decide to deploy into. (Such as Eclipse Jetty 9.1+ or Apache Tomcat 8.0+)
确保您的文件中不包含此工件*.war
,因为此功能将由您决定部署到的任何容器提供。(如Eclipse Jetty 9.1+或Apache Tomcat 8.0+)
回答by Uday Singh
You can freshly add the server say tomcat 7.0, automatically this error will be resolved. As the supporting jars are available in tomcat lib.
你可以新添加服务器说tomcat 7.0,这个错误会自动解决。由于支持 jars 在 tomcat lib 中可用。