private Chat getChat() { if (chat == null) { chat = ChatManager.getInstanceFor(con).createChat(getUserId(), new MessageListener() { @Override public void processMessage(Chat c, Message m) { if (chat != null && listener != null) { listener.onMessage(instance, m.getBody()); } } }); } return chat; }
private void sendMessage(String to,String message){ /* * this sends a message to someone * @param to the xmmp-account who receives the message, the destination * @param message: yeah, the text I'm sending... */ ChatManager chatmanager = conn.getChatManager(); Chat newChat = chatmanager.createChat(to, new MessageListener() { public void processMessage(Chat chat, Message m) { } }); try { newChat.sendMessage(message); } catch (XMPPException e) { System.out.println("Error Delivering block" +e); } }
/** * 创建聊天 * @param xmppConnection * @param toUser * @param messageListener * @return */ public static Chat createChat(XMPPConnection xmppConnection,String toUser,MessageListener messageListener){ // 获取当前登陆用户的聊天管理器 ChatManager chatManager = xmppConnection.getChatManager(); // 为指定用户创建一个chat,MyMessageListeners用于监听对方发过来的消息 */ Chat chat = chatManager.createChat(toUser+"@" + SERVERNAME, messageListener); return chat; }
void sendMessage(Chat chat, Message message) throws NotConnectedException { for(Map.Entry<MessageListener, StanzaFilter> interceptor : interceptors.entrySet()) { StanzaFilter filter = interceptor.getValue(); if(filter != null && filter.accept(message)) { interceptor.getKey().processMessage(message); } } // Ensure that messages being sent have a proper FROM value if (message.getFrom() == null) { message.setFrom(connection().getUser()); } connection().sendStanza(message); }
private void fireNewChatState(Chat chat, ChatState state) { for (MessageListener listener : chat.getListeners()) { if (listener instanceof ChatStateListener) { ((ChatStateListener) listener).stateChanged(chat, state); } } }
public void sendMessage(XMPPMessage xmppMessage) throws XMPPException { ChatManager chatmanager = connection.getChatManager(); Chat newChat = chatmanager.createChat(xmppMessage.getTo(), new MessageListener() { @Override public void processMessage(Chat chat, Message message) { // No need to implement } }); LOG.debug("Sending message '" + xmppMessage.getMessage() + "' to " + newChat.getParticipant()); newChat.sendMessage(xmppMessage.getMessage()); }
@Test public void shouldSendMessage() throws XMPPException { adapter.connect(); XMPPMessage xmppMessage = new XMPPMessage(); ChatManager chatmanager = mock(ChatManager.class); Chat chat = mock(Chat.class); when(connection.getChatManager()).thenReturn(chatmanager); when(chatmanager.createChat(eq(xmppMessage.getTo()), any(MessageListener.class))).thenReturn(chat); adapter.sendMessage(xmppMessage); verify(chat).sendMessage(xmppMessage.getMessage()); }
@Override public Chat createChat(String toUser, MessageListener messageListener) { return XMPPUtil.createChat(xmppConnection, toUser, messageListener); }
public void addOutgoingMessageInterceptor(MessageListener messageInterceptor, StanzaFilter filter) { if (messageInterceptor == null) { return; } interceptors.put(messageInterceptor, filter); }
protected MessageListener getMessageListener() { return messageListener; }
/** * 创建聊天 * @param toUser * @param messageListener * @return */ Chat createChat(String toUser, MessageListener messageListener);
/** * Adds an interceptor which intercepts any messages sent through chats. * * @param messageInterceptor the interceptor. */ public void addOutgoingMessageInterceptor(MessageListener messageInterceptor) { addOutgoingMessageInterceptor(messageInterceptor, null); }
/** * Adds a stanza(/packet) listener that will be notified of any new messages in the * group chat. Only "group chat" messages addressed to this group chat will * be delivered to the listener. If you wish to listen for other packets * that may be associated with this group chat, you should register a * PacketListener directly with the XMPPConnection with the appropriate * PacketListener. * * @param listener a stanza(/packet) listener. * @return true if the listener was not already added. */ public boolean addMessageListener(MessageListener listener) { return messageListeners.add(listener); }
/** * Removes a stanza(/packet) listener that was being notified of any new messages in the * multi user chat. Only "group chat" messages addressed to this multi user chat were * being delivered to the listener. * * @param listener a stanza(/packet) listener. * @return true if the listener was removed, otherwise the listener was not added previously. */ public boolean removeMessageListener(MessageListener listener) { return messageListeners.remove(listener); }