Java 类org.jivesoftware.smack.ChatManager 实例源码

项目:libraries    文件:MessageSender.java   
public void send(final IMessage message) throws NotificationException {
  XMPPConnection connection = null;
  try {
    connection = createConnection();
    final ChatManager chatManager = connection.getChatManager();
    for (final String body : this.bodies) {
      send(chatManager, body, message);
    }
  } catch (final XMPPException exception) {
    logger.log(ILevel.ERROR, exception.getMessage(), exception);
    throw new NotificationException("Coudn't send notification. " + toString(message), exception); //$NON-NLS-1$
  } finally {
    if (connection != null) {
      connection.disconnect();
    }
  }
}
项目:LOL-Chat    文件:Friend.java   
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;
}
项目:SmackDemo    文件:MainActivity.java   
private void initState() {
    getSupportActionBar().setTitle(XMPPManager.getInstance().getState().getMessage());
    chatManager = ChatManager.getInstanceFor(XMPPManager.getInstance().connection);
    chatManager.addChatListener(new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally) {
            if (createdLocally) {
               // Chat created by myself
            } else {
               // Chat created by others
                chat.addMessageListener(chatMessageListener);
            }

        }
    });

    if (!TextUtils.isEmpty(username)) {
        chat = chatManager.createChat(username + "@" + XMPPManager.serverName, chatMessageListener);
    }

}
项目:system_updater    文件:Bot.java   
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);
    }

}
项目:League-of-Legends-XMPP-Chat-Library    文件:Friend.java   
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;
}
项目:opennmszh    文件:XMPPNotificationManager.java   
/**
 * <p>sendMessage</p>
 *
 * @param xmppTo a {@link java.lang.String} object.
 * @param xmppMessage a {@link java.lang.String} object.
 * @return a boolean.
 */
public boolean sendMessage(String xmppTo, String xmppMessage) {
    if (!isLoggedIn()) {
        connectToServer();
    }
    try {
        ChatManager cm = xmpp.getChatManager();
        cm.createChat(xmppTo, new NullMessageListener()).sendMessage(xmppMessage);
        log().debug("XMPP Manager sent message to: " + xmppTo);
    } catch (XMPPException e) {
        log().fatal("XMPP Exception Sending message ", e);
        return false;
    }

    return true;

}
项目:OpenNMS    文件:XMPPNotificationManager.java   
/**
 * <p>sendMessage</p>
 *
 * @param xmppTo a {@link java.lang.String} object.
 * @param xmppMessage a {@link java.lang.String} object.
 * @return a boolean.
 */
public boolean sendMessage(String xmppTo, String xmppMessage) {
    if (!isLoggedIn()) {
        connectToServer();
    }
    try {
        ChatManager cm = xmpp.getChatManager();
        cm.createChat(xmppTo, new NullMessageListener()).sendMessage(xmppMessage);
        log().debug("XMPP Manager sent message to: " + xmppTo);
    } catch (XMPPException e) {
        log().fatal("XMPP Exception Sending message ", e);
        return false;
    }

    return true;

}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 创建聊天
 * @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;
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 创建聊天
 * @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;
}
项目:AyoSunny    文件:MsfService.java   
/**
 * 初始化XMPP
 */
void initXMPP() {
    mXMPPConnection = mXmppConnectionManager.init();                        //初始化XMPPConnection
    loginXMPP();                                                            //登录XMPP
    ChatManager chatmanager = mXMPPConnection.getChatManager();
    chatmanager.addChatListener(new ChatManagerListener() {
        @Override
        public void chatCreated(Chat arg0, boolean arg1) {
            arg0.addMessageListener(new MsgListener(MsfService.this, mNotificationManager));
        }
    });
}
项目:AyoSunny    文件:XmppUtil.java   
/**
 * 发送消息
 * @param position
 * @param content
 * @param touser
 * @throws XMPPException
 */
