org.jivesoftware.smack.MessageListener Java Examples

The following examples show how to use org.jivesoftware.smack.MessageListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmppManager.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 获取或创建聊天窗口
 * 
 * @param friend 好友名
 * @param listenter 聊天監聽器
 * @return
 */
public Chat getFriendChat(String friend, MessageListener listenter) {
	if (getConnection() == null)
		return null;
	/** 判断是否创建聊天窗口 */
	for (String fristr : chatManage.keySet()) {
		if (fristr.equals(friend)) {
			// 存在聊天窗口,则返回对应聊天窗口
			return chatManage.get(fristr);
		}
	}
	/** 创建聊天窗口 */
	Chat chat = getConnection().getChatManager().createChat(friend + "@" + getConnection().getServiceName(), listenter);
	/** 添加聊天窗口到chatManage */
	chatManage.put(friend, chat);
	return chat;
}
 
Example #2
Source File: Friend.java    From League-of-Legends-XMPP-Chat-Library with MIT License 6 votes vote down vote up
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;
}
 
Example #3
Source File: MultiUserChatLight.java    From Smack with Apache License 2.0 6 votes vote down vote up
MultiUserChatLight(XMPPConnection connection, EntityJid room) {
    this.connection = connection;
    this.room = room;

    fromRoomFilter = FromMatchesFilter.create(room);
    fromRoomGroupChatFilter = new AndFilter(fromRoomFilter, MessageTypeFilter.GROUPCHAT);

    messageListener = new StanzaListener() {
        @Override
        public void processStanza(Stanza packet) throws NotConnectedException {
            Message message = (Message) packet;
            for (MessageListener listener : messageListeners) {
                listener.processMessage(message);
            }
        }
    };

    connection.addSyncStanzaListener(messageListener, fromRoomGroupChatFilter);
}
 
Example #4
Source File: TaxiChatManagerListener.java    From weixin with Apache License 2.0 5 votes vote down vote up
@Override
public void chatCreated(Chat chat, boolean arg1) {
	chat.addMessageListener(new MessageListener() {

		@Override
		public void processMessage(Chat arg0, Message msg) {//登录用户
			StringUtils.parseName(XmppManager.getInstance().getConnection().getUser());
			//发送消息用户
			msg.getFrom();
			//消息内容
			String body = msg.getBody();
			System.out.println("body--->" + body);
			boolean left = body.substring(0, 1).equals("{");
			boolean right = body.substring(body.length() - 1, body.length()).equals("}");
			if (left && right) {
				try {
					JSONObject obj = new JSONObject(body);
					String type = obj.getString("messageType");
					String chanId = obj.getString("chanId");
					String chanName = obj.getString("chanName");

					System.out.println("---body--->" + body);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			
			Intent intent = new Intent("net.cgt.weixin.chat");
			intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);// 包含从未启动过的应用
			intent.putExtra("from", msg.getFrom());
			intent.putExtra("body", body);
			GlobalParams.activity.sendBroadcast(intent);
		}
	});
}
 
Example #5
Source File: ChatManager.java    From Smack with Apache License 2.0 5 votes vote down vote up
void sendMessage(Chat chat, Message message) throws NotConnectedException, InterruptedException {
    for (Map.Entry<MessageListener, StanzaFilter> interceptor : interceptors.entrySet()) {
        StanzaFilter filter = interceptor.getValue();
        if (filter != null && filter.accept(message)) {
            interceptor.getKey().processMessage(message);
        }
    }
    connection().sendStanza(message);
}
 
Example #6
Source File: MultiUserChatIntegrationTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@SmackIntegrationTest
public void mucTest() throws Exception {
    EntityBareJid mucAddress = JidCreate.entityBareFrom(Localpart.from("smack-inttest-" + randomString), mucService.getDomain());

    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddress);

    final String mucMessage = "Smack Integration Test MUC Test Message " + randomString;
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();

    mucAsSeenByTwo.addMessageListener(new MessageListener() {
        @Override
        public void processMessage(Message message) {
            String body = message.getBody();
            if (mucMessage.equals(body)) {
                resultSyncPoint.signal(body);
            }
        }
    });

    MucCreateConfigFormHandle handle = mucAsSeenByOne.createOrJoin(Resourcepart.from("one-" + randomString));
    if (handle != null) {
        handle.makeInstant();
    }
    mucAsSeenByTwo.join(Resourcepart.from("two-" + randomString));

    mucAsSeenByOne.sendMessage(mucMessage);
    resultSyncPoint.waitForResult(timeout);

    mucAsSeenByOne.leave();
    mucAsSeenByTwo.leave();
}
 
Example #7
Source File: ChatManager.java    From Smack with Apache License 2.0 4 votes vote down vote up
public void addOutgoingMessageInterceptor(MessageListener messageInterceptor, StanzaFilter filter) {
    if (messageInterceptor == null) {
        return;
    }
    interceptors.put(messageInterceptor, filter);
}
 
Example #8
Source File: MultiUserChatLight.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a stanza 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.
 *
 * @param listener TODO javadoc me please
 *            a stanza listener.
 * @return true if the listener was not already added.
 */
public boolean addMessageListener(MessageListener listener) {
    return messageListeners.add(listener);
}
 
Example #9
Source File: MultiUserChatLight.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Removes a stanza listener that was being notified of any new
 * messages in the MUCLight. Only "group chat" messages addressed to this
 * MUCLight were being delivered to the listener.
 *
 * @param listener TODO javadoc me please
 *            a stanza listener.
 * @return true if the listener was removed, otherwise the listener was not
 *         added previously.
 */
public boolean removeMessageListener(MessageListener listener) {
    return messageListeners.remove(listener);
}
 
Example #10
Source File: ChatManager.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Adds an interceptor which intercepts any messages sent through chats.
 *
 * @param messageInterceptor the interceptor.
 */
public void addOutgoingMessageInterceptor(MessageListener messageInterceptor) {
    addOutgoingMessageInterceptor(messageInterceptor, null);
}
 
Example #11
Source File: MultiUserChat.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a stanza 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 listener.
 * @return true if the listener was not already added.
 */
public boolean addMessageListener(MessageListener listener) {
    return messageListeners.add(listener);
}
 
Example #12
Source File: MultiUserChat.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Removes a stanza 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 listener.
 * @return true if the listener was removed, otherwise the listener was not added previously.
 */
public boolean removeMessageListener(MessageListener listener) {
    return messageListeners.remove(listener);
}
 
Example #13
Source File: SingleUserChat.java    From saros with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the chat's {@link MessageListener}.
 *
 * @return the chat's {@link MessageListener}
 */
synchronized MessageListener getMessageListener() {
  return chatStateListener;
}