在 Websocket 服务器端点中获取错误 [英] Getting errors in Websocket server Endpoint
问题描述
这是文件,我在 import javax.websocket
行和 @serverEndpoint("/websocket")
中遇到编译错误.为什么不带注释?
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?
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);
}
}
}
请帮我解决这个错误.我必须在这段代码中实现一些特定的 api 吗?
Please Help me with this error. Are there some specific api which I have to implement in this code?
推荐答案
缺失的类是 java ee 7 api 的一部分.如果您正在使用 maven 构建您的项目,请查看在以下存储库
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
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>
如果你没有使用maven,你可以从上面的页面下载jar.http://repo1.maven.org/maven2/javax/javaee-api/7.0/javaee-api-7.0.jar
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
然后你就有了 API.
Then you have the API.
这篇关于在 Websocket 服务器端点中获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!