当我运行大气样本的大气聊天多房间时,@Message 注释不起作用
Posted
技术标签:
【中文标题】当我运行大气样本的大气聊天多房间时,@Message 注释不起作用【英文标题】:@Message annotation doesn't work when I run atmosphere-chat-multiroom of atmosphere-samples 【发布时间】:2016-04-22 02:52:53 【问题描述】:我已经搜索了很长时间,但没有找到适合我的解决方案。
这是一些代码(我刚刚添加了一些 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
有效。
但我在input
中输入了一个名称,然后单击回车。没啥事儿。
任何人都可以提供帮助,我们将不胜感激。提前致谢。
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\atmosphere\atmosphere-runtime\2.4.3\atmosphere-runtime-2.4.3.jar!\org\atmosphere\config\managed\Invoker.class
【问题讨论】:
【参考方案1】:我遇到过这个问题,可能是encoders
和decoders
引起了这个问题,试试把你的方法参数改成String
。
【讨论】:
以上是关于当我运行大气样本的大气聊天多房间时,@Message 注释不起作用的主要内容,如果未能解决你的问题,请参考以下文章
急救!关于landsat8图像利用envi进行flaash大气校正后的问题