Android Socket实现多个客户端即时通信聊天
public class ChatRoomActivity extends Activity implements View.OnClickListener{
private Socket socket = null;
private EditText edit;
private Button send,disconnect;
private RecyclerView msg_recyclerView;
private MsgAdapter adapter;
private List
private String name = "";
private static final int UPDATE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
Intent intent = getIntent();
name = intent.getStringExtra("name");
Log.d("孔昊",name);
//从服务器读数进程
new Thread(new Runnable() {
private String msg_get="";
@Override
public void run() {
try {
socket = new Socket();
socket.connect(new InetSocketAddress("公网IP", 30001), 5000);
//input
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((msg_get=br.readLine())!=null){
Message message = new Message();
Bundle bundle = new Bundle();
Log.d("孔昊",msg_get);
bundle.putString("msg",msg_get);
message.setData(bundle);
message.what = UPDATE;
handler.sendMessage(message);
}
}catch (IOException e){
e.printStackTrace();
}
}
}).start();
edit = (EditText) findViewById(R.id.edit);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(this);
disconnect = (Button) findViewById(R.id.disconnect);
disconnect.setOnClickListener(this);
msg_recyclerView = (RecyclerView) findViewById(R.id.msg_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
msg_recyclerView.setLayoutManager(linearLayoutManager);
adapter = new MsgAdapter(msgs);
msg_recyclerView.setAdapter(adapter);
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
switch (msg.what){
//更新消息列表
case UPDATE:{
Bundle bundle = msg.getData();
String msg_get = bundle.getString("msg");
Msg msg1 = new Msg(msg_get);
msgs.add(msg1);
adapter.notifyDataSetChanged();
}
break;
}
}
};
//向服务器发数据进程
class SocketThread extends Thread{
private String msg;
//Socket socket;
public SocketThread(String m){
msg = m;
}
@Override
public void run(){
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(msg);
bw.newLine();
bw.flush();
}catch (IOException e){
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.send:{
String s = name +":"+ edit.getText().toString();
edit.setText("");
new Thread(new SocketThread(s)).start();
}
break;
case R.id.disconnect:{
try {
//socket.shutdownOutput();
//socket.shutdownInput();
//socket.close();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("-用户退出-");
bw.newLine();
bw.flush();
//发送message,更新UI
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg","你已经退出聊天!");
message.setData(bundle);
message.what = UPDATE;
handler.sendMessage(message);
}catch (IOException e){
//Log.d("孔昊","断开连接");
e.printStackTrace();
}
}
break;
}
}
//back事件
@Override
public void onBackPressed(){
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("-用户退出-");
bw.newLine();
bw.flush();
}catch (IOException e){
e.printStackTrace();
}
super.onBackPressed();
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播
相关文章
- bt游戏盒子 for Android V5.0.2823 安卓手机版
- Aa方萌(字体美化) for Android v1.0 安卓版
- Android应用程序保持后台唤醒(使用WakeLock实现)
- Android Studio怎么用?Android Studio使用教程图文详解
- Android Https证书过期的两种解决方案
- Android 5.0新特性有哪些?安卓5.0十大新特性汇总详解
- Android BottomNavigationView底部导航效果
- 3D宝软手机桌面 for android v4.3.6 安卓版
- Android网络访问之Retrofit使用教程
- 101课糖(在线教育) for Android v1.8.4 安卓版