public static void sendMessage(XMPPConnection mXMPPConnection,String content,String touser) throws XMPPException {
    if(mXMPPConnection==null||!mXMPPConnection.isConnected()){
        throw new XMPPException();
    }
    ChatManager chatmanager = mXMPPConnection.getChatManager();
    Chat chat =chatmanager.createChat(touser + "@" + Const.XMPP_HOST, null);
    if (chat != null) {
        chat.sendMessage(content);
        Log.e("jj", "发送成功");
    }
}
项目:SmackAndroidDemo    文件:SmackConnection.java   
private void sendMessage(String body, String toJid) {
    Log.i(TAG, "sendMessage()");
    Chat chat = ChatManager.getInstanceFor(mConnection).createChat(toJid, this);
    try {
        chat.sendMessage(body);
    } catch (SmackException.NotConnectedException | XMPPException e) {
        e.printStackTrace();
    }
}
项目:SmackAndroidDemo    文件:SmackConnection.java   
private void sendMessage(String body, String toJid) {
    Log.i(TAG, "sendMessage()");
    Chat chat = ChatManager.getInstanceFor(mConnection).createChat(toJid, this);
    try {
        chat.sendMessage(body);
    } catch (SmackException.NotConnectedException | XMPPException e) {
        e.printStackTrace();
    }
}
项目:wso2-axis2-transports    文件:XMPPSender.java   
/**
 * Replies to IM clients via a chat message. The reply contains the invocation response as a string. 
 * @param msgCtx
 * @param responseMsg
 * @throws AxisFault
 */
private static void sendChatMessage(MessageContext msgCtx,String responseMsg) throws AxisFault {
        XMPPConnection xmppConnection = null;
        XMPPOutTransportInfo xmppOutTransportInfo = null;           
        Message message = new Message();

        xmppOutTransportInfo = (XMPPOutTransportInfo)msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
        if(xmppOutTransportInfo != null){
            message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
            xmppConnection = xmppOutTransportInfo.getConnectionFactory().getXmppConnection();
            if(xmppConnection == null){
                handleException("Connection to XMPP Server is not established.");           
            }                   
        }else{
            handleException("Could not find message sender details.");
        }               

        //initialize the chat manager using connection
        ChatManager chatManager = xmppConnection.getChatManager();
        Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);         
        try{            
            message.setProperty(XMPPConstants.SEQUENCE_ID, 
                    xmppOutTransportInfo.getSequenceID());
            message.setBody(responseMsg);       
            chat.sendMessage(message);
            log.debug("Sent message :"+message.toXML());
        } catch (XMPPException e) {
            XMPPSender.handleException("Error occurred while sending the message : "+message.toXML(),e);
        }
    }
项目:sonar-xmpp-notifications    文件:XMPPAdapter.java   
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());

}
项目:sonar-xmpp-notifications    文件:XMPPAdapterTest.java   
@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());
}
项目:visual-programming    文件:XmppAgent.java   
public void connect() {
    try {

        ConnectionConfiguration config = new ConnectionConfiguration(server, 5222);
        config.setCompressionEnabled(true);
        config.setSASLAuthenticationEnabled(true);
        config.setReconnectionAllowed(true);


        connection = new XMPPConnection(config);

        connection.connect();

        connection.login(username, password);

        final ChatManager chatManager = connection.getChatManager();
        chatManager.addChatListener(new ChatManagerListener() {

            @Override
            public void chatCreated(Chat chat, boolean createdLocally) {
                chat.addMessageListener(getMessageListener());

            }
        });
    } catch (XMPPException e) {

        throw new RuntimeException(e);
    }
}
项目:visual-programming    文件:XmppAgent.java   
public void send(String receiverAddress, Message msg) throws XMPPException {

        if (msg.messageType == MessageType.SyncRequest)
            setBlockedOutgoingRequest(receiverAddress, msg);

        ChatManager chatManager = connection.getChatManager();
        Chat chat = chatManager.createChat(receiverAddress,
                getMessageListener());

        // TODO convert body to hierarchical text
        String msgBody = "";
        if (msg.body != null) {
            if (msg.body.getType() == ObjectType.NORMAL) {
                for (int i = 0; i < msg.body.getFieldCount(); i++) {
                    if (i != 0)
                        msgBody += ";";

                    _Object param = msg.body.getField(i).getTarget();
                    msgBody += param.getName() + ":" + param.getValue();

                }
            } else if (msg.body.getType().isValueObject()) {
                msgBody = msg.body.getValue();

            }

        }

        // String response = String.format("[%s] %s", msg.getSubject(),
        // msgBody);

        chat.sendMessage(msgBody);
    }
