bool js_cocos2dx_SocketIO_emit(JSContext* cx, uint32_t argc, jsval* vp) { CCLOG("JSB SocketIO.emit method called"); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JSObject *obj = JS_THIS_OBJECT(cx, vp); js_proxy_t *proxy = jsb_get_js_proxy(obj); SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL); JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object"); if (argc == 2) { std::string eventName; do { bool ok = jsval_to_std_string(cx, args.get(0), &eventName); JSB_PRECONDITION2( ok, cx, false, "Error processing arguments"); } while (0); std::string payload; do { bool ok = jsval_to_std_string(cx, args.get(1), &payload); JSB_PRECONDITION2( ok, cx, false, "Error processing arguments"); } while (0); CCLOG("JSB SocketIO emit event '%s' with payload: %s", eventName.c_str(), payload.c_str()); cobj->emit(eventName, payload); return true; } JS_ReportError(cx, "JSB SocketIO.emit: Wrong number of arguments"); return false; }
int main(int argc, char* argv[]) { //create a c++ Poco logger to use and set its channel to the windows console //this is the same logger instance that the library will hook into Logger *logger = &(Logger::get("SIOClientLog")); #ifdef _WIN64 //define something for Windows (64-bit) logger->setChannel(new Poco::WindowsConsoleChannel()); #elif _WIN32 //define something for Windows (32-bit) logger->setChannel(new Poco::WindowsConsoleChannel()); #elif __APPLE__ #if TARGET_IPHONE_SIMULATOR // iOS Simulator #elif TARGET_OS_IPHONE // iOS device #elif TARGET_OS_MAC // Other kinds of Mac OS #else // Unsupported platform #endif #elif __linux // linux logger->setChannel(new Poco::ConsoleChannel()); #elif __unix // all unices not caught above // Unix logger->setChannel(new Poco::ConsoleChannel()); #elif __posix // POSIX logger->setChannel(new Poco::ConsoleChannel()); #endif //Establish the socket.io connection //JS: var socket = io.connect("localhost:3000") SIOClient *sio = SIOClient::connect("http://localhost:3000"); //Create a target and register object its method onUpdate for the Update event //JS: socket.on("Update", function(data) {...}); TestTarget *target = new TestTarget(); sio->on("Update", target, callback(&TestTarget::onUpdate)); //setup is now complete, messages and events can be send and received logger->information("sio setup complete\n"); //test the message sending logger->information("sending message\n"); sio->send("Hello Socket.IO"); //test the event emitter, this will return the same event so let's register a callback too sio->on("testevent", target, callback(&TestTarget::ontestevent)); logger->information("emitting test event\n"); sio->emit("testevent", "[{\"name\":\"myname\",\"type\":\"mytype\"}]"); //test connecting to an endpoint 'testpoint' TestEndpointTarget *target2 = new TestEndpointTarget(); SIOClient *testpoint = SIOClient::connect("http://localhost:3000/testpoint"); testpoint->send("Hello Socket.IO Testpoint"); testpoint->on("Update", target2, callback(&TestEndpointTarget::onUpdate)); testpoint->on("testevent", target2, callback(&TestEndpointTarget::ontestevent)); testpoint->emit("testevent", "[{\"name\":\"myname\",\"type\":\"mytype\"}]"); //wait for user input to move to next section of code //socket receiving occurs in another thread and will not be halted logger->information("Press ENTER to continue..."); std::cin.get(); //test disconnecting a single endpoint, other endpoints stay connected testpoint->disconnect(); //disconnecting the default socket with no endpoint will also disconnect all endpoints sio->disconnect(); logger->information("Press ENTER to quit..."); std::cin.get(); return 0; }