2011-02-18 60 views
0

我正在處理websocket應用程序。我有一個用C#編寫的服務器。我已經使用另一個C#應用程序測試了它,用於發送和接收數據。當我在Chrome開發者工具(控制檯)上使用JavaScript並使用Websockets連接到我的服務器時,會出現問題。Websocket的幫助!

  1. 我收到的頭部字符串從WebSocket的腳本,有兩個鍵,最後8個字符的散列。

  2. 我用的標題字符串鍵生成一個散列代碼和包裝箱頭傳送回鉻(j腳本的開發工具)。

問題: -

  1. OnOpen事件永遠不會觸發和WebSocket的不接收頭(我認爲)。我使用onerror來捕獲任何錯誤。從來沒有發生過。

  2. 上的WebSocket readyState的是0或2(總是)。

    • 但是,當我從服務器發送斷開連接響應時,websocket觸發onclose方法。 (所以,我認爲他是開放的,但沒有準備好通信)

任何建議???????這是JavaScript,如果它有幫助。

websocket = new WebSocket('ws://My server IP here:8080'); 

try { 
    websocket.onopen = function(evt) { 
     open(evt) 
     //websocket.send("Message to send"); 
     alert("Message is sent..."); 
    } 
} 
catch(err) { 
    debug(err,'error') 
} 

websocket.onerror = function(evt) { 
    error(evt) 
} 

websocket.onclose = function(evt) { 
    close(evt) 
} 

websocket.onmessage = function(evt) { 
    message(evt) 
} 

function open(evt) { 
    alert("CONNECTED"); 
    doSend("WebSocket rocks"); 
} 

function error(evt) { 
    alert (evt.data) 
} 

function close(evt) { 
    alert("DISCONNECTED"); 
} 

function message(evt) { 
    alert(evt.data); 
} 

function doSend(message) { 
    alert(message); 
    websocket.send(message); 
} 

和報頭我送回

HTTP/1.1 101 WebSocket協議握手

升級:網頁套接字

連接:升級

仲丁基的WebSocket-產地:鉻:// NEWTAB

二段的WebSocket-地點:WS://我的服務器IP:8080 ??我??? M2 + 9?!?

謝謝大家。

+4

順便說一句Java!= Javascript。 – 2011-02-18 16:16:57

回答

0

你是否實施了正確的WebSockets協議版本? Chrome有moved to version 76,這意味着握手比以前更復雜。如果你的Javascript客戶端沒有正確連接,這可能是原因。

2

看起來您正嘗試在沒有適當的挑戰響應的情況下響應握手請求。像Robin提到的那樣,握手現在更復雜,並且對較新版本的WebSocket協議提出挑戰。 This is a good article更詳細地解釋了版本76握手。

這是一個代碼示例,它檢測WebSocket協議版本並回復相應的響應。 (這是Java的,所以YMMV,但它應該指向你在正確的方向。)

// Create the WebSocket handshake response. 
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, 
    new HttpResponseStatus(101, "Web Socket Protocol Handshake")); 
res.addHeader(Names.UPGRADE, WEBSOCKET); 
res.addHeader(CONNECTION, Values.UPGRADE); 

// Fill in the headers and contents depending on handshake method. 
// New handshake specification has a challenge. 
if (req.containsHeader(SEC_WEBSOCKET_KEY1) 
     && req.containsHeader(SEC_WEBSOCKET_KEY2)) { 

    // New handshake method with challenge 
    res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); 
    res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req)); 

    String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL); 

    if (protocol != null) { 
     res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol); 
    } 

    // Calculate the answer of the challenge. 
    String key1 = req.getHeader(SEC_WEBSOCKET_KEY1); 
    String key2 = req.getHeader(SEC_WEBSOCKET_KEY2); 
    int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", ""))/key1 
      .replaceAll("[^ ]", "").length()); 
    int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", ""))/key2 
      .replaceAll("[^ ]", "").length()); 
    long c = req.getContent().readLong(); 
    ChannelBuffer input = ChannelBuffers.buffer(16); 
    input.writeInt(a); 
    input.writeInt(b); 
    input.writeLong(c); 
    ChannelBuffer output = ChannelBuffers 
      .wrappedBuffer(MessageDigest.getInstance("MD5").digest(
        input.array())); 
    res.setContent(output); 
} else { 
    // Old handshake method with no challenge: 
    res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); 
    res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req)); 
    String protocol = req.getHeader(WEBSOCKET_PROTOCOL); 
    if (protocol != null) { 
     res.addHeader(WEBSOCKET_PROTOCOL, protocol); 
    } 
} 

// Send the response...