Git Product home page Git Product logo

pesocket's Introduction

PESocket

基于C#语言实现的高效便捷网络库。支持集成到Unity当中使用。

不用过多了解网络通信内部原理,只需几行简单的代码,便能简捷快速开发基于C#语言的服务器和客户端,实现网络通信。

技术支持QQ:1785275942

使用示意:

1.创建Socket服务器

PESocket<ClientSession, NetMsg> client = new PESocket<ClientSession, NetMsg>();
client.StartAsServer("127.0.0.1", 17666);

2.创建Socket客户端

PESocket<ClientSession, NetMsg> client = new PESocket<ClientSession, NetMsg>();
client.StartAsClient("127.0.0.1", 17666);

3.网络消息定义

网络消息需要继承自PEMsg类,并打上[Serializable]标签,便于使用C#语言的序列化功能。消息体支持多层嵌套。

[Serializable]
public class NetMsg : PEMsg {
    public int id;
    public string name;
    public int coin;
}

4.发送网络消息

使用ClientSession/ServerSession类中的SendMsg(T msg)函数以及重载函数SendMsg(byte[] data)可以分别发送打包好的网络消息以及完成序列化二进制网络消息。

NetMsg msg = new NetMsg {
    id = 10086,
    name = "Plane",
    coin = 99999
};

this.SendMsg(msg);

5.接收网络消息

在自定义的ClientSession/ServerSession类中重写OnReciveMsg(T msg)可以接收网络消息。

protected override void OnReciveMsg(NetMsg msg) {
    base.OnReciveMsg(msg);

    //TODO 增加处理网络消息的业务逻辑
    PETool.LogMsg("Msg_id:" + msg.id);
    PETool.LogMsg("Msg_name:" + msg.name);
    PETool.LogMsg("Msg_coin:" + msg.coin);
}

6.第三方日志工具接口

通过SetLog(bool log = true, Action<string, int> logCB = null)接口,可以传入第三方的日志显示工具。(下面以Unity为例,实现在Unity编辑器控制台中输出日志信息)

skt.SetLog(true, (string msg, int lv) => {
    switch (lv) {
        case 0:
            msg = "Log:" + msg;
            Debug.Log(msg);
            break;
        case 1:
            msg = "Warn:" + msg;
            Debug.LogWarning(msg);
            break;
        case 2:
            msg = "Error:" + msg;
            Debug.LogError(msg);
            break;
        case 3:
            msg = "Info:" + msg;
            Debug.Log(msg);
            break;
    }
});

pesocket's People

Contributors

planezhong 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.