Skip to content
ScutGame edited this page Jul 6, 2015 · 5 revisions

此章节介绍如何创建Socket的服务端项目

项目搭建参考如何搭建服务端项目工程

  • 提供GameSocketHost类处理可以同时接收Socket和Http请求的处理,只需要在MainClass脚本类继承GameSocketHost类,即可实现短连接的Http通讯方式; 示例代码如下:

public class MainClass : GameSocketHost
{
    protected override void OnConnectCompleted(object sender, ConnectionEventArgs e)
    {
        Console.WriteLine("客户端IP:[{0}]已与服务器连接成功", e.Socket.RemoteEndPoint);
        base.OnConnectCompleted(sender, e);
    }

    protected override void OnDisconnected(GameSession session)
    {
        Console.WriteLine("客户端UserId:[{0}]已与服务器断开", session.RemoteAddress);
        base.OnDisconnected(session);
    }

    protected override void OnHeartbeat(GameSession session)
    {
        Console.WriteLine("{0}>>客户端的心跳包: {1} session count {2}", DateTime.Now.ToString("HH:mm:ss"), session.RemoteAddress, GameSession.Count);
        base.OnHeartbeat(session);
    }

    protected override void OnReceivedBefore(ConnectionEventArgs e)
    {
        //请求的源数据
        Console.WriteLine(Encoding.UTF8.GetString(e.Data));
        base.OnReceivedBefore(e);
    }

    protected override void OnRequested(ActionGetter actionGetter, BaseGameResponse response)
    {
        //这里处理玩家请求的业务
        Console.WriteLine(actionGetter.ToParamString());
        base.OnRequested(actionGetter, response);
    }

    protected override void OnStartAffer()
    {
        //这里处理服务启动后的业务
    }

    protected override void OnServiceStop()
    {
        //这里处理服务停止时的业务
        GameEnvironment.Stop();
    }
}

  • 游戏配置

在项目中的GameServer.exe.config文件中增加以下,同时使用Socket和Http配置。

<add key="Game.Port" value="9001" />

参考服务器推送消息

Clone this wiki locally