
就是一个 java 的异步网络通信库。上手简单。
https://github.com/luohaha/LightComm4J
Maven
<dependency> <groupId>com.github.luohaha</groupId> <artifactId>LightComm4J</artifactId> <version>0.0.4-SNAPSHOT</version> </dependency> Download jar
用 lightcomm4j 写一个简单的聊天室吧!
public class ChatRoomServer { public static void main(String[] args) { ServerParam param = new ServerParam("localhost", 8888); Set<Conn> cOnns= new HashSet<>(); param.setBacklog(128); // 注册 accept 事件回调 param.setOnAccept(conn -> { try { String m = conn.getRemoteAddress().toString() + " " + "is online!"; conns.add(conn); conns.forEach(c -> { try { c.write(m.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); // 注册 read 事件回调 param.setOnRead((conn, msg) -> { try { String m = conn.getRemoteAddress().toString() + " : " + new String(msg); conns.forEach(c -> { try { c.write(m.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); // 注册 close 事件回调 param.setOnClose(conn -> { try { conns.remove(conn); String m = conn.getRemoteAddress().toString() + " " + "is offline!"; conns.forEach(c -> { try { c.write(m.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); // 注册异常事件回调 param.setOnReadError((conn, err) -> { System.out.println(err.getMessage()); }); param.setOnWriteError((conn, err) -> { System.out.println(err.getMessage()); }); param.setOnAcceptError(err -> { System.out.println(err.getMessage()); }); LightCommServer server = new LightCommServer(param, 4); try { server.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class ChatRoomClient { public static void main(String[] args) { ClientParam param = new ClientParam(); // 注册 connect 事件回调 param.setOnConnection(conn -> { new Thread(() -> { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String msg = scanner.nextLine(); try { conn.write(msg.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); }); // 注册 read 事件回调 param.setOnRead((conn, msg) -> { System.out.println("[chatroom] " + new String(msg)); }); // 注册 close 事件回调 param.setOnClose(conn -> { System.out.println("[chatroom] " + "chatroom close!"); }); try { LightCommClient client = new LightCommClient(4); client.connect("localhost", 8888, param); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 1 WhoMercy 2017-05-07 22:04:58 +08:00 via Android 战略性 mark java8 让代码简化了不少,不过看起来真不大习惯 |
2 letitbesqzr 2017-05-07 22:11:05 +08:00 @WhoMercy 现在感觉不敢直视各种循环非 java8 的代码了 |
4 luohaha OP @letitbesqzr 哈哈 |
5 yidinghe 2017-05-11 21:10:23 +08:00 看了一下代码,问题有一些: * 首先是没有使用任何日志框架,输出全部都是用 `System.out`; * 其次对异常的处理显得比较随意,没有整体规划,一些空 catch 块没有注释说明; * 再就是到处都遗留了 Eclipse 缺省的 TODO 行,这是留着干嘛呢; * 还有正式代码中包含了测试用的 main() 方法,最好还是改成单元测试。 还有举个例子: ``` // IoWorker.java if (param == null) System.out.println(">>>>>>>>>>>>>>>"); if (param.getOnRead() != null) { ops |= SelectionKey.OP_READ; } ``` 第一行判断 param 是否为 null。如果这个条件为 true,是不是就应该直接 return 了,至少也应该避免执行到第三行,因为这里此时一定会抛出空指针异常。 总之我觉得这个项目显然在楼主自己这里还缺乏足够的实际应用。 |
6 luohaha OP @yidinghe 多谢建议啊,确实啊,这个项目只是刚开始还有很多细节和具体的工作还没有做啊,比如日志和异常处理这两块。。再次感谢您的意见! |
7 woshixiaohao1982 2017-05-14 20:24:33 +08:00 回调地狱.. |
8 woshixiaohao1982 2017-05-14 20:28:08 +08:00 说实话,我个人还是倾向于 用代码来维护状态,,事件回调 真的很蛋疼 |