問題描述
我已經使用 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!