使用yanf4j写个简单聊天室
程序员文章站
2022-03-10 23:51:44
...
yanf4j
简介,请看这里
。
这里俺要介绍下用yanf4j写个最简单的聊天室,可以直接telnet上去聊天。请先下载common-logging
和yanf4j-0.40-stable-bin.tar.gz
。
聊天室嘛,肯定要维护一个聊天者聊表:
<!---->List
<
Session
>
sessionList
=
new CopyOnWriteArrayList
<
Session
>
();
用户名让系统产生,用户1、2、3.....,最简单的嘛:
<!---->AtomicInteger userId
=
new AtomicInteger();
private String generateUserName() {
return " user " + userId.incrementAndGet();
}
private String generateUserName() {
return " user " + userId.incrementAndGet();
}
服务端要实现一个处理handler,来接受用户连接并转发聊天消息,嗯,继承HandlerAdapter,覆写相应的方法即可。当有人连上来的时候,覆写onSessionCreated方法:
<!---->public
void
onSessionCreated(Session session) {
sessionList.add(session);
// 给session取名
session.attach(generateUserName());
// 广播某人进来
broadcast(session, session.attachment() + " enter room " );
}
sessionList.add(session);
// 给session取名
session.attach(generateUserName());
// 广播某人进来
broadcast(session, session.attachment() + " enter room " );
}
当收到聊天消息的时候,广播给组内成员:
<!----> @Override
public void onReceive(Session session, String t) {
if (t.equals( " quit " ))
session.close();
else
broadcast(session, " [ " + session.attachment() + " ] say: " + t);
}
private void broadcast(Session s, String msg) {
for (Session session : this .sessionList) {
session.send(msg);
}
}
public void onReceive(Session session, String t) {
if (t.equals( " quit " ))
session.close();
else
broadcast(session, " [ " + session.attachment() + " ] say: " + t);
}
private void broadcast(Session s, String msg) {
for (Session session : this .sessionList) {
session.send(msg);
}
}
广播我们只是简单的遍历sessionList并send而已。完整的ChatServerHandler.java清单如下:
<!---->package
com.google.code.yanf4j.test.example.chat;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.code.yanf4j.nio.Session;
import com.google.code.yanf4j.nio.impl.HandlerAdapter;
public class ChatServerHandler extends HandlerAdapter < String > {
List < Session > sessionList = new CopyOnWriteArrayList < Session > ();
AtomicInteger userId = new AtomicInteger();
@Override
public void onReceive(Session session, String t) {
if (t.equals( " quit " ))
session.close();
else
broadcast(session, " [ " + session.attachment() + " ] say: " + t);
}
@Override
public void onSessionClosed(Session session) {
sessionList.remove(session);
broadcast(session, session.attachment() + " leave room " );
}
private void broadcast(Session s, String msg) {
for (Session session : this .sessionList) {
session.send(msg);
}
}
@Override
public void onSessionCreated(Session session) {
sessionList.add(session);
// 给session取名
session.attach(generateUserName());
// 广播某人进来
broadcast(session, session.attachment() + " enter room " );
}
private String generateUserName() {
return " user " + userId.incrementAndGet();
}
}
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.code.yanf4j.nio.Session;
import com.google.code.yanf4j.nio.impl.HandlerAdapter;
public class ChatServerHandler extends HandlerAdapter < String > {
List < Session > sessionList = new CopyOnWriteArrayList < Session > ();
AtomicInteger userId = new AtomicInteger();
@Override
public void onReceive(Session session, String t) {
if (t.equals( " quit " ))
session.close();
else
broadcast(session, " [ " + session.attachment() + " ] say: " + t);
}
@Override
public void onSessionClosed(Session session) {
sessionList.remove(session);
broadcast(session, session.attachment() + " leave room " );
}
private void broadcast(Session s, String msg) {
for (Session session : this .sessionList) {
session.send(msg);
}
}
@Override
public void onSessionCreated(Session session) {
sessionList.add(session);
// 给session取名
session.attach(generateUserName());
// 广播某人进来
broadcast(session, session.attachment() + " enter room " );
}
private String generateUserName() {
return " user " + userId.incrementAndGet();
}
}
接下来我们要实现聊天服务器了,yanf4j实现TCP Server的核心类是TCPController,假设端口设为8080:
<!---->package
com.google.code.yanf4j.test.example.chat;
import java.io.IOException;
import com.google.code.yanf4j.config.Configuration;
import com.google.code.yanf4j.nio.TCPController;
import com.google.code.yanf4j.nio.impl.StringCodecFactory;
public class ChatServer {
public static void main(String[] args) throws IOException {
TCPController controller = new TCPController( new Configuration(),
new StringCodecFactory());
controller.setHandler( new ChatServerHandler());
controller.setPort( 8080 ); // 端口
controller.start(); // 启动
}
}
import java.io.IOException;
import com.google.code.yanf4j.config.Configuration;
import com.google.code.yanf4j.nio.TCPController;
import com.google.code.yanf4j.nio.impl.StringCodecFactory;
public class ChatServer {
public static void main(String[] args) throws IOException {
TCPController controller = new TCPController( new Configuration(),
new StringCodecFactory());
controller.setHandler( new ChatServerHandler());
controller.setPort( 8080 ); // 端口
controller.start(); // 启动
}
}
顺利完成,启动后,telnet到8080端口,看看效果
推荐阅读
-
使用socket.io实现简单聊天室案例
-
ASP.NET 使用application和session对象写的简单聊天室程序
-
GO使用socket和channel实现简单控制台聊天室
-
win10下Free Pascal使用方法(从安装、消除乱码、编写个简单的hello谈起)
-
通过实现简单聊天室了解websocket的基础使用
-
使用Servlet和JSP实现一个简单的Web聊天室系统
-
Spring Boot 使用WebSocket实现简单页面聊天室
-
使用socket.io实现简单聊天室案例
-
ASP.NET 使用application和session对象写的简单聊天室程序
-
Java使用continue break制作简单聊天室的实例分析