Interface IEventListener

  • Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface IEventListener
    The interface can be implemented to handle a single event type or it can be used to capture multiple events. The example below shows how to create separate event handlers.
     
     class ConnectionHandler implements IEventListener
     {
                    void dispatch(BaseEvent evt) throws SFSException
                    {
                            // connection event code goes here
                    }
     }
     
     class LoginHandler implements IEventListener
     {
                    void dispatch(BaseEvent evt) throws SFSException
                    {
                            // login event code goes here
                    }
     }
     
     
     SmartFox sfs = new SmartFox(),
     sfs.addEventListener(SFSEvent.CONNECTION, new ConnectionHandler());
     sfs.addEventListener(SFSEvent.LOGIN, new LoginHandler());
     
     

    The following example demonstrates how to use a single implementation to deal with multiple events. class EventHandler implements IEventListener { void dispatch(BaseEvent evt) throws SFSException { if (evt.getType().equals(SFSEvent.CONNECTION)) { // connection event code goes here } else if (evt.getType().equals(SFSEvent.LOGIN)) { // login event code goes here } else if ... } } SmartFox sfs = new SmartFox(), sfs.addEventListener(SFSEvent.CONNECTION, new ConnectionHandler()); sfs.addEventListener(SFSEvent.LOGIN, new LoginHandler());

    See Also:
    SFSEvent, SmartFox.addEventListener(String, IEventListener)
    • Method Detail

      • dispatch

        void dispatch​(BaseEvent evt)
               throws com.smartfoxserver.v2.exceptions.SFSException
        The method is invoked when the Event is dispatched.
        Parameters:
        evt - The event object, containing one or more parameters
        Throws:
        com.smartfoxserver.v2.exceptions.SFSException - Thrown when an error occurs in the event handler
        See Also:
        BaseEvent, SFSEvent