te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - WebSocket closes on send - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebSocket closes on send - Stack Overflow

programmeradmin3浏览0评论

So I saw this great blog post, Experimenting with Node.js. I decided to try and set it up on my own using the author's gist. It didn't work.

Further debugging shows me that the the websocket is connecting fine, but is closing as soon as 'send' is invoked. Here is the wireshark trace(forgive the weird spacing):

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}.

I've tried this in both Chrome and Firefox 4.0 beta. They both exhibit the same behavior. If I go to the original blog site, it works fine.

Another thing. If I go into the JS console in either FF or Chrome and I do the following:

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

It immediately disconnects and does not send the message. The server shows the connection and handshake, but never receives a message.

I've found a few questions here that were similar but were either resolved without posting the fix or did not seem to apply in this situation. I can post the code from the gist if it will make it easier.

So I saw this great blog post, Experimenting with Node.js. I decided to try and set it up on my own using the author's gist. It didn't work.

Further debugging shows me that the the websocket is connecting fine, but is closing as soon as 'send' is invoked. Here is the wireshark trace(forgive the weird spacing):

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}.

I've tried this in both Chrome and Firefox 4.0 beta. They both exhibit the same behavior. If I go to the original blog site, it works fine.

Another thing. If I go into the JS console in either FF or Chrome and I do the following:

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

It immediately disconnects and does not send the message. The server shows the connection and handshake, but never receives a message.

I've found a few questions here that were similar but were either resolved without posting the fix or did not seem to apply in this situation. I can post the code from the gist if it will make it easier.

Share Improve this question edited Jul 29, 2010 at 14:53 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked Jul 29, 2010 at 12:19 hernan43hernan43 8051 gold badge8 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

The CloseEvent has a "code" property that will give you information about why your connection was closed.

"Returns an unsigned short containing the close code send by the server. The following values are permitted status codes."

A variety of code values are supported. Here are the most prominent:

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

See CloseEvent API docs on MDN for more.

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 menting the send item ws.send(JSON.stringify(whatever));.

  2. Check if something is wrong in Android (Client) - By menting the onMessage. Log.d("TAG","onMessage: " +text); Then just see how you are handling the data and ment 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 munity which is difficult. Good cook books, project video tutorials are hard to e 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

Major headslap. Despite believing I had the latest version of Node.js installed I did not. I have a couple machines with Node.js on them I must have lost track. I had Node.js v0.1.96. After upgrading to v0.1.102, everything is working fine.

Sorry guys! :-D

For the problem of disconnect happening when you issue the send from the browser, you need to wait for the onopen event to fire before issuing a 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);
}
发布评论

评论列表(0)

  1. 暂无评论