WS_ERROR -> ERROR, WS_STATE -> STATE.

This commit is contained in:
James Chen 2013-06-01 19:51:10 +08:00
parent 85d2ccbf65
commit 4b8f76c903
5 changed files with 38 additions and 38 deletions

View File

@ -225,7 +225,7 @@ enum WS_MSG {
}; };
WebSocket::WebSocket() WebSocket::WebSocket()
: _readyState(WS_STATE_CONNECTING) : _readyState(STATE_CONNECTING)
, _port(80) , _port(80)
, _wsHelper(NULL) , _wsHelper(NULL)
, _wsInstance(NULL) , _wsInstance(NULL)
@ -335,7 +335,7 @@ bool WebSocket::init(const Delegate& delegate,
void WebSocket::send(const std::string& message) void WebSocket::send(const std::string& message)
{ {
if (_readyState == WS_STATE_OPEN) if (_readyState == STATE_OPEN)
{ {
// In main thread // In main thread
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
@ -353,7 +353,7 @@ void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
{ {
CCAssert(binaryMsg != NULL && len > 0, "parameter invalid."); CCAssert(binaryMsg != NULL && len > 0, "parameter invalid.");
if (_readyState == WS_STATE_OPEN) if (_readyState == STATE_OPEN)
{ {
// In main thread // In main thread
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
@ -371,11 +371,11 @@ void WebSocket::close()
{ {
CCDirector::sharedDirector()->getScheduler()->unscheduleAllForTarget(_wsHelper); CCDirector::sharedDirector()->getScheduler()->unscheduleAllForTarget(_wsHelper);
if (_readyState == WS_STATE_CLOSING || _readyState == WS_STATE_CLOSED) if (_readyState == STATE_CLOSING || _readyState == STATE_CLOSED)
return; return;
CCLOG("websocket (%p) connection closed by client", this); CCLOG("websocket (%p) connection closed by client", this);
_readyState = WS_STATE_CLOSED; _readyState = STATE_CLOSED;
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
msg->what = WS_MSG_TO_SUBTRHEAD_CLOSING; msg->what = WS_MSG_TO_SUBTRHEAD_CLOSING;
@ -388,21 +388,21 @@ void WebSocket::close()
_delegate->onClose(this); _delegate->onClose(this);
} }
WebSocket::WS_STATE WebSocket::getReadyState() WebSocket::STATE WebSocket::getReadyState()
{ {
return _readyState; return _readyState;
} }
int WebSocket::onSubThreadLoop() int WebSocket::onSubThreadLoop()
{ {
if (_readyState == WS_STATE_CLOSED || _readyState == WS_STATE_CLOSING) if (_readyState == STATE_CLOSED || _readyState == STATE_CLOSING)
{ {
libwebsocket_context_destroy(_wsContext); libwebsocket_context_destroy(_wsContext);
// return 1 to exit the loop. // return 1 to exit the loop.
return 1; return 1;
} }
if (_wsContext && _readyState != WS_STATE_CLOSED && _readyState != WS_STATE_CLOSING) if (_wsContext && _readyState != STATE_CLOSED && _readyState != STATE_CLOSING)
{ {
libwebsocket_service(_wsContext, 0); libwebsocket_service(_wsContext, 0);
} }
@ -442,7 +442,7 @@ void WebSocket::onSubThreadStarted()
_wsContext = libwebsocket_create_context(&info); _wsContext = libwebsocket_create_context(&info);
if(NULL != _wsContext){ if(NULL != _wsContext){
_readyState = WS_STATE_CONNECTING; _readyState = STATE_CONNECTING;
std::string name; std::string name;
for (int i = 0; _wsProtocols[i].callback != NULL; ++i) { for (int i = 0; _wsProtocols[i].callback != NULL; ++i) {
name += (_wsProtocols[i].name); name += (_wsProtocols[i].name);
@ -480,14 +480,14 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
{ {
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == WS_STATE_CONNECTING) || (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == STATE_CONNECTING)
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == WS_STATE_CONNECTING) || (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == STATE_CONNECTING)
) )
{ {
msg->what = WS_MSG_TO_UITHREAD_ERROR; msg->what = WS_MSG_TO_UITHREAD_ERROR;
_readyState = WS_STATE_CLOSING; _readyState = STATE_CLOSING;
} }
else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == WS_STATE_CLOSING) else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == STATE_CLOSING)
{ {
msg->what = WS_MSG_TO_UITHREAD_CLOSE; msg->what = WS_MSG_TO_UITHREAD_CLOSE;
} }
@ -498,7 +498,7 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
{ {
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
msg->what = WS_MSG_TO_UITHREAD_OPEN; msg->what = WS_MSG_TO_UITHREAD_OPEN;
_readyState = WS_STATE_OPEN; _readyState = STATE_OPEN;
/* /*
* start the ball rolling, * start the ball rolling,
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
@ -572,10 +572,10 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
_wsHelper->quitSubThread(); _wsHelper->quitSubThread();
if (_readyState != WS_STATE_CLOSED) if (_readyState != STATE_CLOSED)
{ {
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
_readyState = WS_STATE_CLOSED; _readyState = STATE_CLOSED;
msg->what = WS_MSG_TO_UITHREAD_CLOSE; msg->what = WS_MSG_TO_UITHREAD_CLOSE;
_wsHelper->sendMessageToUIThread(msg); _wsHelper->sendMessageToUIThread(msg);
} }
@ -646,7 +646,7 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
break; break;
case WS_MSG_TO_UITHREAD_ERROR: case WS_MSG_TO_UITHREAD_ERROR:
{ {
WebSocket::WS_ERROR err = WS_ERROR_CONNECTION_FAILS; WebSocket::ERROR err = ERROR_CONNECTION_FAILS;
_delegate->onError(this, err); _delegate->onError(this, err);
} }
break; break;

View File

@ -57,11 +57,11 @@ public:
/** /**
* @brief Errors in websocket * @brief Errors in websocket
*/ */
enum WS_ERROR enum ERROR
{ {
WS_ERROR_TIMEOUT=0, ERROR_TIMEOUT = 0,
WS_ERROR_CONNECTION_FAILS, ERROR_CONNECTION_FAILS,
WS_ERROR_UNKNOWN ERROR_UNKNOWN
}; };
/** /**
@ -74,7 +74,7 @@ public:
virtual void onOpen(WebSocket* ws) = 0; virtual void onOpen(WebSocket* ws) = 0;
virtual void onMessage(WebSocket* ws, const Data& data) = 0; virtual void onMessage(WebSocket* ws, const Data& data) = 0;
virtual void onClose(WebSocket* ws) = 0; virtual void onClose(WebSocket* ws) = 0;
virtual void onError(WebSocket* ws, const WS_ERROR& error) = 0; virtual void onError(WebSocket* ws, const ERROR& error) = 0;
}; };
@ -107,18 +107,18 @@ public:
/** /**
* Websocket state * Websocket state
*/ */
enum WS_STATE enum STATE
{ {
WS_STATE_CONNECTING = 0, STATE_CONNECTING = 0,
WS_STATE_OPEN, STATE_OPEN,
WS_STATE_CLOSING, STATE_CLOSING,
WS_STATE_CLOSED STATE_CLOSED
}; };
/** /**
* @brief Gets current state of connection. * @brief Gets current state of connection.
*/ */
WS_STATE getReadyState(); STATE getReadyState();
private: private:
virtual void onSubThreadStarted(); virtual void onSubThreadStarted();
@ -134,7 +134,7 @@ private:
void *user, void *in, size_t len); void *user, void *in, size_t len);
private: private:
WS_STATE _readyState; STATE _readyState;
std::string _host; std::string _host;
unsigned int _port; unsigned int _port;
std::string _path; std::string _path;

View File

@ -180,7 +180,7 @@ void WebSocketTestLayer::onClose(cocos2d::extension::WebSocket* ws)
CC_SAFE_DELETE(ws); CC_SAFE_DELETE(ws);
} }
void WebSocketTestLayer::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::WS_ERROR& error) void WebSocketTestLayer::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ERROR& error)
{ {
CCLog("Error was fired, error code: %d", error); CCLog("Error was fired, error code: %d", error);
if (ws == _wsiError) if (ws == _wsiError)
@ -201,7 +201,7 @@ void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::CCObject *sender)
// Menu Callbacks // Menu Callbacks
void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender) void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender)
{ {
if (_wsiSendText->getReadyState() == WebSocket::WS_STATE_OPEN) if (_wsiSendText->getReadyState() == WebSocket::STATE_OPEN)
{ {
_sendTextStatus->setString("Send Text WS is waiting..."); _sendTextStatus->setString("Send Text WS is waiting...");
_wsiSendText->send("Hello WebSocket, I'm a text message."); _wsiSendText->send("Hello WebSocket, I'm a text message.");
@ -216,7 +216,7 @@ void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender)
void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::CCObject *sender) void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::CCObject *sender)
{ {
if (_wsiSendBinary->getReadyState() == WebSocket::WS_STATE_OPEN) if (_wsiSendBinary->getReadyState() == WebSocket::STATE_OPEN)
{ {
_sendBinaryStatus->setString("Send Binary WS is waiting..."); _sendBinaryStatus->setString("Send Binary WS is waiting...");
char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0."; char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";

View File

@ -24,7 +24,7 @@ public:
virtual void onOpen(cocos2d::extension::WebSocket* ws); virtual void onOpen(cocos2d::extension::WebSocket* ws);
virtual void onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data); virtual void onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data);
virtual void onClose(cocos2d::extension::WebSocket* ws); virtual void onClose(cocos2d::extension::WebSocket* ws);
virtual void onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::WS_ERROR& error); virtual void onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ERROR& error);
void toExtensionsMainLayer(cocos2d::CCObject *sender); void toExtensionsMainLayer(cocos2d::CCObject *sender);

View File

@ -129,7 +129,7 @@ public:
CC_SAFE_DELETE(ws); CC_SAFE_DELETE(ws);
} }
virtual void onError(WebSocket* ws, const WebSocket::WS_ERROR& error) virtual void onError(WebSocket* ws, const WebSocket::ERROR& error)
{ {
js_proxy_t * p; js_proxy_t * p;
JS_GET_PROXY(p, ws); JS_GET_PROXY(p, ws);
@ -371,13 +371,13 @@ void register_jsb_websocket(JSContext *cx, JSObject *global) {
JSObject* jsclassObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return WebSocket; })()")); JSObject* jsclassObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return WebSocket; })()"));
JS_DefineProperty(cx, jsclassObj, "CONNECTING", INT_TO_JSVAL((int)WebSocket::WS_STATE_CONNECTING) JS_DefineProperty(cx, jsclassObj, "CONNECTING", INT_TO_JSVAL((int)WebSocket::STATE_CONNECTING)
, NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
JS_DefineProperty(cx, jsclassObj, "OPEN", INT_TO_JSVAL((int)WebSocket::WS_STATE_OPEN) JS_DefineProperty(cx, jsclassObj, "OPEN", INT_TO_JSVAL((int)WebSocket::STATE_OPEN)
, NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
JS_DefineProperty(cx, jsclassObj, "CLOSING", INT_TO_JSVAL((int)WebSocket::WS_STATE_CLOSING) JS_DefineProperty(cx, jsclassObj, "CLOSING", INT_TO_JSVAL((int)WebSocket::STATE_CLOSING)
, NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
JS_DefineProperty(cx, jsclassObj, "CLOSED", INT_TO_JSVAL((int)WebSocket::WS_STATE_CLOSED) JS_DefineProperty(cx, jsclassObj, "CLOSED", INT_TO_JSVAL((int)WebSocket::STATE_CLOSED)
, NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
// make the class enumerable in the registered namespace // make the class enumerable in the registered namespace