ofNotifyEvent(...) not working from inside while() loop

Hi,

I want an OSC receiver class to notify some listeners when a new message arrives.

I tryied this code

 void myClass::update()
 {
       while (receiver.hasWaitingMessages()) {
            ofxOscMessage m;
            receiver.getNextMessage(m);
            printOscMessage(m);

            ofNotifyEvent(newOscMessageEvent, m, this);
        }
 }

It didn’t work. The message gets printed alright, but the listeners are not notified.
So I tried testing the notification system by notifying a float instead of an ofxOscMessage – like the advancedEventsExample–:

void myClass::update()
{
    counterfortest++;

    while (receiver.hasWaitingMessages()) {
        ofxOscMessage m;
        receiver.getNextMessage(m);
        printOscMessage(m);

        ofNotifyEvent(newFloatEvent, counterfortest, this);
    }
}

Didn’t work either.
Next I tried calling ofNotifyEvent() from outside of the while() loop, like this:

    void myClass::update()
    {
        counterfortest++;

        ofNotifyEvent(newFloatEvent, counterfortest, this);

        while (receiver.hasWaitingMessages()) {
            ofxOscMessage m;
            receiver.getNextMessage(m);
            printOscMessage(m);
        }
    }

which seems to work fine.

Does anybody know why notifications are not happening inside the while() loop? Any hints on how to solve this would be very much appreciated.

Thanks!

Hi,
try this.

    #pragma once

#include "ofMain.h"
#include "ofxOsc.h"

class ofApp : public ofBaseApp{

    
    ofEvent<ofxOscMessage> OSC_EVENT;
    
	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

    ofxOscReceiver _receiver;
    ofxOscSender _sender;
    
    void eventAction(ofxOscMessage &arg);
    
};


    #include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

    _receiver.setup(12345);
    _sender.setup("localhost", 12345);
    
    ofAddListener(OSC_EVENT, this, &ofApp::eventAction);
}

//--------------------------------------------------------------
void ofApp::update(){

    while (_receiver.hasWaitingMessages()) {
        ofxOscMessage m;
        if(_receiver.getNextMessage(m)){
            
            ofNotifyEvent(OSC_EVENT, m , this);
            
        }
    }
}

//-------------------------------------------------------------
void ofApp::draw(){

}
void ofApp::eventAction(ofxOscMessage &arg){

    cout << " hey " <<  arg.getAddress() << endl;
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

    if(key == '2'){
        ofxOscMessage m;
        m.setAddress("/post/");
        m.addStringArg("hey");
        
        _sender.sendMessage(m);
        
    }
}

Thanks @criseg, it works like charm.