来自 php 源的 Socket.io

时间:2023-03-05
本文介绍了来自 php 源的 Socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经使用 socket.io 开发了一个客户端-服务器连接,我很高兴地说它运行良好.问题来了,因为我需要从 php Source (而不是echo html javascript")创建该连接.像一个

I have developed a client-server connection by using socket.io, and i'm happy to say that it works perfectly. The problem comes because I need to create that connection from php Source ( not "echo html javascript" ). Something Like a

$socket = socket.createConectionJS;

我尝试了不同的方法,例如:

I've tried difrerent ways, such as :

  • 从服务器执行代码(使用 spiderMonkey 和 node )
  • 创建一个 phpSocket 并将其连接到 ServerSocket.j(但显然格式不同).或...
  • 最后,我尝试通过 curl 进行发布,我可以访问服务器脚本,但无法打开连接

我认为解决它的唯一方法是通过使用与 de socket.io 类相同的方式创建套接字来模拟 Web 套接字连接,但在 php 中

I'm thinking the only way to resolve it is simulating the web socket connection by creating a socket with the same way as de socket.io class do it but in php

任何 socket.io 专家可以帮忙吗?

Anny socket.io expert could help ?

谢谢!!_

推荐答案

是的,我当时就快速了解了一下.我很幸运已经在 PHP 中构建了一个相当先进的网络抓取库,所以我只是将来自 TamperData(firefox 插件)的 XML 插入其中,并进行了一些调整.

Yeah, I had a quick go at it just then. I'm fortunate to have a fairly advanced web scraping library already built in PHP, so I just plugged the XML from TamperData (firefox plugin), into it, and tweaked a few things.

为了模拟 xhr-polling(我的代理不允许 websockets,无论如何这看起来更简单)...

To emulate xhr-polling (my proxy doesn't allow websockets, and this looked simpler anyway)...

提出请求:

/socket.io/1/?t=1337779479761   

(13377...数字只是一个时间戳,使用 time() 生成它).

(The 13377... number is just a timestamp, use time() to generate it).

应该返回如下内容:

682970926640665221:60:60:websocket,htmlfile,xhr-polling,jsonp-polling

抓住前面的大数字,那就是您的[CONNECT_ID]",您将在本次会议的剩余时间内保留该号码.现在做另一个请求:

Grab the big number at the front out, that's your "[CONNECT_ID]", which you'll keep for the remainder of the session. Now do another request:

/socket.io/1/xhr-polling/[CONNECT_ID]?t=[TIMESTAMP]

你会得到类似 ::1 的东西

And you'll get back something like ::1

就我费心去遵循它而言,这一切看起来都相当基本......没有特殊的标题或任何偷偷摸摸的东西.建议您使用 TamperData 或数据包嗅探器,并自行遵循.这是我的代码的输出:

That's about as far as I bothered to follow it, it all looked fairly basic from there... no special headers or anything sneaky. Suggest you use TamperData or a packet sniffer, and just follow it yourself. Here was the output from my code:

$ php RealTestCurl.php xml/xhr.xml init1 xhr1 xhr1 xhr1 xhr1

xmlFilename: xml/xhr.xml

Step: init1
Reply: 7638339571841585529:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
Found: connect_id: ([0-9]*) - 7638339571841585529

Step: xhr1
Reply: 1::

Step: xhr1
Reply: ?46?5:::{"name":"news","args":[{"hello":"world"}]}?63?5:::{"name":"this","args":[{"will":"be received by everyone"}]}

Step: xhr1
.... there is a massive 20 second timeout here

Step: xhr1
8::

Step: xhr1
8::

在 node.js/socket.io 一侧,运行其首页上的基本示例:

And on the node.js/socket.io side, running on of the basic examples from their front page:

debug - client authorized
info  - handshake authorized 3445861131360107212
debug - setting request GET /socket.io/1/xhr-polling/3445861131360107212?t=1337781185
debug - setting poll timeout
debug - client authorized for 
debug - clearing poll timeout
debug - xhr-polling writing 1::
debug - set close timeout for client 3445861131360107212
debug - websocket writing 5:::{"name":"this","args":[{"will":"be received by everyone"}]}

这篇关于来自 php 源的 Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:服务器在 WebSocket 中重新启动时重新连接客户端 下一篇:定期向 Ratchet 中的客户端发送消息

相关文章