1、修改原来的连接器
1)在连接时发送信息给服务器
new Thread(new RequestWorker()).start();
2)把验证码放入队列里
public void auth(String auth) {
putRequest(auth);
}
3)代码:
public class Connector {
//ip
private String dstName = "192.168.1.100";
//端口
private int dstPort = 10002;
private Socket mClientSocket;
private ConnectorListener mConnectorListener;
//队列
private ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(8);
/**
* 使用三次握手实现长连接
*/
public void connect() {
try {
if (mClientSocket == null || mClientSocket.isClosed()) {
mClientSocket = new Socket(dstName, dstPort);
}
//发送信息给服务器
new Thread(new RequestWorker()).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream is = mClientSocket.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
final String text = new String(buffer, 0, len);
System.out.println("text : " + text);
if (mConnectorListener != null) {
mConnectorListener.pushData(text);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
public class RequestWorker implements Runnable {
@Override
public void run() {
// 数据通讯
OutputStream os;
try {
os = mClientSocket.getOutputStream();
while (true) {
String content = queue.take();
os.write(content.getBytes());
}
} catch (Exception e) {
throw new RuntimeException("获取数据失败:" + e.getMessage());
}
}
}
/**
* 把验证放入队列
* @param auth
*/
public void auth(String auth) {
putRequest(auth);
}
/**
* 把信息放入队列
* @param content
*/
public void putRequest(String content) {
try {
queue.put(content);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 断开连接
*/
public void disconnect() {
try {
if (mClientSocket != null && !mClientSocket.isClosed()) {
mClientSocket.close();
mClientSocket = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void setConnectorListener(ConnectorListener listener) {
this.mConnectorListener = listener;
}
public interface ConnectorListener {
void pushData(String data);
}
}
2、封装管理类
public class ConnectorManager implements ConnectorListener {
//连接器
private Connector connector;
private ConnectorListener mListener;
private static ConnectorManager instance;
private ConnectorManager() { }
public static ConnectorManager getInstance() {
if (instance == null) {
synchronized (ConnectorManager.class) {
if (instance == null) {
instance = new ConnectorManager();
}
}
}
return instance;
}
/**
* 创建连接发送验证
* @param auth
*/
public void connnect(String auth) {
connector = new Connector();
connector.setConnectorListener(this);
connector.connect();
connector.auth(auth);
}
public void connnect(AuthRequest auth) {
connector = new Connector();
connector.setConnectorListener(this);
connector.connect();
connector.auth(auth.getData());
}
public void putRequest(String request) {
connector.putRequest(request);
}
public void putRequest(Request request) {
connector.putRequest(request.getData());
}
@Override
public void pushData(String data) {
if (mListener != null) {
mListener.pushData(data);
}
}
public void setConnectorListener(ConnectorListener listener) {
this.mListener = listener;
}
public interface ConnectorListener {
void pushData(String data);
}
}
3、服务类:
public class ConnService extends Service implements ConnectorListener {
private ConnectorManager mConnManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* 连接服务
*/
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
mConnManager = ConnectorManager.getInstance();
mConnManager.setConnectorListener(ConnService.this);
mConnManager.connect("#A");
}
}).start();
}
@Override
public void pushData(String data) {
Intent intent = new Intent();
intent.setAction(PushReceiver.ACTION);
intent.putExtra(PushReceiver.DATA, data);
sendBroadcast(intent);
}
}
4、主界面:
public class ClientActivity extends Activity {
private EditText mEtContent;
private ConnectorManager mConnectorManager;
private PushReceiver mPushReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConnectorManager = ConnectorManager.getInstance();
mEtContent = (EditText)findViewById(R.id.et);
//开启服务
startService(new Intent(this, ConnService.class));
initReceiver();
}
private void initReceiver() {
mPushReceiver = new PushReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (PushReceiver.ACTION.equals(intent.getAction())) {
String response = intent.getStringExtra(PushReceiver.DATA);
ToastUtil.show(ClientActivity.this, response);
}
}
};
IntentFilter filter = new IntentFilter(PushReceiver.ACTION);
registerReceiver(mPushReceiver, filter);
}
/**
* 发送信息
* @param view
*/
public void clickMessage(View view){
if (!TextUtils.isEmpty(mEtContent.getText())) {
mConnectorManager.putRequest(mEtContent.getText().toString());
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mPushReceiver);
super.onDestroy();
}
}