/** * Represents a single connection to a League of Legends chatserver. * * @param server * The chatserver of the region you want to connect to * @param friendRequestPolicy * Determines how new Friend requests are treated. * @param riotApiKey * Your apiKey used to convert summonerId's to name. You can get * your key here <a * href="https://developer.riotgames.com/">developer * .riotgames.com</a> * * @see LolChat#setFriendRequestPolicy(FriendRequestPolicy) * @see LolChat#setFriendRequestListener(FriendRequestListener) */ public LolChat(ChatServer server, FriendRequestPolicy friendRequestPolicy, RiotApiKey riotApiKey) { this.friendRequestPolicy = friendRequestPolicy; this.server = server; if (riotApiKey != null && server.api != null) { this.riotApi = RiotApi.build(riotApiKey, server); } Roster.setDefaultSubscriptionMode(SubscriptionMode.manual); final ConnectionConfiguration config = new ConnectionConfiguration( server.host, 5223, "pvp.net"); config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); config.setSocketFactory(SSLSocketFactory.getDefault()); config.setCompressionEnabled(true); connection = new XMPPTCPConnection(config); addListeners(); }
@Override public void processPacket(Packet packet) { String user = getSharedPreferences(Constant.LOGIN_SET, 0) .getString(Constant.USERNAME, null); if (packet.getFrom().contains(user)) return; // ������Զ���������������ظ�һ�������Ϣ if (Roster.getDefaultSubscriptionMode().equals( SubscriptionMode.accept_all)) { Presence subscription = new Presence(Presence.Type.subscribe); subscription.setTo(packet.getFrom()); XmppConnectionManager.getInstance().getConnection() .sendPacket(subscription); } else { NoticeManager noticeManager = NoticeManager .getInstance(context); Notice notice = new Notice(); notice.setTitle("��������"); notice.setNoticeType(Notice.ADD_FRIEND); notice.setContent(StringUtil.getUserNameByJid(packet.getFrom()) + "�������Ϊ����"); notice.setFrom(packet.getFrom()); notice.setTo(packet.getTo()); notice.setNoticeTime(DateUtil.date2Str(Calendar.getInstance(), Constant.MS_FORMART)); notice.setStatus(Notice.UNREAD); long noticeId = noticeManager.saveNotice(notice); if (noticeId != -1) { Intent intent = new Intent(); intent.setAction(Constant.ROSTER_SUBSCRIPTION); notice.setId("" + noticeId); intent.putExtra("notice", notice); sendBroadcast(intent); setNotiType(R.drawable.icon, "��������", StringUtil.getUserNameByJid(packet.getFrom()) + "�������Ϊ����", MyNoticeActivity.class); } } }
/** * Initializing the XMPPListener. Retrieve connection details provided in * xmpp transport receiver, connect to those servers & start listening in * for messages. */ public void init(ConfigurationContext configurationCtx, TransportInDescription transportIn) throws AxisFault { log.info("Initializing XMPPListener..."); //allow anyone to send message to listening account Roster.setDefaultSubscriptionMode(SubscriptionMode.accept_all); configurationContext = configurationCtx; initializeConnectionFactories(transportIn); if (connectionFactories.isEmpty()) { log.warn("No XMPP connection factories defined." + "Will not listen for any XMPP messages"); return; } }
/** * Represents a single connection to a League of Legends chatserver. * * @param server The chatserver of the region you want to connect to * @param friendRequestPolicy Determines how new Friend requests are treated. * @param riotApiKey Your apiKey used to convert summonerId's to name. You can get * your key here <a * href="https://developer.riotgames.com/">developer * .riotgames.com</a> * @see LolChat#setFriendRequestPolicy(FriendRequestPolicy) * @see LolChat#setFriendRequestListener(FriendRequestListener) */ public LolChat(ChatServer server, FriendRequestPolicy friendRequestPolicy, String riotApiKey) { this.friendRequestPolicy = friendRequestPolicy; if (riotApiKey != null && server.api != null) { this.riotApi = new JRiot(riotApiKey, server.name().toLowerCase()); } Roster.setDefaultSubscriptionMode(SubscriptionMode.manual); final ConnectionConfiguration config = new ConnectionConfiguration( server.host, 5223, "pvp.net"); config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); config.setSocketFactory(SSLSocketFactory.getDefault()); config.setCompressionEnabled(true); connection = new XMPPTCPConnection(config); try { connection.connect(); } catch (Exception e) { System.err.println("Failed to connect to " + server.host); return; } addListeners(); new Thread(new Runnable() { @Override public void run() { while (!stop) { try { Thread.sleep(500);//NOT SURE ABOUT THIS, TODO:REVIST } catch (final InterruptedException ignored) { } } } }).start(); }
private boolean connect() { final String server = config.getString("xmpp.server"); if (server == null || server.equals("example.com")) { LOGGER.log(Level.WARNING, "config broken for xmpp"); return false; } final int port = config.getInt("xmpp.port", 5222); final String serviceName = config.getString("xmpp.servicename", server); final String xmppuser = config.getString("xmpp.user"); final String password = config.getString("xmpp.password"); final ConnectionConfiguration connConf = new ConnectionConfiguration(server, port, serviceName); final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Connecting to xmpp server ").append(server).append(":").append(port); stringBuilder.append(" as user ").append(xmppuser).append("."); LOGGER.log(Level.INFO, stringBuilder.toString()); connConf.setSASLAuthenticationEnabled(config.getBoolean("xmpp.sasl-enabled", false)); connConf.setSendPresence(true); connConf.setReconnectionAllowed(true); connection = new XMPPConnection(connConf); try { connection.connect(); connection.login(xmppuser, password); connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all); chatManager = connection.getChatManager(); chatManager.addChatListener(this); return true; } catch (XMPPException ex) { LOGGER.log(Level.WARNING, "Failed to connect to server: " + server, ex); return false; } }
@Override public void processPacket(Packet packet) { Log.i("TAG", "[服务]收到联系人消息:"+packet.getFrom()); //在登录中保存的 String user = getSharedPreferences(IMConstant.IMSHARE, 0) .getString(IMConstant.USERNAME, null); if (packet.getFrom().contains(user)) return; // 如果是自动接收所有请求,则回复一个添加信息 if (Roster.getDefaultSubscriptionMode().equals( SubscriptionMode.accept_all)) { Presence subscription = new Presence(Presence.Type.subscribe); subscription.setTo(packet.getFrom()); IMUtil.getXMPPConnection() .sendPacket(subscription); } else { // 生成消息历史记录 IMMessage mIMMessage = new IMMessage(); mIMMessage.setTitle("好友请求"); String from = IMUtil.getUserNameByJid(packet.getFrom()); mIMMessage.setContent(from + "申请加您为好友"); Log.i("TAG", "[服务]收到好友请求:"+from + "申请加您为好友"); mIMMessage.setSendState(IMMessage.RECEIVED); mIMMessage.setType(IMMessage.ADD_FRIEND_MSG); //发送方 mIMMessage.setUserName(from); //接收方 mIMMessage.setToUserName(IMUtil.getUserNameByJid(packet.getTo())); mIMMessage.setReadState(IMMessage.UNREAD); mIMMessage.setRequestState(IMMessage.ALL); String time = AbDateUtil.getCurrentDate(AbDateUtil.dateFormatYMDHMS); mIMMessage.setTime(time); //保存本地 mIMMsgDao.startWritableDatabase(false); long messageId = mIMMsgDao.insert(mIMMessage); mIMMsgDao.closeDatabase(); if (messageId != -1) { //发出接收到会话的消息 Intent intent = new Intent(); intent.setAction(IMConstant.ACTION_ROSTER_SUBSCRIPTION); intent.putExtra("MESSAGE", mIMMessage); sendBroadcast(intent); } } }
/** * {@inheritDoc} */ @Override public void onCreate() { super.onCreate(); registerReceiver(mReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); mSettings = PreferenceManager.getDefaultSharedPreferences(this); mSettings.registerOnSharedPreferenceChangeListener(mPreferenceListener); if (mSettings.getBoolean(BeemApplication.USE_AUTO_AWAY_KEY, false)) { mOnOffReceiverIsRegistered = true; registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF)); registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON)); } String tmpJid = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "").trim(); mLogin = StringUtils.parseName(tmpJid); boolean useSystemAccount = mSettings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false); mPort = DEFAULT_XMPP_PORT; mService = StringUtils.parseServer(tmpJid); mHost = mService; initMemorizingTrustManager(); if (mSettings.getBoolean(BeemApplication.ACCOUNT_SPECIFIC_SERVER_KEY, false)) { mHost = mSettings.getString(BeemApplication.ACCOUNT_SPECIFIC_SERVER_HOST_KEY, "").trim(); if ("".equals(mHost)) mHost = mService; String tmpPort = mSettings.getString(BeemApplication.ACCOUNT_SPECIFIC_SERVER_PORT_KEY, "5222"); if (!"".equals(tmpPort)) mPort = Integer.parseInt(tmpPort); } if (mSettings.getBoolean(BeemApplication.FULL_JID_LOGIN_KEY, false) || "gmail.com".equals(mService) || "googlemail.com".equals(mService) || useSystemAccount) { mLogin = tmpJid; } configure(ProviderManager.getInstance()); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Roster.setDefaultSubscriptionMode(SubscriptionMode.manual); mBind = new XmppFacade(this); Log.d(TAG, "Create BeemService"); }