java - @Message annotation doesn't work when I run atmosphere-chat-multiroom of atmosphere-samples -
i've searched issue long while no solution comes me.
here code (i've added system.out.println
phrases).
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>(); (broadcaster broadcaster : broadcasters) { if (!("/*".equals(broadcaster.getid()))) { // if no room 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 connection"); } } @message(decoders = {userdecoder.class}) public void onprivatemessage(usermessage user) { system.out.println("client " + user.getuser() + " send message."); string useruuid = users.get(user.getuser()); if (useruuid != null) { // retrieve original atmosphereresource atmosphereresource r = resourcefactory.find(useruuid); if (r != null) { chatprotocol m = new chatprotocol(user.getuser(), " sent 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 message 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 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())); } }
when run sample in intellij idea, websocket can establish. , of functions works fine except @message
annotation, can choose chatroom means @ready
annotation works cause console log prints below.
client 6c981440-0ae6-43ed-be40-95dfad23d586 connected.
when close browser, console log is
client 6c981440-0ae6-43ed-be40-95dfad23d586 closed connection
which means @disconnect
works.
but type name in input
, click enter. nothing happened.
anyone can appreciated. in advance.
ps: run environment jdk1.7, tomcat7 , atmosphere-runtime version 2.4.3. i've debug yesterday, after typed name, illegalargumentexception
catched in code below.
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; }
which code in org\atmosphere\atmosphere-runtime\2.4.3\atmosphere-runtime-2.4.3.jar!\org\atmosphere\config\managed\invoker.class
i've met issue, , maybe encoders
, decoders
caused problem, try change method parameter string
.
Comments
Post a Comment