Network structure for online programming game with webSockets

时间:2015-10-30 23:42:02

标签: websocket webserver 2d-games

Problem I'm making a game where you would provide a piece of code to represent the agent program of an Intelligent Agent (think Robocode and the like), but browser-based. Being an AI/ML guy for the most part, my knowledge of web development was/is pretty lacking, so I'm having a bit of a trouble implementing the whole architecture. Basically, after the upload of text (code), naturally part of the client-side, the backend would be responsible for running the core logics and returning JSON data that would be parsed and used by the client mainly for the drawing part. There isn't really a need for multiplayer support right now. If I model after Robocode's execution loop, I would need a separate process for each battle that then assigns different agents (user-made or not) to different threads and gives them some execution time for each loop, generating new information to be given to the agents as well as data for drawing the whole scene. I've tried to think of a good way to structure the multiple clients, servers/web servers/processes [...], and came to multiple possible solutions. Favored solution (as of right now) Clients communicate with a Node.js server that works kinda like an interface (think websocketd) for unique processes running on the same (server) machine, keeping track of client and process via ID and forwarding the data (via webSockets) accordingly. So an example scenario would be: Client C1 requests new battle to server S and sends code (not necessarily a single step, I know); S handles the code (e.g. compiling), executes new battle and starts a connection with it's process P1 (named pipes/FIFO?); P1 generates JSON, sends to S; S sees P1 is "connected" to C1, sends data to C1 (steps 3 and 4 will be repeated as long as the battle is active); Client C2 requests new battle; Previous steps repeated; C2 is assigned to new process P2; Client C3 requests "watching" battle under P1 (using a unique URL or a token); S finds P1's ID, compares to the received one and binds P1 to C3; This way, the Server forwards received data from forked processes to all clients connected to each specific Battle. Questions Regarding this approach: Is it simple enough? Are there easier or even more elegant ways of doing it? Could scalability be a problem? Is it secure enough (the whole compiling and running code — likely C++ — on the server)? Is it fast enough (this one worries me the most for now)? It seems a bit counter intuitive to have a single server dealing with the entire traffic, but as far as I know, if I'd assign all these processes to a separate web server, I would need different ports for each of them, which seems even worse.

1 个答案:

答案 0 :(得分:0)

由于这是一个基于理论和意见的问题......我可以随意向不同的方向投球。我可能会在我考虑事情或阅读评论时编辑答案。

  1. 每场战斗的过程?

    听起来很贵。此外,还存在消息在进程之间来回传递的问题......也许能够在机器之间发送消息并完全分离关注点。

    我们可以让它们独立运行,让它们崩溃并重新启动并做任何他们想要的事情而不会造成任何其他战斗或我们的服务器受到任何伤害。

  2. 的Javascript?为什么只有一种语言?

    我会考虑利用面向对象的方法或语言 - 至少在战斗中,如果不是服务器的话。

    如果我们要分离代码,我们可以使用不同的语言。否则我可能会选择Ruby,因为它对我来说很容易,但也许我错了并深入研究Javascript的原型会做什么。

  3. 哦......外国代码 - 清理工作正常。

    外国代码有多安全?它应该是一种本地化的语言,它承诺使用现有语言解释器的安全性,这可能会让代码弄乱它真正不应该的东西......

    我可能会为战斗编写我自己的“伪语言”......或者(如果它对我和我来说是一个非常本地的项目)使用Ruby,其中一个是消毒宝石。

  4. 战斗和网络服务可能无法以相同的速度扩展。

    在我看来,处理邮件 - client->server->battlebattle->server->client - 相当容易。然而,处理战斗似乎更加耗费资源。

    我说服自己,关注点的分离几乎是不可避免的。

    拥有服务器后端和不同的战斗后端将允许您更快地扩展战斗处理程序,并且在没有任何需要之前不会浪费资源来扩展Web服务器。

  5. 网络断开连接。

    假设我们允许玩家在他们的代理人在战场上“战斗”时离线......当我们需要向刚刚重新连接到服务器X的用户“Mitchel”发送消息时他会离开的战斗会发生什么?在服务器Y上肆虐?

    分离问题意味着我们从一开始就拥有一个可随时扩展的通信系统,允许我们的用户连接到不同的终端并仍然可以收到他们的消息。

  6. 总结一下,我会将此视为我的工作流程:

    • Http工作流程:

      • 客户 - > Web服务器:请求具有标识符和可选战斗数据的代理(战斗数据用于创建代理,省略战斗数据将用于将请求限制为现有代理(如果存在))。

        此步骤可能会根据客户端身份验证/凭据(即会话数据/ Cookie标识符或登录过程)自动执行。

        • 如果请求中存在战斗数据(请求制作):

          Web服务器 - >战斗实例:创建代理(如果不存在)。

        • 如果请求中缺少战斗数据:

          Web服务器 - >战斗数据库,以检查代理是否存在。

      • Web服务器 - >客户:关于代理的响应(存在/创建与无)

        • 如果存在或创建了代理,请在设置连接凭据(会话数据,唯一cookie标识符或要附加到Websocket请求查询的一次性唯一令牌)后启动Websocket连接。

        • 如果Agent不存在,请将客户端转发到Web表单以填写代理代码,战斗类型等数据。

    • Websocket“工作流程”(非线性):

      • 代理有数据:代理消息 - > (战斗通信经理) - > Web服务器 - >客户端

        可以在其中放置Redis或类似的数据库,以允许在用户离线时堆叠消息,并允许多个战斗实例和多个Web服务器实例。

      • 客户端更新代理:客户端消息 - > (战斗通信经理) - > Web服务器 - >剂