/** * 被邀请的人 加入房间 (加入到房间) * * @param object * @return */ public String receive(JSONObject object, String sessionID) { String nickname = (String) object.get("nickname"); String mucname = (String) object.get("mucname"); AbstractXMPPConnection conn = conn_map.get(sessionID); MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(conn); MultiUserChat multiChat = mucMgr.getMultiUserChat(mucname); try { multiChat.join(nickname); // multiChat.addMessageListener( new MessageListener() { @Override public void processMessage(Message message) { System.out.println("txt-----------" + message.toXML()); if (getResource(message.getFrom()).equals(getName(message.getTo()))) return; String body = message.getBody(); String username = getName(message.getTo()); sess_map .get(user_map.get(username)) .getAsyncRemote() .sendText(rs.replace("${type}", "msg").replace("${msg}", body + "--helloworld")); } }); } catch (NoResponseException | XMPPErrorException | NotConnectedException e) { e.printStackTrace(); } return null; }
private void registerRoom( MultiUserChat muc, String number, String name, Integer randomInt, int mode) { MUCPacketListener chatListener = new MUCPacketListener(number, muc, name, mode, mCtx); muc.addMessageListener(chatListener); mRoomNumbers.add(randomInt); mRooms.put(number, muc); mMucHelper.addMUC(muc.getRoom(), number, mode); }
/** * Constructor. * * @param chat The chat to adapt */ public ChatMUCAdapter(final MultiUserChat chat, final BeemService service, String nick) { mAdaptee = chat; mParticipant = new Contact(chat.getRoom(), true); mMessages = new LinkedList<Message>(); mAdaptee.addMessageListener(mMsgListener); mNick = nick; mService = service; // Join the MUC try { chat.join(nick); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
// kick off the minions void run() throws MinionsException { try { LOG.debug("Starting MinionsRunner"); ConnectionConfiguration connectionConfiguration; if (StringUtils.isNotBlank(config.getServer())) { connectionConfiguration = new ConnectionConfiguration(config.getServer(), config.getPort(), config.getService()); } else { connectionConfiguration = new ConnectionConfiguration(config.getService(), config.getPort()); } XMPPConnection conn = new XMPPConnection(connectionConfiguration); conn.connect(); conn.login(config.getUser(), config.getPassword(), config.getResource()); LOG.debug(format("Logged in: %s@%s", config.getUser(), config.getService())); MultiUserChat muc = new MultiUserChat(conn, config.getRoom()); if (StringUtils.isBlank(config.getRoomPassword())) { muc.join(config.getRoomNick()); } else { muc.join(config.getRoomNick(), config.getRoomPassword()); } LOG.debug(format("Joined: %s as %s", config.getRoom(), config.getRoomNick())); MinionStore minions = new MinionStore(config.getPluginsDir(), config.getRefreshSeconds(), muc); MinionsListener listener = new MinionsListener(minions, config.getPrefix(), muc, config.getRoomNick()); muc.addMessageListener(listener); Object lock = new Object(); synchronized (lock) { while (true) { lock.wait(); } } } catch (Throwable t) { throw new MinionsException(t); } }
/** * muc */ private void addListenerToMuc(MultiUserChat muc) { if (null != muc) { muc.addMessageListener( new PacketListener() { @Override public void processPacket(Packet packet) { Log.i("AFRIN", "receiving message" + packet.getFrom() + " " + packet.getPacketID()); if (onMessageReceivedListener != null) { onMessageReceivedListener.onMessageReceived((Message) packet); } /*if (onMessageReceivedListener != null) { onMessageReceivedListener.onMessageReceived(packet); }*/ } }); } }
@Override public void chatCreated(Chat chat, boolean createdLocally) { // Set listener for incoming messages. chat.addMessageListener(messageListener); muc2.addMessageListener(packetListener); }
public static void createMucChannel( String channelname, String channeltopic, int userid, int channelid) { try { if (!Application.mucchannels.containsKey(channelid)) { // create MUC MultiUserChat muc = new MultiUserChat(Application.conn, channelname + "@conference.webchat"); System.out.println("Channelname: " + channelname); muc.create(channelname); Application.mucchannels.put(channelid, muc); Form form = muc.getConfigurationForm(); Form submitForm = form.createAnswerForm(); for (Iterator<FormField> fields = form.getFields(); fields.hasNext(); ) { FormField field = (FormField) fields.next(); if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) { submitForm.setDefaultAnswer(field.getVariable()); } } List<String> owners = new ArrayList<String>(); owners.add("webchat@webchat"); submitForm.setAnswer("muc#roomconfig_roomowners", owners); muc.sendConfigurationForm(submitForm); muc.invite( models.User.find.byId(userid).username + "@webchat/Smack", "Einladung in Channel " + channelname); final int chanid = channelid; muc.sendMessage("Willkommen: Topic lautet " + channeltopic + "!"); muc.addMessageListener( new PacketListener() { @Override public void processPacket(Packet packet) { if (packet instanceof Message) { String[] temp; temp = ((Message) packet).getFrom().split("/"); if (!temp[1].equals(models.Channel.find.byId(chanid).name)) { System.out.println("Received user: "******"@webchat/Smack", "Einladung in Channel " + channelname); muc.sendMessage("Willkommen: Topic lautet" + channeltopic + "!"); } } catch (XMPPException exp) { exp.printStackTrace(); } }
/** * 房间创建 * * @param object * @return */ public String createMuc(JSONObject object, String sessionID) { AbstractXMPPConnection connection = conn_map.get(this.session.getId()); String mucName = getName(connection.getUser()) + "-MUC" + CONFERENCE_NAME; String nickName = (String) object.get("nickname"); MultiUserChat muc = null; try { muc = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(mucName); muc.create(nickName); muc.sendConfigurationForm(new Form(DataForm.Type.submit)); muc.addMessageListener( new MessageListener() { @Override public void processMessage(Message message) { System.out.println("TXT--" + message.toXML()); if (getResource(message.getFrom()).equals(getName(message.getTo()))) return; // sess_map.get(user_map.get(getName(message.getTo()))).getAsyncRemote().sendText(message.getBody()+" -- Hello World"); sess_map .get(user_map.get(getName(message.getTo()))) .getAsyncRemote() .sendText( rs.replace("${type}", "msg") .replace("${msg}", message.getBody() + "--helloworld")); } }); // muc.join(nickName); //加入房间 // 如果成员被邀请进来则通知 muc.addParticipantListener( new PresenceListener() { @Override public void processPresence(Presence presence) { // 监听成员的状态 // broadcast(object, sessionID); Collection<Session> values = sess_map.values(); // String from = presence.getFrom(); Iterator<Session> iterator = values.iterator(); // String rs1 = rs.replace("${type}", "broadcast").replace("${nickname}", // nickname).replace("${sessionID}", sessionID); while (iterator.hasNext()) { try { Session next = iterator.next(); String data = rs.replace("${type}", "ParticipantStatus"); System.out.println(data); if (next.isOpen()) next.getBasicRemote().sendText(data); } catch (IOException e) { e.printStackTrace(); } } } }); // 发送通知到页面 sess_map .get(sessionID) .getBasicRemote() .sendText(rs.replace("${type}", "createMuc").replace("${mucname}", mucName)); } catch (XMPPErrorException | SmackException | IOException e1) { e1.printStackTrace(); } return mucName; }
@Override public void joinMuc(String roomName, String nickName) throws RemoteException { if (XmppSession.this.isConnected()) { try { muc = new MultiUserChat(connection, roomName + "@muc." + serveraddress); muc.join(nickName); Log.i("social-lable", "joined conversation!"); occupants = new ArrayList<String>(); Iterator<String> it = muc.getOccupants(); while (it.hasNext()) { String s = it.next(); occupants.add(s); } this.roomName = muc.getRoom(); muc.addUserStatusListener( new DefaultUserStatusListener() { @Override public void kicked(String actor, String reason) { if (listener != null) { try { listener.onMucKicked(null, actor, reason); } catch (RemoteException e) { Log.e("social-lable", "failed to handle kicked", e); } } } }); muc.addParticipantStatusListener( new DefaultParticipantStatusListener() { @Override public void kicked(String participant, String actor, String reason) { if (listener != null) { try { occupants.remove(participant); listener.onMucKicked(participant, actor, reason); } catch (RemoteException e) { Log.e("social-lable", "failed to handle kicked", e); } } } @Override public void joined(String participant) { if (listener != null) { try { occupants.add(participant); listener.onMucJoined(participant); } catch (RemoteException e) { Log.e("social-lable", "failed to handle joined", e); } } } @Override public void left(String participant) { if (listener != null) { try { listener.onMucLeft(participant); } catch (RemoteException e) { Log.e("social-lable", "failed to handle left", e); } } } }); muc.addMessageListener( new PacketListener() { @Override public void processPacket(Packet packet) { if (packet instanceof Message) { Message msg = (Message) packet; if (listener != null) { Log.i("social-lable", "Received message : " + msg.getBody()); String from = msg.getFrom().replaceAll(muc.getRoom() + "/", ""); DelayInformation inf = (DelayInformation) msg.getExtension("x", "jabber:x:delay"); Date sentDate; if (inf != null) { sentDate = inf.getStamp(); } else { sentDate = new Date(); } try { listener.onMucMessage(from, msg.getBody(), sentDate.toString()); } catch (RemoteException e) { Log.e("social-lable", "failed to handle message", e); } } } } }); } catch (XMPPException e) { Log.e("social-lable", "failed to join muc", e); throw new RemoteException(e.getMessage()); } } }