Giter VIP home page Giter VIP logo

nets's Introduction

license issues issues contributors
watchers forks stars

NETS 简介

一个追求轻量、性能、实用、可快速上手的网络框架。采用工作池模式,已实现协程复用并且可根据并发数量自动扩容协程池。建立连接只需占用3个协程(1个读协程、1个写协程、1个协程池内的工作协程)

使用面向接口编程和组合设计模式,最大程度提高系统的灵活性、可维护性和可扩展性

TODO : 连接断开时,需要等待任务队列全部执行完毕之后再执行onClose / 丢弃等待执行的任务

现已支持:

  • 服务:
    • TCP
    • WebSocket(s)
    • UDP / KCP (🚧进行中)
  • 协议:
    • Protocol Buffer
    • JSON
  • 功能:
    • 设置连接建立时的前置
    • 设置连接断开时的后置
    • 绑定消息属性
    • 消息处理中间件
    • 自定义编码/解码器
    • 消息业务panic阻断
    • 停服时优雅关闭所有连接
    • 分组广播
    • 全服广播
    • 广播历史记录 (🚧进行中)

future:
完善消息广播功能,✅支持创建广播组、✅加入广播组、✅退出广播组、❌广播组解散 (标记不可用,记录保留)

架构图

架构图

使用说明

=> 环境配置

Golang >= 1.18

=> 快速上手

  • 一个简单的例子
// 启动TCP服务
serverTCP := network.NewServerTCP(nil)
serverTCP.Listen()

// 启动WebSocket服务
serverWS := network.NewServerWS(nil)
serverWS.Listen()

// 阻塞主进程
network.ServerWaitFlag.Wait()
  • 连接管理器 ( iface.IConnManager ) 的应用
    network.GetInstanceConnManager() 为单例模式,保持全局唯一
connManager := network.GetInstanceConnManager()

// 设置连接建立时的处理
connManager.OnConnOpen(func(conn iface.IConnection) {
    // do something ...
})

// 设置连接断开时的处理
connManager.OnConnClose(func(conn iface.IConnection) {
    // do something ...
})
  • 消息处理器 ( iface.IMsgHandler ) 的应用
msgHandler := network.GetInstanceMsgHandler()

// 添加一个路由
msgHandler.AddRouter(int32(pb.MSgID_PlayerLogin_Req), func() proto.Message { return &pb.PlayerLoginRequest{} }, func(con iface.IConnection, message proto.Message) {
    // do something ...
})

// 自定义消息过滤器。返回 true 时可正常执行,返回 false 则不会执行路由方法
msgHandler.SetFilter(func(request iface.IRequest, msgData proto.Message) bool {
    // do something ...
    return true
})

// 自定义panic捕获。保障业务逻辑不会导致服务整体崩溃
msgHandler.SetErrCapture(func(request iface.IRequest, r any) {
    // do something ...
})
  • 广播管理器 ( iface.IBroadcastManager ) 的应用
	broadcastManager := network.GetInstanceBroadcastManager()

=> Issues

致谢

许可证

⚖️Apache-2.0 license

nets's People

Contributors

451008604 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.