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

此章节介绍HelloWorld示例--C#脚本

通过Helloworld程序简单介绍下Scut服务端开发的流程,客户端的这里就不介绍了可以查看客户端的Lua源码。

功能概述

客户端使用Socket协议发送一个请求到服务端,服务端接收到请求后响应一段字符串给客户端,接收客户端接收到后显示出来。 序列图如下:

Hello sequence

开发流程

  • 设计接口协议

通过协议工具平台定义一个与客户端通讯的Action接口协议,Action编号为100(编号规则以3位数开始,低于3位数的Scut预留使用);

增加一个string类型,名称为content的返回参数,请求参数可以不加,如图:

contract action

在右边会自动生成服务端与客户端的通讯接口脚本,可以直接复制到项目中;

  • 创建项目

通过打开VS创建“HelloWorld”项目,详细创建参考如何搭建服务端项目工程;在Script/PyScript/Action的目录下新建立Action100类文件;然后将协议平台生成的服务端协议脚本复制到文件中;修改如下:

#!Action100.py

"""Hello World"""
import ReferenceLib
from action import *
from ZyGames.Framework.Cache.Generic import *


class UrlParam(HttpParam):
    def __init__(self):
        HttpParam.__init__(self)


class ActionResult(DataResult):
    def __init__(self):
        DataResult.__init__(self)
        self._content = ''


def getUrlElement(httpGet, parent):
    urlParam = UrlParam()
    urlParam.Result = True
    return urlParam


def takeAction(urlParam, parent):
    actionResult = ActionResult()
    actionResult._content = 'Hello World for Python!'
    return actionResult


def buildPacket(writer, urlParam, actionResult):
    writer.PushIntoStack(actionResult._content)

    return True

开启使用Python脚本,修改Config配置如下:

#!GameServer.exe.config

<add key="Script_IsDebug" value="True"/>
<add key="Python_Disable" value="False" />

注:Python目录说明 如果需要定义另外Python脚本的业务目录,必须在PyScript目录下,如:PyScript/BLL/目录

  • 启动服务并调试
  1. 服务端需要依赖的服务包括:Redis服务器;在启动服务端之前需要先启动Redis服务器,Redis服务器相当于一个数据库,一个服务端对应一个 Redis实例;如果connectionStrings节点配置为空,可以不需要Sql数据库。

  2. 按F5启动服务端,如果不能启动说明调试配置项没有配对;如果控制台没有打印出“Server has started Successfully”的字串,说明启动失败,详情需要在Nlog日志文件中查看出错信息,文件位置可以查看Nlog.config文件里的配置路径。

  3. 启动客户端,打开Sample的源码仓库Sample\HelloWorld\Client目录下的MainApp.exe,点击“Send”按键,发送请求;如果连接失败,需要检查客户端LoginScene.lua脚本配置的服务器地址是否正确;