作业帮 > 优秀作文 > 教育资讯

camchatrooms

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/14 09:13:25 优秀作文
camchatrooms优秀作文

篇一:xmpp-多人聊天实例

一直想写东西,但是上班不能在CSDN上写东西。下班回家又忙着学Iphone开发,唉,两个个字,忒忙。今天就写写Smack的聊天室功能吧。

先上代码:

view plain 1. import java.io.BufferedReader;

2. imp

camchatrooms

ort java.io.InputStreamReader;

3. import java.util.ArrayList;

4. import java.util.Collection;

5. import java.util.Iterator;

6. import java.util.List;

7.

8. import org.jivesoftware.smack.Chat;

9. import org.jivesoftware.smack.ConnectionConfiguration;

10. import org.jivesoftware.smack.MessageListener;

11. import org.jivesoftware.smack.PacketListener;

12. import org.jivesoftware.smack.SmackConfiguration;

13. import org.jivesoftware.smack.XMPPConnection;

14. import org.jivesoftware.smack.XMPPException;

15. import org.jivesoftware.smack.packet.Message;

16. import org.jivesoftware.smack.packet.Packet;

17. import org.jivesoftware.smack.provider.ProviderManager;

18. import org.jivesoftware.smackx.Form;

19. import org.jivesoftware.smackx.FormField;

20. import org.jivesoftware.smackx.ServiceDiscoveryManager;

21. import org.jivesoftware.smackx.muc.DefaultParticipantStatusListener;

22. import org.jivesoftware.smackx.muc.DefaultUserStatusListener;

23. import org.jivesoftware.smackx.muc.DiscussionHistory;

24. import org.jivesoftware.smackx.muc.HostedRoom;

25. import org.jivesoftware.smackx.muc.InvitationListener;

26. import org.jivesoftware.smackx.muc.InvitationRejectionListener;

27. import org.jivesoftware.smackx.muc.MultiUserChat;

28. import org.jivesoftware.smackx.muc.RoomInfo;

29. import org.jivesoftware.smackx.muc.SubjectUpdatedListener;

30. import org.jivesoftware.smackx.packet.ChatStateExtension;

31. import org.jivesoftware.smackx.packet.DiscoverInfo;

32. import org.jivesoftware.smackx.packet.DiscoverItems;

33. import org.jivesoftware.smackx.packet.OfflineMessageInfo;

34. import org.jivesoftware.smackx.packet.OfflineMessageRequest;

35. import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;

36. import org.jivesoftware.smackx.provider.BytestreamsProvider;

37. import org.jivesoftware.smackx.provider.DataFormProvider;

38. import org.jivesoftware.smackx.provider.DiscoverInfoProvider;

39. import org.jivesoftware.smackx.provider.DiscoverItemsProvider;

40. import org.jivesoftware.smackx.provider.IBBProviders;

41. import org.jivesoftware.smackx.provider.MUCAdminProvider;

42. import org.jivesoftware.smackx.provider.MUCOwnerProvider;

43. import org.jivesoftware.smackx.provider.MUCUserProvider;

44. import org.jivesoftware.smackx.provider.StreamInitiationProvider;

45. import org.jivesoftware.smackx.provider.VCardProvider;

46. import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;

47.

