Permalink
Browse files

websocket, feat: change readyState to CLOSED in js thread.

  • Loading branch information...
xicilion committed Oct 28, 2017
1 parent 3240d9d commit 44e76577278df56b32fc15e02b7a5f382e5b5014
Showing with 18 additions and 4 deletions.
  1. +10 −0 fibjs/include/WebSocket.h
  2. +4 −4 fibjs/src/websocket/WebSocket.cpp
  3. +4 −0 test/ws_test.js
View
@@ -26,6 +26,7 @@ class WebSocket : public WebSocket_base {
, m_compress(false)
, m_maxSize(67108864)
, m_readyState(ws_base::_CONNECTING)
, m_closeState(ws_base::_OPEN)
{
}
@@ -37,6 +38,7 @@ class WebSocket : public WebSocket_base {
, m_compress(false)
, m_maxSize(67108864)
, m_readyState(ws_base::_OPEN)
, m_closeState(ws_base::_OPEN)
{
}
@@ -50,6 +52,13 @@ class WebSocket : public WebSocket_base {
return 0;
}
virtual result_t onEventEmit(exlib::string ev)
{
if (m_closeState == ws_base::_CLOSED)
m_readyState.xchg(ws_base::_CLOSED);
return 0;
}
public:
// WebSocket_base
virtual result_t get_url(exlib::string& retVal);
@@ -95,6 +104,7 @@ class WebSocket : public WebSocket_base {
exlib::atomic m_readyState;
exlib::atomic m_readState;
exlib::atomic m_closeState;
int32_t m_code;
exlib::string m_reason;
@@ -373,7 +373,7 @@ void WebSocket::startRecv()
obj_ptr<SeekableStream_base> body;
pThis->m_msg->get_body(body);
if (pThis->m_this->m_readyState.CompareAndSwap(ws_base::_OPEN, ws_base::_CLOSING) == ws_base::_OPEN)
if (pThis->m_this->m_closeState.CompareAndSwap(ws_base::_OPEN, ws_base::_CLOSING) == ws_base::_OPEN)
new asyncSend(pThis->m_this, body, ws_base::_CLOSE);
else
pThis->m_this->endConnect(body);
@@ -412,7 +412,7 @@ void WebSocket::startRecv()
void WebSocket::endConnect(int32_t code, exlib::string reason)
{
if (m_readyState.xchg(ws_base::_CLOSED) != ws_base::_CLOSED) {
if (m_closeState.xchg(ws_base::_CLOSED) != ws_base::_CLOSED) {
Isolate* isolate = holder();
if (isolate) {
if (code > 1000 && code < 3000) {
@@ -512,7 +512,7 @@ result_t WebSocket::close(int32_t code, exlib::string reason)
result_t WebSocket::send(exlib::string data)
{
if (m_readyState != ws_base::_OPEN)
return CHECK_ERROR(CALL_E_INVALID_CALL);
return CHECK_ERROR(Runtime::setError("websocket: WebSocket is in CONNECTING, CLOSING or CLOSED state."));
new asyncSend(this, data);
return 0;
@@ -521,7 +521,7 @@ result_t WebSocket::send(exlib::string data)
result_t WebSocket::send(Buffer_base* data)
{
if (m_readyState != ws_base::_OPEN)
return CHECK_ERROR(CALL_E_INVALID_CALL);
return CHECK_ERROR(Runtime::setError("websocket: WebSocket is in CONNECTING, CLOSING or CLOSED state."));
new asyncSend(this, data);
return 0;
View
@@ -607,6 +607,7 @@ describe('ws', () => {
};
s.onclose = (e) => {
assert.equal(s.readyState, ws.CLOSED);
assert.equal(e.code, 1000);
assert.equal(e.reason, "123");
tc = true;
@@ -628,6 +629,7 @@ describe('ws', () => {
};
s.onclose = (e) => {
assert.equal(s.readyState, ws.CLOSED);
// assert.equal(e.code, 3000);
// assert.equal(e.reason, "remote");
tc = true;
@@ -705,6 +707,7 @@ describe('ws', () => {
};
s.onerror = (e) => {
assert.equal(s.readyState, ws.CLOSED);
assert.equal(e.code, 1002);
te = true;
};
@@ -737,6 +740,7 @@ describe('ws', () => {
};
s.onerror = (e) => {
assert.equal(s.readyState, ws.CLOSED);
assert.equal(e.code, 1002);
te = true;
};

0 comments on commit 44e7657

Please sign in to comment.