需要了解
- 联网有两种:
- 短连接(http连接):必须客户端发请求,服务器返回,连接断开,下一次再连接服务,必须客户端先发请求
- 长连接,实现服务器推送信息给客户端,客户端先连上服务器,连接不断开。客户端再发信息不用建立连接
- Asmack 就是长连接框架,是做聊天应用的客户端的框架
- Openfire服务器,类似于tomcat,tomcat是短连接,客户端请求,tomcat返回网页;openfire是长连接,把一个用户聊天说的话主动推送给所有人
服务端Openfire
xmpp协议是一种规范的协议,你可以在这里看到整个协议的文档http://xmpp.org/
,您只需要知道xmpp是一种即使通讯协议就可以了,支持文本、视频、文件等的传输
同时,这种协议很成熟,很多大公司都在使用,例如google。
官网下载 或者 我的网盘下载提取码:w6lc
下载推荐的这个即可。这个版本不包含Java JRE。你如果没有java运行环境,那么可以下载包含Java JRE的版本,这个包可以帮助你安装一下java
下载完后,你可以一路什么都不管,就直接安装openfire就可以了。安装完成后然后在浏览器输入
http://localhost:9090
会出现设置界面,语言选择中文
设置主机的访问ip地址
本地的域,要设置为127.0.0.1
密码我设置的123456
管理界面,账号是admin 密码是刚才设置的123456
登录成功
更多openfire的学习
http://www.myopenfire.com/
客户端Spark
spark从本质上来说就是一个运行在PC上的java程序,你可以看成是官方为我们实现好的运行在PC上的客户端,我们只需要下载使用即可。
(当然如果你项目的需求现有的spark无法满足,你可以选择在官方下载spark的源码,对它进行修改)
继续一路安装
在登录界面,我们可以使用之前openfire的账号:admin 密码:123456 进行登录,或者我们可以在openfire中创建新用户,如下图:
这里我们创建一个账号:spark 密码:123456 的账号
登录界面,点击高级,进入高级设置,修改以下选项
保存后,登录页登录
登录成功
Asmack
Asmack Github地址,可以找到相关jar包进行下载,但是网址打不开…所以在网上找了一个jar包,可以去、我的网盘下载,提取码:5xcq
实践一下
加入群聊
1、现在创建一个群聊
2、新建一个项目ChatApplication
3、把之前下载的jar包放入项目lib文件夹下,添加到项目
4、创建MyApplication,并编写代码
public class MyApplication extends Application {
//asmack框架提供,负责连接到openfire
XMPPConnection xmppConnection;
MultiUserChat multiUserChat;
@Override
public void onCreate() {
super.onCreate();
//连接服务器,起一个线程
new Thread() {
@Override
public void run() {
try {
//1.设置服务器信息
ConnectionConfiguration configuration = new ConnectionConfiguration("10.0.2.2", 5222, "Spark");
//2.连接上服务器
xmppConnection = new XMPPConnection(configuration);
xmppConnection.connect();
//3.登录
xmppConnection.login("spark","123456");
//4.加入群聊
String room = "729@conference.127.0.0.1";
multiUserChat = new MultiUserChat(xmppConnection,room);
multiUserChat.join("Errol_King");
} catch (XMPPException e) {
e.printStackTrace();
}
}
}.start();
}
}
5、添加联网权限,并且注册Application
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".MyApplication"
......
6、运行程序,然后去后台查看一下群聊成员
值得说明的是以下几点:
注意:设置服务器信息时
ConnectionConfiguration configuration = new ConnectionConfiguration("10.0.2.2", 5222, "Spark");
这里的三个参数分别对应上面Spark中的高级设置的主机、端口、资源
也就是其实应该写
ConnectionConfiguration configuration = new ConnectionConfiguration("127.0.0.1", 5222, "Spark");
但是由于是本地站点,如果想让模拟机访问电脑本地站点需要写成10.0.2.2
,具体请查看Android模拟机访问本地站点
注意:登录时
xmppConnection.login("spark","123456");
这里的两个参数就是之前创建的用户的账号、密码
注意:房间信息
String room = "729@conference.127.0.0.1";
这里的字符串是房间名称,房间名称为:“房间名称@聊天服务”
具体点进openfire的房间设置即可
注意:设置昵称
multiUserChat.join("Errol_King");
发送消息
写一个简单布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"/>
</LinearLayout>
</LinearLayout>
MyApplication中的两个变量改为public static
MainActivity中直接调用方法即可
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
button = findViewById(R.id.send);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send();
}
});
}
private void send() {
try {
MyApplication.multiUserChat.sendMessage(editText.getText().toString());
} catch (XMPPException e) {
e.printStackTrace();
}
}
}