简体   繁体   English

通过PHP和cURL访问NodeJS / Socket.io

[英]Access NodeJS / Socket.io over PHP and cURL

I'm running a nodejs / socket.io (v 0.7) server to push data to web clients. 我正在运行一个nodejs / socket.io(v 0.7)服务器,将数据推送到Web客户端。 That works greatly. 那很有用。

To extend the application, I need to trigger / influence socket.io over php. 为了扩展应用程序,我需要通过php触发/影响socket.io。 I know a solution that works with cURL over http with the nodejs http server. 我知道一个可以在nodejs http服务器上通过http使用cURL的解决方案。

One possible solution is to run the socket.io server and the nodejs http server in one an the same app.js. 一种可能的解决方案是在同一app.js中运行socket.io服务器和nodejs http服务器。 The clients connect and communicate with socket.io, and the only allowed connection to the "seperate" http server (listening on another port than socket.io, of course) is from 127.0.0.1 respectively from the php server over cURL. 客户端与socket.io连接并与之通信,并且与“单独的” http服务器(当然,监听的端口不是socket.io)唯一允许的连接分别是来自cURL的php服务器的127.0.0.1。

Because the both servers are in the same scope, the http server can trigger events on the socket.io server. 因为两个服务器都在同一作用域中,所以http服务器可以在socket.io服务器上触发事件。

My Question: doest that make any sense? 我的问题:这有意义吗? Is there a cleaner solution to trigger events on socket.io from PHP? 是否有更干净的解决方案可以从PHP触发socket.io上的事件?

I think that's a reasonable and straightforward way to go; 我认为这是一种合理而直接的方法。 it seems like caution would be advised for dealing with order of execution, but that's probably just my uncertainty with node showing. 似乎建议谨慎处理执行顺序,但这可能只是我不确定节点显示的情况。

I am writing a solution which I think looks a lot like pusher API. 我正在写一个解决方案,我认为它看起来很像Pusher API。 It utilizes room(channel) concept. 它利用房间(通道)的概念。 From a route via express I can curl(via PHP) to send messages to room(channel). 从通过Express的路由中,我可以卷曲(通过PHP)将消息发送到room(通道)。

io.sockets.in('your room').json.send('your json');

I'd like to open-source that project in the future(hopefully within a week), but hopefully you get the picture. 我想将来(希望在一周之内)将该项目开源,但希望您能理解。

socket.io with version greater than 1.3.3 (may be older),you can trigger a emit event as below 版本高于1.3.3(可能更旧)的socket.io,您可以触发如下的发射事件

//send broadcast message to all connecting clients
io.emit([message event],[your message body obj or str]);

// send to specific user
io.to([your room]).emit([message event], [your message body obj or str]);

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

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