48. public class TestSmack2 {

49. public static void main(String[] args) {XMPPConnection.DEBUG_ENABLED = true;

50. final ConnectionConfiguration connectionConfig = new ConnectionConfiguration("

PC2010102716", 5222, "");

51. connectionConfig.setSASLAuthenticationEnabled(false);

52. ProviderManager pm = ProviderManager.getInstance();

53. configure(pm);

54. try {

55. XMPPConnection connection = new XMPPConnection(connectionConfig);

56. connection.connect();//连接

57. initFeatures(connection);

58. connection.login("test", "test");//登陆

59. //聊天室

60. //MultiUserChat multiUserChat = new MultiUserChat(connection, new Invitati

onListener() {});

61. //查找服务

62. System.out.println(connection.getServiceName());

63. List col = getConferenceServices(connection.getServiceName(), conn

ection);

64. for (Object aCol : col) {

65. String service = (String) aCol;

66. //查询服务器上的聊天室

67. Collection rooms = MultiUserChat.getHostedRooms(connection

, service);

68. for(HostedRoom room : rooms) {

69. //查看Room消息

70. System.out.println(room.getName() + " - " +room.getJid());

71. RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, room.get

Jid());

72. if(roomInfo != null) {

73. System.out.println(roomInfo.getOccupantsCount() + " : " + room

Info.getSubject());

74. }

75. }

76. }

77.

78. /*---创建默认配置的聊天室 ---

79. 先看看官方的文档:

80. Creates a new multi user chat with the specified connection and room name.

Note: no

81. * information is sent to or received from the server until you attem

pt to

82. * {@link #join(String) join} the chat room. On some server implementa

tions,

83. * the room will not be created until the first person joins it

84. * 最重要一句:直到用户调用join方法的时候聊天室才会被创建

85. */

86. MultiUserChat muc = new MultiUserChat(connection, "instant@conference.pc20

10102716");

87. muc.create("user1");

88. muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

89.

90. //----创建手动配置聊天室----

91. muc = new MultiUserChat(connection, "reserved4@conference.pc2010102716");

92.

93. //销毁聊天室

94. //muc.destroy("Test", null);

95. muc.create("user2");

96. //获取聊天室的配置表单

97. Form form = muc.getConfigurationForm();

98. //根据原始表单创建一个要提交的新表单

99. Form submitForm = form.createAnswerForm();

100. //向提交的表单添加默认答复

101. for(Iterator fields = form.getFields(); fields.hasNext();) { 102. FormField field = (FormField) fields.next();

103. if(!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable

() != null) {

104. submitForm.setDefaultAnswer(field.getVariable());

105. }

106. }

107. //重新设置聊天室名称

108. submitForm.setAnswer("muc#roomconfig_roomname", "Reserved4 Room"); 109. //设置聊天室的新拥有者

110. List owners = new ArrayList();

111. owners.add("test@pc2010102716");

112. submitForm.setAnswer("muc#roomconfig_roomowners", owners);

113. //设置密码

114. submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true); 115. submitForm.setAnswer("muc#roomconfig_roomsecret", "reserved");

116. //设置描述

117. submitForm.setAnswer("muc#roomconfig_roomdesc", "新创建的reserved聊天室

");

118. //设置聊天室是持久聊天室,即将要被保存下来

119. //submitForm.setAnswer("muc#roomconfig_persistentroom", true);

120. //发送已完成的表单到服务器配置聊天室

121. muc.sendConfigurationForm(submitForm);

122.

123. //加入聊天室(使用昵称喝醉的毛毛虫 ,使用密码ddd)

124. muc = new MultiUserChat(connection, "ddd@conference.pc2010102716"); 125. muc.join("喝醉的毛毛虫", "ddd");

126.

127. //监听消息

128. muc.addMessageListener(new PacketListener() {

129. @Override

130. public void processPacket(Packet packet) {

131. Message message = (Message) packet;

132. System.out.println(message.getFrom() + " : " + message.getBody());

;

133. }

134. });

135.

136. //muc = new MultiUserChat(connection, "ddd@conference.pc2010102716"); 137. //muc.join("喝醉的毛毛虫", "ddd");

138.

139. //加入聊天室(使用昵称喝醉的毛毛虫 ,使用密码ddd)并且获取聊天室里最后5条信息, 140. //注:addMessageListener监听器必须在此join方法之前,否则无法监听到需要的5条消

141. muc = new MultiUserChat(connection, "ddd@conference.pc2010102716"); 142. DiscussionHistory history = new DiscussionHistory();

143. history.setMaxStanzas(5);

144. muc.join("喝醉的毛毛虫

", "ddd", history, SmackConfiguration.getPacketReplyTimeout());

145.

146. //监听拒绝加入聊天室的用户

147. muc.addInvitationRejectionListener(new InvitationRejectionListener() { 148. @Override

149. public void invitationDeclined(String invitee, String reason) { 150. System.out.println(invitee + " reject invitation, reason is " + reason

);

151. }

152. });

153. //邀请用户加入聊天室

154. muc.invite("test3@pc2010102716", "大家来谈谈人生");

155. //监听邀请加入聊天室请求

156. MultiUserChat.addInvitationListener(connection, new InvitationListener() {

157. @Override

158. public void invitationReceived(XMPPConnection conn, String room, Strin

g inviter, String reason, String password, Message message) {

159. //例:直接拒绝邀请

160. MultiUserChat.decline(conn, room, inviter, "你丫很闲啊!"); 161. }

162. });

163.

164.

165. //根据roomJID获取聊天室信息

166. RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, "ddd@conference.

pc2010102716");

167. System.out.println(roomInfo.getRoom() + "-" + roomInfo.getSubject() + "-"

+ roomInfo.getOccupantsCount());

168.

169. //判断用户是否支持Multi-User聊天协议

170. //注:需要加上资源标识符

171. boolean supports = MultiUserChat.isServiceEnabled(connection, "test3@pc201

0102716/spark");

172. //获取某用户所加入的聊天室

173. if(supports) {

174. Iterator joinedRooms = MultiUserChat.getJoinedRooms(connection

, "test3@pc2010102716/spark");

175. while(joinedRooms.hasNext()) {

176. System.out.println("test3 has joined Room " + joinedRooms.next());

177. }

178. }

179.

180. //与聊天室用户私聊

181. Chat chat = muc.createPrivateChat("ddd@conference.pc2010102716/飞鸟

", new MessageListener() {

182. @Override

183. public void processMessage(Chat chat, Message message) {

184. System.out.println("Private Chat: Received message from " + messa

ge.getFrom() + "-" + message.getBody());

185. }

186. });

187. chat.sendMessage("今天不用加班吧?");

优秀作文