项目:Beem    文件:BeemChatManager.java   
/**
    * Constructor.
    * @param chatManager the smack ChatManager to adapt
    * @param service the service which runs the chat manager
    * @param roster roster used to get presences changes
    */
   public BeemChatManager(final ChatManager chatManager, final BeemService service, final Roster roster) {
mService = service;
mAdaptee = chatManager;
mRoster = roster;
mRoster.addRosterListener(mChatRosterListn);
mAdaptee.addChatListener(mChatListener);
   }
项目:beem-fork-xmpp    文件:BeemChatManager.java   
/**
    * Constructor.
    * @param chatManager the smack ChatManager to adapt
    * @param service the service which runs the chat manager
    * @param roster roster used to get presences changes
    */
   public BeemChatManager(final ChatManager chatManager, final BeemService service, final Roster roster) {
mService = service;
mAdaptee = chatManager;
mRoster = roster;
mRoster.addRosterListener(mChatRosterListn);
mAdaptee.addChatListener(mChatListener);
   }
项目:libraries    文件:MessageSender.java   
private void send(final ChatManager chatManager, final String body, final IMessage message) throws XMPPException {
  final Chat chat = chatManager.createChat(body, new DeafMessageListener());
  chat.sendMessage(toString(message));
}
项目:Camel    文件:XmppConsumer.java   
@Override
protected void doStart() throws Exception {
    try {
        connection = endpoint.createConnection();
    } catch (SmackException e) {
        if (endpoint.isTestConnectionOnStartup()) {
            throw new RuntimeException("Could not connect to XMPP server.", e);
        } else {
            LOG.warn(e.getMessage());
            if (getExceptionHandler() != null) {
                getExceptionHandler().handleException(e.getMessage(), e);
            }
            scheduleDelayedStart();
            return;
        }
    }

    chatManager = ChatManager.getInstanceFor(connection);
    chatManager.addChatListener(this);

    OrFilter pubsubPacketFilter = new OrFilter();
    if (endpoint.isPubsub()) {
        //xep-0060: pubsub#notification_type can be 'headline' or 'normal'
        pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.headline));
        pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.normal));
        connection.addPacketListener(this, pubsubPacketFilter);
    }

    if (endpoint.getRoom() == null) {
        privateChat = chatManager.getThreadChat(endpoint.getChatId());

        if (privateChat != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding listener to existing chat opened to " + privateChat.getParticipant());
            }
            privateChat.addMessageListener(this);
        } else {
            privateChat = ChatManager.getInstanceFor(connection).createChat(endpoint.getParticipant(), endpoint.getChatId(), this);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Opening private chat to " + privateChat.getParticipant());
            }
        }
    } else {
        // add the presence packet listener to the connection so we only get packets that concerns us
        // we must add the listener before creating the muc

        final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class));
        connection.addPacketListener(this, packetFilter);

        muc = new MultiUserChat(connection, endpoint.resolveRoom(connection));
        muc.addMessageListener(this);
        DiscussionHistory history = new DiscussionHistory();
        history.setMaxChars(0); // we do not want any historical messages

        muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getDefaultPacketReplyTimeout());
        if (LOG.isInfoEnabled()) {
            LOG.info("Joined room: {} as: {}", muc.getRoom(), endpoint.getNickname());
        }
    }

    this.startRobustConnectionMonitor();
    super.doStart();
}
项目:carbon-identity    文件:MPAuthenticationProvider.java   
/**
 * Read the PIN number sent by the user as the reply.
 *
 * @param connection
 * @param userName
 * @return
 */
