//-------------------------------------------------------------- void ofApp::onMessage( ofxLibwebsockets::Event& args ){ cout<<"got message "<<args.message<<endl; try{ // trace out string messages or JSON messages! if ( !args.json.isNull() ){ ofPoint point = ofPoint( args.json["point"]["x"].asFloat(), args.json["point"]["y"].asFloat() ); // for some reason these come across as strings via JSON.stringify! int r = ofToInt(args.json["color"]["r"].asString()); int g = ofToInt(args.json["color"]["g"].asString()); int b = ofToInt(args.json["color"]["b"].asString()); ofColor color = ofColor( r, g, b ); int _id = ofToInt(args.json["id"].asString()); map<int, Drawing*>::const_iterator it = drawings.find(_id); Drawing * d = it->second; d->addPoint(point); } else { } // send all that drawing back to everybody except this one vector<ofxLibwebsockets::Connection *> connections = server.getConnections(); for ( int i=0; i<connections.size(); i++){ if ( (*connections[i]) != args.conn ){ connections[i]->send( args.message ); } } } catch(exception& e){ ofLogError() << e.what(); } }
//-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ ofPoint p(x,y); map<int, Drawing*>::iterator it = drawings.find(0); Drawing * d = it->second; d->addPoint(p); server.send( "{\"id\":-1,\"point\":{\"x\":\""+ ofToString(x)+"\",\"y\":\""+ofToString(y)+"\"}," + d->getColorJSON() +"}"); }
//-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ ofPoint p(x,y); map<int, Drawing*>::iterator it = drawings.find(id); if ( it == drawings.end() ) return; Drawing * d = it->second; d->addPoint(p); client.send( "{\"id\":"+ ofToString(id) + ",\"point\":{\"x\":\""+ ofToString(x)+"\",\"y\":\""+ofToString(y)+"\"}," + d->getColorJSON() +"}"); }
//-------------------------------------------------------------- void ofApp::onMessage( ofxLibwebsockets::Event& args ){ try{ cout<<"got message "<<args.message<<endl; // trace out string messages or JSON messages! if ( !args.json.isNull() ){ if (!args.json["setup"].isNull()){ Drawing * d = new Drawing(); d->_id = args.json["setup"]["id"].asInt(); // for some reason these come across as strings via JSON.stringify! int r = ofToInt(args.json["setup"]["color"]["r"].asString()); int g = ofToInt(args.json["setup"]["color"]["g"].asString()); int b = ofToInt(args.json["setup"]["color"]["b"].asString()); d->color.set(r, g, b); drawings.insert( make_pair( d->_id, d )); id = d->_id; color.set(r, g, b); cout << "setup with id:" << id << endl; } else if (args.json["id"].asInt() != id){ cout << "received point" << endl; ofPoint point = ofPoint( args.json["point"]["x"].asFloat(), args.json["point"]["y"].asFloat() ); // for some reason these come across as strings via JSON.stringify! int r = ofToInt(args.json["color"]["r"].asString()); int g = ofToInt(args.json["color"]["g"].asString()); int b = ofToInt(args.json["color"]["b"].asString()); ofColor color = ofColor( r, g, b ); int _id = args.json["id"].asInt(); map<int, Drawing*>::const_iterator it = drawings.find(_id); Drawing * d; if (it!=drawings.end()){ d = it->second; } else { d = new Drawing(); d->_id = _id; // for some reason these come across as strings via JSON.stringify! int r = ofToInt(args.json["color"]["r"].asString()); int g = ofToInt(args.json["color"]["g"].asString()); int b = ofToInt(args.json["color"]["b"].asString()); d->color.set(r, g, b); drawings.insert( make_pair( d->_id, d )); cout << "new drawing with id:" << _id << endl; } d->addPoint(point); } } else { } } catch(exception& e){ ofLogError() << e.what(); } }