Created
September 30, 2014 16:52
-
-
Save robotconscience/4e36e9174c8869c55b5e to your computer and use it in GitHub Desktop.
Revisions
-
robotconscience created this gist
Sep 30, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,149 @@ #include "ofApp.h" string sid = ""; //-------------------------------------------------------------- void ofApp::setup(){ ofSetLogLevel(OF_LOG_VERBOSE); ofxLibwebsockets::ClientOptions opts = ofxLibwebsockets::defaultClientOptions(); opts.channel = "/socket.io/1/websocket?EIO=2&transport=websocket&sid="; opts.port = 3000; client.connect(opts); // client.connect("echo.websocket.org", true); // optionally use SSL ofSetLogLevel(OF_LOG_ERROR); client.addListener(this); ofSetFrameRate(60); } //-------------------------------------------------------------- void ofApp::update(){ } //-------------------------------------------------------------- void ofApp::draw(){ } //-------------------------------------------------------------- void ofApp::onConnect( ofxLibwebsockets::Event& args ){ cout<<"on connected"<<endl; } //-------------------------------------------------------------- void ofApp::onOpen( ofxLibwebsockets::Event& args ){ cout<<"on open"<<endl; } //-------------------------------------------------------------- void ofApp::onClose( ofxLibwebsockets::Event& args ){ cout<<"on close"<<endl; } //-------------------------------------------------------------- void ofApp::onIdle( ofxLibwebsockets::Event& args ){ cout<<"on idle"<<endl; } //-------------------------------------------------------------- void ofApp::onMessage( ofxLibwebsockets::Event& args ){ cout<<"got message "<<args.message<<endl; // control is first char of message int control = ofToInt(args.message.substr(0,1)); bool bGoodJson = false; // messages with len > 1 have some sort of data if ( args.message.length() > 1 ){ string m = args.message.substr(1); static Json::Reader reader; reader.parse(m, args.json); bGoodJson = !args.json.isNull(); } if ( bGoodJson ){ switch (control) { case 0: if ( args.json["sid"] != NULL ) { sid = args.json["sid"].asString(); cout << sid << endl; } break; case 1: //?? break; case 2: { //Ping->Pong stringstream out; out<<"3:"<<args.json; client.send(out.str()); } break; case 3: // message break; case 4: // control event break; case 5: //Upgrade Required break; case 6: //Noop break; default: break; } } } //-------------------------------------------------------------- void ofApp::onBroadcast( ofxLibwebsockets::Event& args ){ cout<<"got broadcast "<<args.message<<endl; } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ client.send("{\"type\":\"message\", \"data\":\"Hello\"}"); cout << "sending hello" <<endl; } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }