8

だから私はこの素晴らしいブログ投稿、Node.jsの実験を見ました。著者の要点を使って、自分で設定してみることにしました。うまくいきませんでした。

さらにデバッグすると、WebSocketは正常に接続されていますが、「send」が呼び出されるとすぐに閉じます。これがwiresharkのトレースです(奇妙な間隔を許してください):

GET /test HTTP/1.1

Host: 127.0.0.1:8000

Sec-WebSocket-Key2: 3   j 92 9   62" 7 0 8 8

Upgrade: WebSocket

Connection: Upgrade

Origin: http://127.0.0.1:3000

Sec-WebSocket-Key1: 96'5% S72.93?06



......(bHTTP/1.1 101 WebSocket Protocol Handshake

Upgrade: WebSocket

Connection: Upgrade

Sec-WebSocket-Origin: http://127.0.0.1:3000

Sec-WebSocket-Location: ws://127.0.0.1:8000/test



.4.R....mh.....{.{"action":"move","x":450,"y":22,"w":1146,"h":551}.

ChromeとFirefox4.0ベータの両方でこれを試しました。どちらも同じ動作を示します。元のブログサイトにアクセスすると、正常に動作します。

別物。FFまたはChromeのいずれかでJSコンソールにアクセスし、次のことを行う場合:

ws = new WebSocket('ws://localhost:8000/test')
ws.send("foo")

すぐに切断され、メッセージは送信されません。サーバーは接続とハンドシェイクを表示しますが、メッセージを受信することはありません。

ここで、類似しているが、修正を投稿せずに解決されたか、この状況では当てはまらないように思われる質問をいくつか見つけました。簡単にできるのであれば、要点からコードを投稿できます。

4

4 に答える 4

3

CloseEvent には、接続が閉じられた理由に関する情報を提供する "code" プロパティがあります。

「サーバーから送信された終了コードを含む unsigned short を返します。次の値は許可されているステータス コードです。」

さまざまなコード値がサポートされています。最も顕著なものは次のとおりです。

  • 1000: CLOSE_NORMAL
  • 1001: CLOSE_GOING_AWAY
  • 1002: CLOSE_PROTOCOL_ERROR
  • 1003: CLOSE_UNSUPPORTED
  • 1005: CLOSE_NO_STATUS

詳細については、MDN の CloseEvent API ドキュメントを参照してください。

于 2016-05-03T15:18:42.727 に答える
1

メジャーヘッドスラップ。Node.js の最新バージョンがインストールされていると信じていたにもかかわらず、そうではありませんでした。Node.js を搭載したマシンがいくつかありますが、道に迷ったに違いありません。私はNode.js v0.1.96を持っていました。v0.1.102 にアップグレードした後、すべて正常に動作しています。

ごめんなさい!:-D

于 2010-07-30T13:46:53.707 に答える
1

In Android, for me the problem is on how I am handling the data. I was able to pinpoint by doing the following.

  1. Check if something is wrong in NodeJs (Server) - By commenting the send item ws.send(JSON.stringify(whatever));.

  2. Check if something is wrong in Android (Client) - By commenting the onMessage. Log.d("TAG","onMessage: " +text); Then just see how you are handling the data and comment out those parts. Run the log cats on onClosed, onFailure

This should help you in pinpointing the problem at least, because NodeJs Websocket help is hard to find. Languages are not hard - its the documentation, support and lack of community which is difficult. Good cook books, project video tutorials are hard to come by.

I was getting the data as Json arrays and trying to populate in recycler view the wrong way. This was the problem for me.

You should also try the following code in your node.js file to see the reason

// Connection Closed
ws.on('close', function close(code, reason) {
console.log('ws is closed with code: ' + code + ' reason: ' + reason);
});

// On Error
ws.on('error', function(e) {
    console.log("error occured" +e);
});

For full problem and solution please see here : Websocket closed code: 1006 Node Android okhttp3 AmazonEc2

于 2020-05-20T05:35:24.850 に答える
0

ブラウザから send を発行したときに切断が発生する問題については、onopen イベントが発生するのを待ってから send を発行する必要があります。

var conn = new WebSocket('ws://localhost:8000/test');
conn.onopen = function (e) {
    conn.send('foo');
}
conn.onmessage = function (e) {
    console.log('got: ' + e.data);
}
于 2010-10-14T13:51:37.847 に答える