BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #56208同步于 2017/5/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

宣传一下自己开源的轻量java异步网络通信库LightComm4J

poiuasd
2017/5/7镜像同步19 回复
地址 : https://github.com/luohaha/LightComm4J 特点 比起现在市面上的异步通信库,最大的特点就是上手简单吧~~ 如何使用 看一个用它实现聊天室的例子,就大概知道怎么用了 聊天室服务端 public class ChatRoomServer { public static void main(String[] args) { ServerParam param = new ServerParam("localhost", 8888); Set 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(); // 注册connection事件回调 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(); } } } 更多的介绍 github地址在 :https://github.com/luohaha/LightComm4J,欢迎star..
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
jiarong机器人#1 · 2017/5/7
帮顶
poiuasd机器人#2 · 2017/5/7
你能看到我发的这篇文章吗?为啥我看到的是502错误。。。 【 在 jiarong 的大作中提到: 】 : 帮顶
Footprints机器人#3 · 2017/5/7
看标题就知道是你 发自「贵邮」
hx0502001机器人#4 · 2017/5/7
赞!~ 通过『我邮2.0』发布
ml3615556机器人#5 · 2017/5/7
设计能力好强!
poiuasd机器人#6 · 2017/5/7
你是? 【 在 Footprints 的大作中提到: 】 : 看标题就知道是你 : 发自「贵邮」
poiuasd机器人#7 · 2017/5/7
多谢帮顶~~ 【 在 hx0502001 的大作中提到: 】 : 赞!~ : 通过『我邮2.0』发布
poiuasd机器人#8 · 2017/5/7
多谢多谢,主要是觉得netty那些网络库太复杂了,想做个简单的,一看就会用的。。[ema3] 【 在 ml3615556 的大作中提到: 】 : 设计能力好强!
sdhhx机器人#9 · 2017/5/7