A simple Android socket communication, you're gonna love it!
compile 'com.virtue.androidsocket:AndroidSocket:1.0.7'
<dependency>
<groupId>com.virtue.androidsocket</groupId>
<artifactId>AndroidSocket</artifactId>
<version>1.0.7</version>
<type>pom</type>
</dependency>
First you need to build a service for socket communication and heartbeat tasks before you use it.(在使用之前建一个服务,用于socket通讯, 在服务中可以建立自己的心跳任务)
startService(new Intent(this, MySocketService.class));
Socketer.getInstance(getApplicationContext()).bindServerConnect("123.57.56.201", 20083) //配置socket地址和端口
.setTimeout(15).setEncode("UTF_8") //Configure Timeout and encoding,Timeout unit is seconds配置超时时间与编码
.setReceiveType(ReceiveType.SEPARATION_SIGN) //Configuring the Receive Type配置接收形式以分隔符接收
.setEndCharSequence("\r\n") //"\r\n" is End for split 配置结束符
.setSendMaxByteLength(1500).start(); //Send Max bytes配置一次性最多发送的消息字节数
或者or:
Socketer.getInstance(getApplicationContext()).bindServerConnect("123.57.56.201", 20083)
.setTimeout(15).setEncode("UTF_8")
.setReceiveType(ReceiveType.FIXED_LENGTH) //Configuring the Receive Type配置接收形式以分隔符接收
.setMsgLength(2048) //Fixed length receive 配置固定长度大小接收
.setSendMaxByteLength(1500).start(); //配置一次性最多发送的消息字节数
If the service has unsolicited information to you, you need to register a broadcast, like this:(如果服务有主推通知消息,你需要注册以下广播)
//Set parse to Auto
Socketer.getInstance(MainActivity.this).setParseMode(ParseMode.AUTO_PARSE);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BroadCastType.SERVER_NOTICE);
MessageReceiver dataReceiver = new MessageReceiver();
registerReceiver(dataReceiver, intentFilter);
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BroadCastType.SERVER_NOTICE)) {
String dataStr = intent.getStringExtra(BroadCastType.SERVER_NOTICE_DATA);
Log.i(TAG, "Data given to me by the server:" + dataStr);
}
}
Socketer.getInstance(MainActivity.this).sendStrData(reDataStr, "\"seq\":100", new ResponseListener() {
@Override
public void onSuccess(final String data) {
Log.i("Test server data", "callback data:" + data);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvResponse.setText(data);
}
});
}
@Override
public void onFail(int failCode) {
Log.e("Test server data", "callback error:" + failCode);
}
});
其中参数1代表是请求的数据,参数2代表是返回数据中的唯一标识,可以是请求ID、token值或者能标识唯一性的字符串
Where parameter 1 represents the requested data, parameter 2 represents a unique identity in the returned data, either a request ID, a token value, or a string that uniquely identifies the uniqueness.
//Set parse to Manual
Socketer.getInstance(MainActivity.this).setParseMode(ParseMode.MANUALLY_PARSE);
Socketer.getInstance(MainActivity.this).setOnReceiveListener(new OnReceiveListener() {
@Override
public void onConnected(Socketer socketer) {
}
@Override
public void onDisconnected(Socketer socketer) {
}
@Override
public void onResponse(final String data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//response data
tvResponse.setTextColor(getResources().getColor(R.color.blue));
tvResponse.setText(data);
//... your parse ...
}
});
}
});
Socketer.getInstance(MainActivity.this).sendStrData(reDataStr); //request
//If the configuration of another server is different, first configure the information
//如果另一服务器配置不一样请先配置信息
//Socketer.getInstance(getApplicationContext()).setEncode("UTF_8");
//Socketer.getInstance(getApplicationContext()).setReceiveType(ReceiveType.SEPARATION_SIGN);
//Socketer.getInstance(getApplicationContext()).setTimeout(15); ...
Socketer.getInstance(getApplicationContext()).reConnectSever(ip, port);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BroadCastType.NETWORK_CONNECT_STATE);
ConnectReceiver dataReceiver = new ConnectReceiver();
registerReceiver(dataReceiver, intentFilter);
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BroadCastType.SERVER_NOTICE)) {
boolean isConnected = intent.getBooleanExtra(BroadCastType.IS_CONNECTED, false);
if(isConnected){
Log.i(TAG, "socket connected");
}else{
Log.e(TAG, "socket disconnect");
}
}
}
For bugs, feature requests, and discussion please use GitHub Issues