(聊天)服务器和客户端之间进行通信

发布于 2024-10-17 09:42:17 字数 543 浏览 0 评论 0原文

只是为了澄清某些问题。

假设我正在制作一个聊天应用程序。大多数在线教程都是基本的服务器客户端程序,直接以字符串形式发送聊天消息。

如果有人上线或离线怎么办?客户端如何通知服务器此类更改?我想出的是使用标志 {online}User、{offline}user、{privatechat}blabla。

如果有人知道您的代码,并允许他们通过继续发送 {online}blabla 进行破坏,该怎么办? 这可行,但也有一些我能想到的缺陷。正确或更好的方法是什么?

有人能指出我正确的方向吗?谢谢。

或者另一个例子,在游戏中。为了告诉单元向右移动,它是否向服务器发送一个字符串{unit}{move right}?沿着这些思路。


我有点了解如何制作聊天服务器的逻辑。如果我只是在文本框中添加“{chat}”前缀。只要我读到这个命令“{chat}”,我就会忽略出现的任何命令。

在 RTS 中怎么样(不是我要制作一个,只是好奇),你的意思是实际上有 50 多个字符串告诉单位如何移动、攻击、受到伤害、行动等?所有这些命令都会在一个线程上完成吗?或者多线程?

just to clarify certain questions.

Let's say I'm making a chat application. Most of the online tutorials are basic server-client programs that send the chat message directly as strings.

So what if there is someone that came online, or offline. How would the client notify the server of such changes? What I came up with is to use flags {online}User, {offline}user, {privatechat}blabla.

What if someone knew how your code and that would allow them to sabotage by keep sending {online}blabla.
This would work, but has some flaws that I could think of as well. What would be the correct or better way to do this?

Can someone point me in the right direction? Thanks.

Or another example, in games. To tell the unit to move right, does it send a string back to the server {unit}{move right}? Something along those lines.


I kinda got the logic on how to make the chat server. If I just prefix a "{chat}" to the textbox. As long as I read this command "{chat}" I'll just ignore whichever commands which comes along.

How about in an RTS (not that I'm gonna make one, just curious), you mean there's literally 50 over strings telling how units move, attack, take damage, actions etc? Will all these commands be done on one thread? or multi-threaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-10-24 09:42:18

那么你必须实现会话处理并发送 sessionToken 以及你在游戏中移动单位的命令。因此服务器将能够检查指向的用户是否有权订购指向的单元等。与聊天相同。

在我的聊天中,每个客户端每两分钟向服务器发送一些 PING 消息。因此,如果5分钟过去了,还没有收到PING,服务器就会将该用户视为[离线]。

如果您担心欺骗用户对您的客户端进行反向工程,并可能通过作弊给系统带来严重麻烦,您必须做两件事:

  1. 让服务器检查是否给定
    用户订单有效,
  2. 实施机器人检测。检查是否
    用户对某些进行某些操作
    恒定的时间间隔或者如果用户
    使用一些有限数量的单词
    聊天等。

我希望这篇文章至少能给你一些启发。

服务器逻辑的示例如下:

[WebMethod]
    string LoginUser(string login, string pwd)
        {
            if( dal.IsCorrectUser(login,pwd) )
            {
                string token = dal.MakeSession(login);
                return string;
            }

            return "-1";
        }

[WebMethod]
    bool UserOrder(string token, string order)
        {
            if( !dal.SessionExist(token) )
            {
                return false;
            }

            int userId = dal.GetUserBySession(token);
            if(! dal.ValidOrderForUser(userId,order) )
            {
                dal.RegisterWrongOrder(userid,order); // For cheaters-detecting purposes
                return false;
            }

            return dal.ExecuteUserOrder(userId, order);
        }

事实上,您可以将字符串或任何可序列化的对象作为用户订单发送。

[Serializable]
struct UserOrder
{
  int unitId;
  int Order;
}

所有示例均适用于 c#。只是为了证明逻辑。

Well you have to implement session-handling and send sessionToken with your order to move a unit in game. So server will be able to check whether pointed user have rights to order the pointed unit etc. Same things with chats.

In my chat, every client sends some PING message to server every two minutes. So if 5 minutes passed and no PING is received, server counts the user as [offline].

If you are afraid of cheating users who reverse engineer your client and can make serious troubles to the system with cheats, you have to do two things:

  1. make server to check if given
    user order is valid
  2. implement bot-detection. check if
    user makes some actions with some
    constant time interval or if user
    uses some limited amount of words in
    chat etc.

I hope this post at least gives you some point.

The example of server logic is following:

[WebMethod]
    string LoginUser(string login, string pwd)
        {
            if( dal.IsCorrectUser(login,pwd) )
            {
                string token = dal.MakeSession(login);
                return string;
            }

            return "-1";
        }

[WebMethod]
    bool UserOrder(string token, string order)
        {
            if( !dal.SessionExist(token) )
            {
                return false;
            }

            int userId = dal.GetUserBySession(token);
            if(! dal.ValidOrderForUser(userId,order) )
            {
                dal.RegisterWrongOrder(userid,order); // For cheaters-detecting purposes
                return false;
            }

            return dal.ExecuteUserOrder(userId, order);
        }

In fact you can send string or any serializable objects as an user-order.

[Serializable]
struct UserOrder
{
  int unitId;
  int Order;
}

All samples are for c#. Just to demonstrate logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文