效果图如下:
服务器端代码:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MinaPushServer {
private NioSocketAcceptor acceptor;
private JTextField jTextField;
public static void main(String[] args) {
MinaPushServer minaPushServer = new MinaPushServer();
minaPushServer.initServer();
// minaPushServer.initConsole();
minaPushServer.initJavaSwing();
}
public void initServer() {
acceptor = new NioSocketAcceptor();
// 添加我们的过滤器
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 添加处理器--目的:监听回话
acceptor.setHandler(new ServerIOHandler());
try {
// 绑定端口
acceptor.bind(new InetSocketAddress(9999));
} catch (IOException e) {
e.printStackTrace();
}
}
// 2、Java Swing编程创建我们的窗体程序,给我们的客户端推送消息
public void initJavaSwing() {
// 创建一个窗体
JFrame jFrame = new JFrame("推送服务器");
// 设置窗体的大小
jFrame.setSize(320, 200);
// 设置平台位置
jFrame.setLocationByPlatform(true);
// 设置我们的窗体关闭的时候,应用程序结束
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加控件
Container contentPane = jFrame.getContentPane();
// 指定布局
contentPane.setLayout(new FlowLayout());
jTextField = new JTextField("这是一个关于推送的小测试,测试");
jTextField.setBounds(0, 0, 200, 20);
contentPane.add(jTextField);
// 创建按钮
JButton jButton = new JButton("推送消息");
jButton.addMouseListener(new ServerMouseListener());
JButton clearButton = new JButton("清空消息");
clearButton.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent arg0) {
jTextField.setText("");
}
});
jFrame.add(jButton);
jFrame.add(clearButton);
jFrame.setVisible(true);
}
class ServerMouseListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
// 当我们点击按钮的时候,推送一条消息
String text = jTextField.getText();
// 获取所有的回话
Map sessions = acceptor.getManagedSessions();
Set> entrySet = sessions.entrySet();
for (Entry entry : entrySet) {
// 发送推送消息
IoSession ioSession = entry.getValue();
ioSession.write(text);
}
}
}
}
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class ServerIOHandler extends IoHandlerAdapter {
private static final String TAG = ServerIOHandler.class.getSimpleName();
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println(TAG + "Server--回话创建--调用了sessionCreated方法");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println(TAG + "Server--回话打开--调用了sessionOpened方法");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println(TAG + "Server--回话关闭--调用了sessionOpened方法");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
System.out.println(TAG + "Server--回话闲置--调用了sessionOpened方法");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
System.out.println(TAG + "Server--抓取异常--调用了exceptionCaught方法");
}
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
System.out.println(TAG + "Server--接收消息--调用了messageReceived方法" + " message:" + message);
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println(TAG + "Server--发送消息--调用了messageSent方法");
}
@Override
public void inputClosed(IoSession session) throws Exception {
System.out.println(TAG + "Server--关闭输入流--调用了inputClosed方法");
}
}
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class MinaPushClient {
public static void main(String[] args) {
// 在我们的客户端创建连接器
NioSocketConnector connector = new NioSocketConnector();
// 添加过滤器
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 添加处理器
connector.setHandler(new ClientIOHandler());
// 绑定服务器
connector.connect(new InetSocketAddress("127.0.0.1", 9999));
}
}
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class ClientIOHandler extends IoHandlerAdapter {
private static final String TAG = ClientIOHandler.class.getSimpleName();
private JTextField jTextField;
public ClientIOHandler(){
System.out.println("=== init ===");
initJavaSwing();
}
public void initJavaSwing() {
// 创建一个窗体
JFrame jFrame = new JFrame("推送接收端");
// 设置窗体的大小
jFrame.setSize(320, 200);
// 设置平台位置
jFrame.setLocationByPlatform(true);
// 设置我们的窗体关闭的时候,应用程序结束
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加控件
Container contentPane = jFrame.getContentPane();
// 指定布局
contentPane.setLayout(new FlowLayout());
jTextField = new JTextField("等待接收消息 ");
jTextField.setBounds(0, 0, 200, 20);
contentPane.add(jTextField);
jFrame.setVisible(true);
}
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println(TAG + "回话创建--调用了sessionCreated方法");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println(TAG + "回话打开--调用了sessionOpened方法");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println(TAG + "回话关闭--调用了sessionOpened方法");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
System.out.println(TAG + "回话闲置--调用了sessionOpened方法");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
System.out.println(TAG + "抓取异常--调用了exceptionCaught方法");
}
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
System.out.println(TAG + "接收消息--调用了messageReceived方法" + " message:" + message);
jTextField.setText(message.toString());
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println(TAG + "发送消息--调用了messageSent方法");
}
@Override
public void inputClosed(IoSession session) throws Exception {
System.out.println(TAG + "关闭输入流--调用了inputClosed方法");
}
}