diff --git a/extensions/network/WebSocket.cpp b/extensions/network/WebSocket.cpp index 844fd9ed99..a2a2516620 100644 --- a/extensions/network/WebSocket.cpp +++ b/extensions/network/WebSocket.cpp @@ -225,7 +225,7 @@ enum WS_MSG { }; WebSocket::WebSocket() -: _readyState(WS_STATE_CONNECTING) +: _readyState(STATE_CONNECTING) , _port(80) , _wsHelper(NULL) , _wsInstance(NULL) @@ -335,7 +335,7 @@ bool WebSocket::init(const Delegate& delegate, void WebSocket::send(const std::string& message) { - if (_readyState == WS_STATE_OPEN) + if (_readyState == STATE_OPEN) { // In main thread 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."); - if (_readyState == WS_STATE_OPEN) + if (_readyState == STATE_OPEN) { // In main thread WsMessage* msg = new WsMessage(); @@ -371,11 +371,11 @@ void WebSocket::close() { CCDirector::sharedDirector()->getScheduler()->unscheduleAllForTarget(_wsHelper); - if (_readyState == WS_STATE_CLOSING || _readyState == WS_STATE_CLOSED) + if (_readyState == STATE_CLOSING || _readyState == STATE_CLOSED) return; CCLOG("websocket (%p) connection closed by client", this); - _readyState = WS_STATE_CLOSED; + _readyState = STATE_CLOSED; WsMessage* msg = new WsMessage(); msg->what = WS_MSG_TO_SUBTRHEAD_CLOSING; @@ -388,21 +388,21 @@ void WebSocket::close() _delegate->onClose(this); } -WebSocket::WS_STATE WebSocket::getReadyState() +WebSocket::STATE WebSocket::getReadyState() { return _readyState; } int WebSocket::onSubThreadLoop() { - if (_readyState == WS_STATE_CLOSED || _readyState == WS_STATE_CLOSING) + if (_readyState == STATE_CLOSED || _readyState == STATE_CLOSING) { libwebsocket_context_destroy(_wsContext); // return 1 to exit the loop. return 1; } - if (_wsContext && _readyState != WS_STATE_CLOSED && _readyState != WS_STATE_CLOSING) + if (_wsContext && _readyState != STATE_CLOSED && _readyState != STATE_CLOSING) { libwebsocket_service(_wsContext, 0); } @@ -442,7 +442,7 @@ void WebSocket::onSubThreadStarted() _wsContext = libwebsocket_create_context(&info); if(NULL != _wsContext){ - _readyState = WS_STATE_CONNECTING; + _readyState = STATE_CONNECTING; std::string name; for (int i = 0; _wsProtocols[i].callback != NULL; ++i) { name += (_wsProtocols[i].name); @@ -480,14 +480,14 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx, { WsMessage* msg = new WsMessage(); if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR - || (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == WS_STATE_CONNECTING) - || (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == WS_STATE_CONNECTING) + || (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == STATE_CONNECTING) + || (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == STATE_CONNECTING) ) { 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; } @@ -498,7 +498,7 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx, { WsMessage* msg = new WsMessage(); msg->what = WS_MSG_TO_UITHREAD_OPEN; - _readyState = WS_STATE_OPEN; + _readyState = STATE_OPEN; /* * start the ball rolling, * LWS_CALLBACK_CLIENT_WRITEABLE will come next service @@ -572,10 +572,10 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx, _wsHelper->quitSubThread(); - if (_readyState != WS_STATE_CLOSED) + if (_readyState != STATE_CLOSED) { WsMessage* msg = new WsMessage(); - _readyState = WS_STATE_CLOSED; + _readyState = STATE_CLOSED; msg->what = WS_MSG_TO_UITHREAD_CLOSE; _wsHelper->sendMessageToUIThread(msg); } @@ -646,7 +646,7 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg) break; case WS_MSG_TO_UITHREAD_ERROR: { - WebSocket::WS_ERROR err = WS_ERROR_CONNECTION_FAILS; + WebSocket::ERROR err = ERROR_CONNECTION_FAILS; _delegate->onError(this, err); } break; diff --git a/extensions/network/WebSocket.h b/extensions/network/WebSocket.h index 021e4e4395..f7698fff60 100644 --- a/extensions/network/WebSocket.h +++ b/extensions/network/WebSocket.h @@ -57,11 +57,11 @@ public: /** * @brief Errors in websocket */ - enum WS_ERROR + enum ERROR { - WS_ERROR_TIMEOUT=0, - WS_ERROR_CONNECTION_FAILS, - WS_ERROR_UNKNOWN + ERROR_TIMEOUT = 0, + ERROR_CONNECTION_FAILS, + ERROR_UNKNOWN }; /** @@ -74,7 +74,7 @@ public: virtual void onOpen(WebSocket* ws) = 0; virtual void onMessage(WebSocket* ws, const Data& data) = 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 */ - enum WS_STATE + enum STATE { - WS_STATE_CONNECTING = 0, - WS_STATE_OPEN, - WS_STATE_CLOSING, - WS_STATE_CLOSED + STATE_CONNECTING = 0, + STATE_OPEN, + STATE_CLOSING, + STATE_CLOSED }; /** * @brief Gets current state of connection. */ - WS_STATE getReadyState(); + STATE getReadyState(); private: virtual void onSubThreadStarted(); @@ -134,7 +134,7 @@ private: void *user, void *in, size_t len); private: - WS_STATE _readyState; + STATE _readyState; std::string _host; unsigned int _port; std::string _path; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp index a06681e71a..e80a0a8dae 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp @@ -180,7 +180,7 @@ void WebSocketTestLayer::onClose(cocos2d::extension::WebSocket* 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); if (ws == _wsiError) @@ -201,7 +201,7 @@ void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::CCObject *sender) // Menu Callbacks 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..."); _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) { - if (_wsiSendBinary->getReadyState() == WebSocket::WS_STATE_OPEN) + if (_wsiSendBinary->getReadyState() == WebSocket::STATE_OPEN) { _sendBinaryStatus->setString("Send Binary WS is waiting..."); char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0."; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h index 1070ce750a..dd4d1991cd 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h @@ -24,7 +24,7 @@ public: virtual void onOpen(cocos2d::extension::WebSocket* ws); virtual void onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data); 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); diff --git a/scripting/javascript/bindings/jsb_websocket.cpp b/scripting/javascript/bindings/jsb_websocket.cpp index 5768be58eb..eea1ad41ff 100644 --- a/scripting/javascript/bindings/jsb_websocket.cpp +++ b/scripting/javascript/bindings/jsb_websocket.cpp @@ -129,7 +129,7 @@ public: 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_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; })()")); - 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); - 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); - 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); - 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); // make the class enumerable in the registered namespace