ChatSample - ScutGame/Scut GitHub Wiki

此章节介绍Chat聊天室示例--WebSocket & Cocos2d-JS

Chat是介绍如何使用WebSocket通讯协议作为服务端运行的示例,使用Cocos2d-JS作为客户端;可以从Github或OsChina上下载最新Sample。

服务端

使用6.7.9.6版本的Scut类库,聊天功能使用聊天中间件来快速创建服务;玩家的数据只存放在内存中不作持久化, Config中没有配置数据库连接串。

流程步骤

using System;

namespace GameServer.Script.CsScript.JsonProtocol
{
    /// <summary>
    /// Json result data
    /// </summary>
    public class ResultData
    {
        /// <summary>
        /// 
        /// </summary>
        public int MsgId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int ActionId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int ErrorCode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ErrorInfo { get; set; }

        /// <summary>
        /// logic data
        /// </summary>
        public object Data { get; set; }
    }
}

修改WebSocketActionDispatcher.cs的通讯结构


public class WebSocketActionDispatcher : ScutActionDispatcher
{

  public override void ResponseError(BaseGameResponse response, ActionGetter actionGetter, int errorCode, string errorInfo)
  {
    var result = new ResultData()
    {
	MsgId = actionGetter.GetMsgId(),
	ActionId = actionGetter.GetActionId(),
	ErrorCode = errorCode,
	ErrorInfo = errorInfo,
	Data = null
    };
    //实现出错处理下发
    response.BinaryWrite(Encoding.UTF8.GetBytes(MathUtils.ToJson(result)));
  }
}

  • 定义登录接口协议(Action1004),使用自定的身份认证MyLogin类,实现ILogin接口
    #!MyLogin.cs

    public class MyLogin : ILogin
    {
        public MyLogin(string userName)
        {
            PassportID = userName;
        }

        public string GetRegPassport()
        {
            return PassportID;
        }

        public bool CheckLogin()
        {
            var cache = new MemoryCacheStruct<ChatUser>();
            ChatUser chatUser = cache.Find(t => t.UserName == PassportID);
            if (chatUser != null)
            {
                UserID = chatUser.UserId.ToString();
                return true;
            }
            //not user create it.
            chatUser = new ChatUser()
            {
                UserId = (int)RedisConnectionPool.GetNextNo(typeof(ChatUser).FullName),
                UserName = PassportID,
                AccessTime = DateTime.Now
            };
            if (cache.TryAdd(chatUser.UserId.ToString(), chatUser))
            {
                UserID = chatUser.UserId.ToString();
                return true;
            }
            return false;
        }

        public string PassportID { get; private set; }
        public string UserID { get; private set; }
        public int UserType { get; private set; }
        public string Password { get; private set; }
        public string SessionID { get; private set; }
    }

#! Action1004.cs 部分代码

    /// <summary>
    /// login
    /// </summary>
    public class Action1004 : LoginAction
    {
        private string _userName;


        public Action1004(ActionGetter actionGetter)
            : base((short)1004, actionGetter)
        {
	    //使用WebSocket的Text内容格式
            IsWebSocket = true;
            actionGetter.OpCode = OpCode.Text;
        }

        protected override ILogin CreateLogin()
        {
            return new MyLogin(_userName);
        }

        //输出Json内容格式
        protected override string BuildResponsePack()
        {
            ResultData resultData = new ResultData()
            {
                MsgId = actionGetter.GetMsgId(),
                ActionId = actionGetter.GetActionId(),
                ErrorInfo = "",
                Data = new UserData()
                {
                    UserId = Current.UserId,
                    SessionId = Current.SessionId
                }
            };
            return MathUtils.ToJson(resultData);
        }

  • 使用聊天中间件,定义MyChatService类继承ChatService

  • 定义两个接口协议,3001发送消息,3002聊天列表通知

客户端

使用Cocos2d-JS开发客户端,新创建一个"Cocos2d JavaScript Project"项目,选择从现有的资源导入创建项目;如下:

Chat cocos js

注: 项目中"src/scut"目录是官方提供的SDK,在新项目中可以Copy过去使用; runtime目录下有win平台运行的客户端已编译的版本;

运行效果:

Chat cocos

⚠️ **GitHub.com Fallback** ⚠️