2014-06-30 28 views
2

我試圖將PHP函數轉換成jQuery函數,以便我可以直接從瀏覽器使用TCP/IP套接字本地連接。jQuery Web插座沒有連接,也沒有發送

我用這個:

$socket = @fsockopen('xxx.xxx.xxx.xxx', '8810', $err_no, $err_str); 

if(!$socket){ 
    return 'Errore #'.$err_no.': '.$err_str; 
}else{ 
    fwrite($socket, '["D";"C";"?ST";200]'); 
    $read = fread($socket, 2024); 

    //other stuff... 

    return $read; 
    fclose($socket); 

} 

這工作得很好。然後,我從谷歌代碼網站下載了Github jQuery Websocket 0.0.4,並按照該示例進行操作,但未成功。

我只是單純的試圖建立連接和發送數據是這樣的:

ws = $.websocket("ws://95.110.224.199:8810/"); 
ws.send('string', '["D";"C";"?ST";200]'); 

這給了我「不確定是不是一個函數」的錯誤。

然後我想看看如果連接實際上是建立(不發送數據)這樣說:

var ws = $.websocket("ws://xxx.xxx.xxx.xxx:8810/", { 
    open: function() {console.log('WS:connesso')}, 
    close: function() {console.log('WS:chiuso')}, 
}); 

沒有運氣,我......控制檯說沒什麼...... 任何提示或幫助在這?任何幫助表示讚賞。

+0

我不知道該庫,但因爲它似乎你只是想建立一個簡單的WS連接和發送的東西也許它會更好地使用默認功能。看看這裏的一個簡單的WebSocket JS指南:http://www.tutorialspoint.com/html5/html5_websocket.htm – zewa666

+0

嗨,tks,這給了我這個錯誤'WebSocket連接'ws://xxx.xxx.xxx .xxx:8830 /'失敗:連接在收到握手響應之前關閉。我也嘗試過重新連接插槽,但仍然... –

+0

這可能表明A:Socket未運行,B:端口已被佔用,C:握手未正確完成(與使用不同版本的PHP實現相同的問題),D:訪問跨域可能會禁止websockets通過您的瀏覽器設置。還有更多的場景,但那些是我現在可以記得的場景。無論如何,我認爲問題更多地放在服務器實現上。沒有關於服務器和具體實施的更多信息,很難正確地幫助您 – zewa666

回答

3

也許你錯過了jquery.websocket.js文件,但你可以使用JavaScript web套接字:

ws = new WebSocket('ws://95.110.224.199:8810/'); 

或者你可以把0.0.0.0中,而不是你的ip的,因爲一些服務器不允許直接IP接入:

ws = new WebSocket('ws://0.0.0.0:8810/') 
ws.onopen = function(msg) { 
    // Logic for opened connection 
    console.log('Connection successfully opened'); 
}; 
ws.onmessage = function(msg) { 
    // Handle received data 
}; 

ws.onclose = function(msg) { 
    // Logic for closed connection 
    console.log('Connection was closed.'); 
} 
ws.error =function(err){ 
    console.log(err); // Write errors to console 
} 
+0

我總是得到相同的錯誤:'WebSocket連接到'ws://000.000.000:8810 /'失敗:在php工作時,連接在接收握手響應之前關閉。 –

+0

你可以試試'ws://0.0.0.0:8810' –

0

個人而言,我建議使用純javascript new WebSocket('ws://');它很容易使用,只要有一個後端套接字偵聽相同的地址和端口,就會連接。

1

WebSockets不允許您以來自PHP的fsockopen的方式連接到任意的TCP服務器。服務器還必須支持WebSocket協議,具體爲:

  1. If the server chooses to accept the incoming connection, it MUST reply with a valid HTTP response indicating the following.

    1. A Status-Line with a 101 response code as per RFC 2616 [RFC2616]. Such a response could look like "HTTP/1.1 101 Switching Protocols".

    2. An |Upgrade| header field with value "websocket" as per RFC 2616 [RFC2616].

    3. A |Connection| header field with value "Upgrade".

    4. A |Sec-WebSocket-Accept| header field. The value of this header field is constructed by concatenating /key/, defined above in step 4 in Section 4.2.2, with the string "258EAFA5- E914-47DA-95CA-C5AB0DC85B11", taking the SHA-1 hash of this concatenated value to obtain a 20-byte value and base64- encoding (see Section 4 of [RFC4648]) this 20-byte hash.

      The ABNF [RFC2616] of this header field is defined as follows:

      Sec-WebSocket-Accept = base64-value-non-empty base64-value-non-empty = (1*base64-data [ base64-padding ]) | base64-padding base64-data = 4base64-character base64-padding = (2base64-character "==") | (3base64-character "=") base64-character = ALPHA | DIGIT | "+" | "/"

(從WebSocket protocol specification

我懷疑你試圖連接的服務器是不是特殊的WebSocket服務器,因此沒有按回復客戶端握手的HTTP響應。