简体   繁体   English

有没有办法通过PHP页面修改node.js代码?

[英]Is there a way to modify node.js code via a PHP page?

So I'm using http://socket.io with node.js and developing a chat to learn more about node.js 因此,我将http://socket.io与node.js结合使用,并进行了聊天以了解有关node.js的更多信息

I use node.js to communicate with MySQL when users open the page. 用户打开页面时,我使用node.js与MySQL进行通信。

But for bandwidth reasons I can't keep using MySQL for everything so I store a few variables in there. 但是由于带宽的原因,我无法继续使用MySQL进行所有操作,因此我在其中存储了一些变量。 For example I don't want the user signed in twice, so I would store the socket ID for all users in JSON. 例如,我不希望用户登录两次,所以我将所有用户的套接字ID存储在JSON中。 If the socket ID already exists for that user (in node.js), then I would disconnect the socket. 如果该用户的套接字ID已经存在(在node.js中),那么我将断开套接字。

This works fine, but let's say I wanted to disconnect a user, how would I go about doing that with PHP? 这可以正常工作,但是假设我想断开用户连接,我将如何使用PHP进行连接?

One option I've considered is maybe I update a table in the database with the required changes and then node.js checks that table every 60 seconds and does what it needs to do, then updates the table after the changes are completed. 我考虑过的一个选项可能是我用所需的更改更新数据库中的表,然后node.js每60秒检查一次该表并执行所需的操作,然后在更改完成后更新该表。

Is that the best option or should I try to accomplish this with PHP? 那是最好的选择还是我应该尝试使用PHP做到这一点? Obviously PHP would be more immediate -- but that's not too much of a concern for me. 显然,PHP会更直接-但这对我来说不是太大的问题。

It's quite simple: 很简单:

  1. Make sure your node.js code supports HTTP requests and not just sockets. 确保您的node.js代码支持HTTP请求,而不仅仅是套接字。

  2. Add a route to perform any actions. 添加路线以执行任何操作。 For instance you could handle /api/disconnect/:userId which would do whatever you need. 例如,您可以处理/api/disconnect/:userId ,它将执行所需的任何操作。

  3. In your PHP code, call the relevant URL using either file_get_contents or the cURL family of calls. 在您的PHP代码中,使用file_get_contents或cURL调用族来调用相关的URL。

Of course, you want to make sure you have some decent security so that only your PHP script can call you webservices, otherwise you'd open the door to all sorts of attacks. 当然,您要确保具有一定的安全性,以便只有PHP脚本才能调用Web服务,否则,您将为各种攻击打开大门。 In the short term, this could be done simply by only listening 127.0.0.1 and using that address to communicate. 在短期内,仅通过侦听127.0.0.1并使用该地址进行通信即可完成此操作。

Note that the title of your question does not actually match its text. 请注意,问题的标题实际上与文本不符。 You're not actually modifying the node.js code, you're just modifying the data it manipulates. 您实际上并没有在修改node.js代码,而只是在修改它处理的数据。

Not sure that I follow your description, however one thing that stands out is that you are considering modifying code using code. 不确定我是否遵循您的描述,但是突出的一件事是您正在考虑使用代码修改代码。 Don't. 别。 Self-modifying code (regardless if it split across separate components of a system) is a very bad idea. 自修改代码(无论是否将代码拆分到系统的各个组件中)都是一个非常糟糕的主意。

If the socket ID already exists for that user, then I would disconnect the socket. 如果该用户的套接字ID已经存在,那么我将断开套接字。

Exists where? 存在于哪里? Presumably in node.js. 大概在node.js中。

While you could use the database to persist the data, I would try to inject the data directly into node.js from the PHP script. 虽然您可以使用数据库来保留数据,但我会尝试将数据从PHP脚本直接注入到node.js中。 That eliminates the polling step. 这样就消除了轮询步骤。 There is a question over how you re-populate the information into node.js after a restart, but that depends on a lot more information than you've provided. 关于如何在重启后将信息重新填充到node.js中存在一个问题,但这取决于比您提供的信息更多的信息。

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

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