Skip to content

Instantly share code, notes, and snippets.

@robotconscience
Created September 30, 2014 16:52
Show Gist options
  • Save robotconscience/4e36e9174c8869c55b5e to your computer and use it in GitHub Desktop.
Save robotconscience/4e36e9174c8869c55b5e to your computer and use it in GitHub Desktop.
Start of connecting ofxLibwebsockets with socket.io. Not finished and probably won't ever be...
#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){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment