2016-10-16 8 views
3

WebSocketモジュールを購入し、WAMP環境にインストールしました。私はまた、正しい場所にIPCファイルを生成し、永遠にイベントを聞くためにループするPHPスクリプトを持っています。しかし、このクライアント側のコードを使用して:接続がトリガされるとステータス200のWebSocketエラーは何ですか?

var websocket = null; 

var connect = function() { 
    var button = document.getElementById('connect-button'); 
    // This function is a convenience function, to set the content 
    // of the response display 
    var setResponse = function(text) { 
     var element = document.getElementById('response'); 
     element.innerHTML = text; 
    } 
    // A message telling the user we started connecting is always 
    // useful 
    button.innerHTML = 'Connecting ...'; 
    // The file name `demo.ws' could in principle help in having multiple 
    // websockets at a single domain name. 
    // 
    // It's not implemented right now, but it can be if it's needed and 
    // it's not too hard.  
    //var url = "ws://websocket-example/websocket-example/websocket-example-websocket.rp"; 
    var url = 'ws://' + window.location.hostname + '/WebSocket/websocket-example-websocket.rp'; 
    // Create the websocket connection now  
    websocket = new WebSocket(url, 'standard'); 
    // Install the handlers, the On Open handler is triggered 
    // immediately after the conection has been established 
    // and a successful handshake 
    websocket.onopen = function(event) { 
     // Update the connection status indicator 
     var element = document.getElementById('connection-status'); 
     var input = document.getElementById('input'); 
     element.innerHTML = 'Connected Now'; 
     element.setAttribute('class', 'online'); 
     // Update the button, and install a new handler to allow 
     // closing the websocket connection 
     button.innerHTML = 'Close'; 
     button.onclick = function() { 
      websocket.close(); 
     } 
     input.focus(); 
     input.value = ''; 
    } 
    // On close and on error handler, this is a simple demo 
    // hence the simplistic approach. Ideally this should be 
    // two separate functions 
    websocket.onclose = 
    websocket.onerror = function(event) { 
     // Update the connection status indicator 
     var element = document.getElementById('connection-status'); 
     var input = document.getElementById('input'); 
     element.innerHTML = 'Offline'; 
     element.setAttribute('class', 'offline'); 
     // Update button click handler, to reconnect if requested 
     button.innerHTML = 'Connect'; 
     button.onclick = connect; 
     input.value = ''; 
     // Clear the response text 
     setResponse(''); 
     // Reset the websocket global variable 
     websocket = null; 
    } 
    // On message handler, triggered when a message is received 
    websocket.onmessage = function(event) { 
     // Set the response text 
     setResponse(event.data); 
    } 
} 

var send = function(message) { 
    // Send a message to the server but check that the websocket 
    // was connected first 
    if (websocket === null) 
     return; 
    // It's ok so, send the message now 
    websocket.send(message); 
} 

、私の要求は、次のエラーで失敗します。

WebSocket connection to 'ws://websocket-example/WebSocket/websocket-example-websocket.rp' failed: Error during WebSocket handshake: Unexpected response code: 200

websocketnullまま。私は絶対にこのエラーを理解していない、200はOK状態だと思われるが、依然として私の要求は失敗する。 IPCファイルはサーバーの起動後に生成され、正しい場所にあり、スクリプトを実行しているユーザーはファイルを要求するのに必要な特権を持っています。この動作の理由は何ですか?どのように修正できますか?

+0

PHPでWebSocketサーバの初期化について説明するサンプルを用意できますか?サーバーは、任意の接続元からの接続を受け入れますか? –

+0

@AxelIsouard、私はそれを買ったので、私はハンドラのソースコードを共有することが許されていないと私はモジュールのソースコードを持っていないので、あなたの質問に答えるために、私は依存する必要がありますIPCファイルが存在する場合はそれを削除して再作成し、接続やメッセージをチェックして処理する永遠のループに入るハンドラPHPスクリプトがあります。ループの各反復の後、それは250ミリ秒待機する。これは作者で働いて、私はデモをテストして、WebSocketが動作するのを見ました。 –

+0

@AxelIsouardしかし、このモジュールは若干異なるバージョンのApache用にビルドされました。それは考えられる問題です。私は彼にもEメールを送り、彼の反応を見るでしょう。私はここにWebSocketを走らせることに非常に近づき、このエラーに悩まされています。どこから来て、何を意味するのか分かりません。 –

答えて

3

WebSocket接続では、101 Switching Protocolsのステータス応答が表示されません。
200ステータスレスポンスを取得すると、要求がWebSocketハンドラに届かなかった可能性があります。

また、dev-toolsを調べて、応答にWebSocketハンドシェイクヘッダーがあるかどうかを確認します。
もしそうなら、私はそれがモジュールの問題だと思うでしょう。それ以外の場合はおそらくあなたの設定です。

0

このような応答は、URLに配置されているリモートのリソースがリッスンしていないか、少なくともWebSocket要求に応答できないことを意味します。 アクセスしようとしているリモートサービスを確認します。サーバーをコード化した場合は、ソースを投稿してください

+0

ケビン、私は私が支払ったコードを掲載することを許可されているか分からない。私は質問のコメントセクションでどのように動作するかを要約しましたが、ここで実際のサーバーコードを共有することはできません。うまく動作するモジュールを作成した優秀な人の利益を損なうでしょう。私はそれを設定することでほぼ完了しましたが、このエラーに固執して、意味と原因について疑問に思っています。私たちがそれを絞り込むことができれば、私は問題を簡単に解決することができますが、問題のスペースはかなり広がっています。 –

関連する問題