我已经搜索了这个问题很长一段时间了,但没有解决方案。
以下是一些代码(我刚添加了一些System.out.println
个短语)。
的web.xml
<display-name>Atmosphere Chat</display-name>
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>
ChatRoom.java
@ManagedService(path = "/chat/{room}")
public class ChatRoom {
private final ConcurrentHashMap<String, String> users = new ConcurrentHashMap<String, String>();
private final static String CHAT = "/chat/";
@PathParam("room")
private String room;
@Inject
private BroadcasterFactory factory;
@Inject
private AtmosphereResourceFactory resourceFactory;
@Inject
private MetaBroadcaster metaBroadcaster;
private static Collection<String> getRooms(Collection<Broadcaster> broadcasters) {
Collection<String> result = new ArrayList<String>();
for (Broadcaster broadcaster : broadcasters) {
if (!("/*".equals(broadcaster.getID()))) {
// if no room is specified, use ''
String[] p = broadcaster.getID().split("/");
result.add(p.length > 2 ? p[2] : "");
}
};
return result;
}
@Ready(encoders = {JacksonEncoder.class})
@DeliverTo(DeliverTo.DELIVER_TO.ALL)
public ChatProtocol onReady(final AtmosphereResource resource) {
System.out.println("Client " + resource.uuid() + " connected.");
return new ChatProtocol(users.keySet(), getRooms(factory.lookupAll()));
}
@Disconnect
public void onDisconnect(AtmosphereResourceEvent event) {
if (event.isCancelled()) {
System.out.println("Client " + event.getResource().uuid() + " unexpectedly disconnected.");
} else if (event.isClosedByClient()) {
System.out.println("Client " + event.getResource().uuid() + " closed the connection");
}
}
@Message(decoders = {UserDecoder.class})
public void onPrivateMessage(UserMessage user) {
System.out.println("Client " + user.getUser() + " send a message.");
String userUUID = users.get(user.getUser());
if (userUUID != null) {
// Retrieve the original AtmosphereResource
AtmosphereResource r = resourceFactory.find(userUUID);
if (r != null) {
ChatProtocol m = new ChatProtocol(user.getUser(), " sent you a private message: " + user.getMessage().split(":")[1], users.keySet(), getRooms(factory.lookupAll()));
if (!user.getUser().equalsIgnoreCase("all")) {
factory.lookup(CHAT + room).broadcast(m, r);
}
}
} else {
ChatProtocol m = new ChatProtocol(user.getUser(), " sent a message to all chatroom: " + user.getMessage().split(":")[1], users.keySet(), getRooms(factory.lookupAll()));
metaBroadcaster.broadcastTo("/*", m);
}
}
@Message(encoders = {JacksonEncoder.class}, decoders = {ProtocolDecoder.class})
public ChatProtocol onMessage(ChatProtocol message) throws IOException {
if (message.getMessage().contains("disconnecting")) {
users.remove(message.getAuthor());
return new ChatProtocol(message.getAuthor(), " disconnected from room " + room, users.keySet(), getRooms(factory.lookupAll()));
}
if (!users.containsKey(message.getAuthor())) {
users.put(message.getAuthor(), message.getUuid());
return new ChatProtocol(message.getAuthor(), " entered room " + room, users.keySet(), getRooms(factory.lookupAll()));
}
message.setUsers(users.keySet());
return new ChatProtocol(message.getAuthor(), message.getMessage(), users.keySet(), getRooms(factory.lookupAll()));
}
}
当我在IntelliJ IDEA中运行此示例时,websocket可以建立。并且除了@Message
注释之外,所有函数都可以正常工作,我可以选择一个聊天室意味着@Ready
注释工作会导致控制台日志打印如下所示。
客户端6c981440-0ae6-43ed-be40-95dfad23d586已连接。
当我关闭浏览器时,控制台日志是
客户端6c981440-0ae6-43ed-be40-95dfad23d586已关闭连接
表示@Disconnect
有效。
任何人都可以提供帮助。提前谢谢。
PS:运行环境是JDK1.7,tomcat7和大气运行时版本是2.4.3。我昨天调试了这个,在输入名称后,IllegalArgumentException
被捕获在下面的代码中。
public static Object invokeMethod(Method method, Object objectToInvoke, Object... parameters) {
Object objectToEncode = null;
boolean hasMatch = false;
try {
objectToEncode = method.invoke(objectToInvoke, method.getParameterTypes().length == 0?new Object[0]:parameters);
hasMatch = true;
} catch (IllegalAccessException var6) {
logger.trace("", var6);
} catch (InvocationTargetException var7) {
logger.error("", var7);
} catch (IllegalArgumentException var8) {
logger.trace("", var8);
} catch (Throwable var9) {
logger.error("", var9);
}
if(!hasMatch) {
logger.trace("No Method\'s Arguments {} matching {}", method.getName(), objectToInvoke);
}
return objectToEncode;
}
哪个代码位于 org \ _conmospicle/conmospour- runtime \ 2.4.3 \ atmosphere-runtime-2.4.3.jar!\ org \ atmosphere \ config \ managed \ Invoker.class
答案 0 :(得分:1)
我遇到了这个问题,可能encoders
和decoders
导致了这个问题,请尝试将您的方法参数更改为String
。