public boolean getUserResponse(XMPPConnection connection, String userName) {

    String response = null;
    Presence presence = connection.getRoster().getPresence(userName);

    if (presence.isAvailable()) {
        try {

            ChatManager chatManager = connection.getChatManager();
            Chat chat = chatManager.createChat(userName, null);
            PacketFilter filter =
                    new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(userName));
            XmppResponseListener chatListener = new XmppResponseListener();
            connection.addPacketListener(chatListener, filter);

            if (isPINEnabled) {

                chat.sendMessage("Please reply with your PIN Number here.");

                if (log.isInfoEnabled()) {
                    log.info("User PIN is sent to the user and awaiting for the response.");
                }

                while (!chatListener.isResponseReceived()) {
                    Thread.sleep(100);
                }

                response = chatListener.getResponse();

                if (response != null) {
                    return userPIN.contentEquals(response.trim());
                }
            } else {
                chat.sendMessage(
                        "You are about to get authenticated for your OpenID. Do you want to continue: [Yes] or [No]");

                if (log.isInfoEnabled()) {
                    log.info("User PIN is sent to the user and awaiting for the response.");
                }

                while (!chatListener.isResponseReceived()) {
                    Thread.sleep(100);
                }

                response = chatListener.getResponse();

                if (response != null) {
                    if ("YES".equalsIgnoreCase(response.trim())) {
                        return true;
                    } else if ("NO".equalsIgnoreCase(response.trim())) {
                        return false;
                    } else {
                        pinDisabledResponse = false;
                        return false;
                    }
                }
            }

        } catch (Exception e) {
            log.error("Error while getting user response", e);
        }
    } else {
        return false;
    }
    return false;

}
项目:SmackAndroidDemo    文件:SmackConnection.java   
public void connect() throws IOException, XMPPException, SmackException {
    Log.i(TAG, "connect()");

    XMPPTCPConnectionConfiguration.XMPPTCPConnectionConfigurationBuilder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setServiceName(mServiceName);
    builder.setResource("SmackAndroidTestClient");
    builder.setUsernameAndPassword(mUsername, mPassword);
    builder.setRosterLoadedAtLogin(true);


    mConnection = new XMPPTCPConnection(builder.build());

    //Set ConnectionListener here to catch initial connect();
    mConnection.addConnectionListener(this);

    mConnection.connect();
    mConnection.login();

    PingManager.setDefaultPingInterval(600); //Ping every 10 minutes
    PingManager pingManager = PingManager.getInstanceFor(mConnection);
    pingManager.registerPingFailedListener(this);

    setupSendMessageReceiver();

    ChatManager.getInstanceFor(mConnection).addChatListener(this);
    mConnection.getRoster().addRosterListener(this);

}
项目:SmackAndroidDemo    文件:SmackConnection.java   
public void connect() throws IOException, XMPPException, SmackException {
    Log.i(TAG, "connect()");

    XMPPTCPConnectionConfiguration.XMPPTCPConnectionConfigurationBuilder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setServiceName(mServiceName);
    builder.setResource("SmackAndroidTestClient");
    builder.setUsernameAndPassword(mUsername, mPassword);
    builder.setRosterLoadedAtLogin(true);


    mConnection = new XMPPTCPConnection(builder.build());

    //Set ConnectionListener here to catch initial connect();
    mConnection.addConnectionListener(this);

    mConnection.connect();
    mConnection.login();

    PingManager.setDefaultPingInterval(600); //Ping every 10 minutes
    PingManager pingManager = PingManager.getInstanceFor(mConnection);
    pingManager.registerPingFailedListener(this);

    setupSendMessageReceiver();

    ChatManager.getInstanceFor(mConnection).addChatListener(this);
    mConnection.getRoster().addRosterListener(this);

}
项目:NewCommunication-Android    文件:XmppManager.java   
private void initChatManager(){

    ChatManager chatManager = this.getConnection().getChatManager();

    chatManager.addChatListener(chatManagerListener);

}