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

问题描述

我已经通过使用 socket.io 开发了一个客户端 - 服务器连接,我很高兴地说,它工作完美。
问题出现,因为我需要从php源(不是echo html javascript)创建该连接。像

  $ socket = socket.createConectionJS; 

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




  • 从服务器(使用spiderMonkey和节点)

  • 执行代码创建一个phpSocket并将其连接到ServerSocket.j格式不同)。或...
  • 最后,我已经尝试通过curl,我可以访问serverscript,但我无法打开连接



我认为解决它的唯一方法是模拟web套接字连接创建一个套接字与de socket.io类做同样的方式,但在php



Anny socket.io专家可以帮助?



Tnks! ! _

解决方案

是啊,我很幸运有一个相当高级的网络抓取库已经内置在PHP,所以我只是插入来自TamperData(firefox插件)的XML,并调整了几件事。



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



请求:

  /socket.io/1/?t=1337779479761 

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



像这样:

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

抓住前面的大数字,这是你的[CONNECT_ID],会话。现在再执行一个请求:

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

你会得到类似:: 1的


$ b b

这是关于我打算跟着它,这一切看起来相当基本从那里...没有特殊的标题或任何鬼祟。建议你使用TamperData或数据包嗅探器,只是自己跟着它。这是我的代码的输出:

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

xmlFilename:xml / xhr.xml

步骤:init1
回复:7638339571841585529:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
找到:connect_id:([0-9] *) - 7638339571841585529

步骤:xhr1
回复:1 ::

步骤:xhr1
:?46?5 ::: {name:news,args:[{hello:world}] args:[{will:will received by everyone}]}

步骤:xhr1
....这里有一个巨大的20秒超时

步骤:xhr1
8 ::

步骤:xhr1
8 ::

在node.js / socket.io端,从头开始运行基本示例:

  debug  -  client authorized 
info - handshake authorized 3445861131360107212
debug - 设置请求GET /socket.io/1/xhr-polling/3445861131360107212?t=1337781185
debug - 设置轮询超时
debug - 客户机授权
debug - 清除轮询超时
debug - xhr-polling writing 1 ::
debug - 设置客户机的关闭超时3445861131360107212
debug - websocket writing 5 ::: {name:this,args:[{will:will received by everyone}]}
pre>

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 :

  • execute the code from the server ( with spiderMonkey, and node )
  • creating a phpSocket and connecting it to the ServerSocket.j( but obbiously the format is diferent). or ...
  • Finally i've tried by post with curl, where I had access to the serverscript, but i can't open a connection

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

Anny socket.io expert could help ?

Tnks ! ! _

解决方案

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.

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

Make a request to:

/socket.io/1/?t=1337779479761

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

That should return something like this:

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

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]

And you'll get back something like ::1

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::

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"}]}

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

07-30 19:43