简体   繁体   English

在SilverStripe中运行的棘轮Web套接字服务器

[英]Ratchet web socket server running within SilverStripe

I am trying to get a Ratchet WebSocket server running that has access to DataObject s from a SilverStripe instance that can be run as a long-running process from the terminal. 我正在尝试使Ratchet WebSocket服务器运行,该服务器可以从SilverStripe实例访问DataObject ,该实例可以从终端作为长时间运行的进程运行。

My current approach, taking the lead from cli-script.php is to include core/Core.php and model/DB.php and connect to the database using the same database config as is defined in _config.php 我目前从cli-script.php是包括core/Core.phpmodel/DB.php并使用与_config.php定义的相同的数据库配置连接到数据库

Is there a better way of doing this that doesn't involve copying code from cli-script.php ? 有没有一种更好的方法可以执行此操作而不涉及从cli-script.php复制代码? The only though I had was a controller that fires up the server but that sounds wrong too. 尽管我只有一个控制器,它可以启动服务器,但听起来也很错误。

Another thought was to keep them separate and use the SS REST API to interact between them but it would be simpler to interact with the PHP objects instead. 另一个想法是将它们分开并使用SS REST API在它们之间进行交互,但是与PHP对象进行交互会更简单。

In the end, I turned the design on it's head and had all the messages go via the SilverStripe instance (Ajax from the client), which then pushed WebSocket messages out over a REST API. 最后,我改变了设计,让所有消息都通过SilverStripe实例(来自客户端的Ajax)传递,然后该实例通过REST API推送了WebSocket消息。

When looking into hosting for such a process, I stumbled upon Pusher, which I am now using for the real-time aspect of the system. 在寻找托管此类程序的过程时,我偶然发现了Pusher,我现在将其用于系统的实时方面。

In short: 简而言之:

  1. A JS client informs the SilverStripe app of a change using Ajax. JS客户端使用Ajax通知SilverStripe应用更改。
  2. The SilverStripe app persists the change in the database. SilverStripe应用程序将更改保留在数据库中。
  3. The SilverStripe app pushes a message via Pusher using their API / PHP library. SilverStripe应用程序使用其API / PHP库通过Pusher推送消息。
  4. Pusher sends the message over a WebSocket connection to all other connected clients. Pusher通过WebSocket连接将消息发送到所有其他已连接的客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM