Skip to content

jack81722/FT-SocketServer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FT SocketServer 0.0.3 indev

Socket Server for .NET Framework 4.5, Mono, .NET Core 2.0, .NET Standard 2.0 with Unity Client Libary.
We recommend use docker or k8s deploy server.

Build

Features

  • UDP, RUDP(LitNetLib), TCP(rc1), Websocket(Websocketsharp,rc1) support
  • Ipv6 support
  • Unity support
    • Android
    • iOS
    • WebGL (websocket)
  • Ready for Container!! Nice work with Docker and K8s

Usage samples

  • Change PlayerSetting First!!! File/Build Setting/PlayerSetting
    • PlayerSetting/OtherSettings/Configuration/ScriptRuntimeVersion -> .Net 4.x Equivalent
    • PlayerSetting/OtherSettings/Configuration/ScriptingBackend -> IL2CPP

Client

public class NewBehaviourScript : MonoBehaviour {
    private Connect mConnect;
    private MyCallBackHandler MyCallBackHandler;
    private _System SystemHandler;
    // Use this for initialization
    void Start () {
        //create connection
        mConnect = new Connect("104.199.194.170"/*Server Ip*/, 30100/*port*/, NetworkProtocol.RUDP);
        //create logic object
        MyCallBackHandler = new MyCallBackHandler();    
        //add this logic object to connection object
        mConnect.AddCallBackHandler(MyCallBackHandler.OperatorCode/*if server send packet which code is 20. this obj is going to handler it.*/, MyCallBackHandler);
        //Add default callbackhandler
        SystemHandler = new _System();
        mConnect.AddCallBackHandler(_System.OperatorCode, SystemHandler);
        //establish connection
        SystemHandler.ConnectToServer();
    }
    // Update is called once per frame
    void Update () {
        //call every frame
        mConnect.Service();
        if (Input.anyKeyDown)
            MyCallBackHandler.Send("hellow world!!");
    }
    void OnApplicationQuit()
    {
        //Release connection
        mConnect.Dispose();    
    }
}
public class MyCallBackHandler : CallbackHandler
{
    public const int OperatorCode = 20;
    public void Send(string packet)
    {
        //send packet to server
        gameService.Deliver(MyCallBackHandler.OperatorCode, new Dictionary<byte, object>(){ {0,packet }});
    }
    public override void ServerCallback(Dictionary<byte, object> server_packet)
    {
        //get something from server
        Debug.Log("Msg from server : " + server_packet[0].ToString());     
    }
}

Server

    class Program : SocketServer
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.StartListen(30100, Protocol.RUdp);
            while (true){Thread.Sleep(500);}
        }
        public override ClientNode GetPeer(Core core, IPEndPoint iPEndPoint, SocketServer socketServer)
        {
            Peer player = new Peer(core, iPEndPoint, socketServer);
            return player;
        }
    }
    public class Peer : PeerBase
    {
        public Peer(ISender sender, IPEndPoint iPEndPoint, FTServer.SocketServer socketServer) : base(sender, iPEndPoint, socketServer)
        {}
        public override void OnOperationRequest(IPacket packet)
        {
            if (packet.OperationCode == 20)
            {
                Console.WriteLine("Client tell me : " + packet.Parameters[0].ToString());
                SendEvent(packet.OperationCode, new System.Collections.Generic.Dictionary<byte, object>()
                {
                    {0,"hello client!" }
                });
            }
        }
        public override void OnDisconnect()
        {
            Console.WriteLine("OnDisconnect");
        }
    }

License

FTServer is provided under The MIT License.

About

Socket Server for .NET Family

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%