简单介绍:
使用一个开源SocketClient库
地址:https://github.com/vilyever/AndroidSocketClient
参考于:https://blog.csdn.net/jiuyuefenglove/article/details/80089384
Android studio项目中配置:
接着在整个工程的build.gradle里添加如下配置:
buildscript和allprojects下添加:
maven { url "https://jitpack.io" }
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
然后还要在app下的build.gradle里配置:
使用新版本导致onResponse一直不回调。后来我使用一个老的版本(2.0.1)
compile 'com.github.vilyever:AndroidSocketClient:2.0.1'
添加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
public class MainActivity extends AppCompatActivity {
private SocketClient socketClient;
private SocketClientDelegate delegate;
private EditText et_text;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_text = (EditText) findViewById(R.id.et_text);
btn_send = (Button) findViewById(R.id.btn_send);
try {
startSocketClient("");
} catch (IOException e) {
e.printStackTrace();
}
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String string = et_text.getText().toString().trim();
if (!TextUtils.isEmpty(string)){
socketSendMessage(string);
et_text.setText("");
}
}
});
}
//socket通信
private void startSocketClient(String info) throws IOException {
//String[] array = info.split("_");
socketClient = new SocketClient();
//socketClient.getAddress().setRemoteIP(array[0]);//设置IP,这里设置的是本地IP
//socketClient.getAddress().setRemotePort(String.valueOf(Integer.parseInt(array[1])));//设置端口
socketClient.getAddress().setRemoteIP("49.4.69.20");//设置IP,这里设置的是本地IP
socketClient.getAddress().setRemotePort(16018);//设置端口
socketClient.getAddress().setConnectionTimeout(15 * 1000);//设置超时时间
//socketClient.setCharsetName(CharsetUtil.UTF_8);//设置编码格式,默认为UTF-8
socketClient.setCharsetName("GBK");//设置编码格式,默认为UTF-8
socketClient.connect(); // 连接,异步进行
//常用回调配置
// 对应removeSocketClientDelegate
socketClient.registerSocketClientDelegate(delegate = new SocketClientDelegate() {
/**
* 连接上远程端时的回调
*/
@Override
public void onConnected(SocketClient client) {
Log.d("melog", "gamesocket连接成功");
//launcher.callExternalInterface("gameSocketConnectSuccess", "success");
}
/**
* 与远程端断开连接时的回调
*/
@Override
public void onDisconnected(SocketClient client) {
Log.d("melog", "gamesocket连接断开");
// 可在此实现自动重连
socketClient.connect();
//launcher.callExternalInterface("socketClose", "close");
}
/**
* 接收到数据包时的回调
*/
@Override
public void onResponse(SocketClient client, @NonNull SocketResponsePacket responsePacket) {
String message = responsePacket.getMessage(); // 获取按默认设置的编码转化的String,可能为null
Log.i("接收服务端消息:",message);
//launcher.callExternalInterface("socketDataHandler", message);
}
});
}
//发送消息
private void socketSendMessage(String info) {
String status = String.valueOf(socketClient.getState());
if (socketClient != null && status == "Connected") {
socketClient.sendData(info.getBytes()); // 发送byte[]消息
}
}
//前台请求gamesocket连接状态
private void requestGameSocketConnectFlg() {
String status = String.valueOf(socketClient.getState());
//launcher.callExternalInterface("gameSocketFlg", status);
}
//前台主动断开gamesocket
private void closeGameSocket() {
if (socketClient != null) {
socketClient.removeSocketClientDelegate(delegate);
socketClient.disconnect();
}
}
}