Package org.zkoss.zkmax.au.websocket

## Class WebSocketServerPush

- java.lang.Object
- - org.zkoss.zkmax.au.websocket.WebSocketServerPush

- All Implemented Interfaces:
    `ServerPush`

      public class WebSocketServerPush
      extends java.lang.Object
      implements ServerPush

  A server-push implementation that is based on WebSocket. This is the default server-push class when WebSocket connection is enabled. To enable WebSocket connection, please refer to `WebSocketWebAppInit` for more information.
  Since:
    8.5.0
  Author:
    wenninghsu

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `WebSocketServerPush()` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `boolean` | `activate​(long timeout)` | Activate the current thread (which must be a server-push thread).
    `boolean` | `deactivate​(boolean stop)` | Deactivates the current thread (which must be a server-push thread).
    `boolean` | `isActive()` | Returns if the working thread of the server push is active.
    `void` | `onPiggyback()` | Called when `Events.ON_PIGGYBACK` is received.
    `void` | `resume()` | Resumes server-push - this is required after desktop recycling.
    `<T extends Event> void` | `schedule​(EventListener<T> listener, T event, Scheduler<T> scheduler)` | Called by the associated desktop to schedule a task to execute asynchronously.
    `void` | `setReady​(boolean ready)` |
    `protected void` | `setWebSocketConnectionInfos​(WebSocketEndPoint webSocketEndpoint, javax.servlet.http.HttpServletRequest request)` |
    `void` | `start​(Desktop desktop)` | Starts and initializes the server-push controller.
    `protected void` | `startClientPush()` | Sends an AU response to the client to start the server push.
    `void` | `stop()` | Stops and cleans up the server-push controller.
    `protected void` | `wakePending()` |

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### WebSocketServerPush

          public WebSocketServerPush()

  - ### Method Detail

    - #### setReady

          public void setReady​(boolean ready)

    - #### setWebSocketConnectionInfos

          protected void setWebSocketConnectionInfos​(WebSocketEndPoint webSocketEndpoint,
                                                     javax.servlet.http.HttpServletRequest request)

    - #### isActive

          public boolean isActive()

      Description copied from interface: `ServerPush`
      Returns if the working thread of the server push is active. In other words, it is true if it is between `Executions.activate(org.zkoss.zk.ui.Desktop)` and `Executions.deactivate(org.zkoss.zk.ui.Desktop)`.
      Specified by:
        `isActive` in interface `ServerPush`

    - #### start

          public void start​(Desktop desktop)

      Description copied from interface: `ServerPush`
      Starts and initializes the server-push controller. One server-push controller is associated with exactly one desktop.

      `ServerPush.start(org.zkoss.zk.ui.Desktop)` is called when `Desktop.enableServerPush(boolean)` is called to enable the server-push feature for the specified desktop.

      Specified by:
        `start` in interface `ServerPush`

    - #### stop

          public void stop()

      Description copied from interface: `ServerPush`
      Stops and cleans up the server-push controller.

      `ServerPush.stop()` is called when `Desktop.enableServerPush(boolean)` is called to disable the server-push feature, or when the desktop is being removed.

      Specified by:
        `stop` in interface `ServerPush`

    - #### resume

          public void resume()

      Description copied from interface: `ServerPush`
      Resumes server-push - this is required after desktop recycling. (added ServerPush interface to fix ZK-1777)

      `ServerPush.resume()` is called when `UiEngine.recycleDesktop(org.zkoss.zk.ui.Execution, org.zkoss.zk.ui.Page, java.io.Writer)` is called to resume a previously enabled server-push on the recycled desktop.

      Specified by:
        `resume` in interface `ServerPush`

    - #### startClientPush

          protected void startClientPush()

      Sends an AU response to the client to start the server push. It is called by `start(org.zkoss.zk.ui.Desktop)`.

      The derived class usually overrides this method to support different devices.

      The default implementation is to send an `AuScript` response.

    - #### schedule

          public <T extends Event> void schedule​(EventListener<T> listener,
                                                 T event,
                                                 Scheduler<T> scheduler)

      Description copied from interface: `ServerPush`
      Called by the associated desktop to schedule a task to execute asynchronously.

      The implementation usually delegates the scheduling to the scheduler passed as the third argument, which is controlled by the desktop. Of course, it could schedule it by itself.

      Specified by:
        `schedule` in interface `ServerPush`
      Parameters:
        `listener` - the task to execute
        `event` - the event to be passed to the task (i.e., the event listener). It could null or any instance as long as the task recognizes it.
        `scheduler` - the default scheduler to schedule the task. The implementation usually delegates the scheduling back to it. If you prefer to handle it by yourself, you could ignore it.

    - #### activate

          public boolean activate​(long timeout)
                           throws java.lang.InterruptedException,
                                  DesktopUnavailableException

      Description copied from interface: `ServerPush`
      Activate the current thread (which must be a server-push thread). It causes the current thread to wait until the desktop is available to access, the desktop no longer exists, some other thread interrupts this thread, or a certain amount of real time has elapsed.

      The invoker of this method must invoke `ServerPush.deactivate(boolean)` in this finally clause.

      Note: the activation is applied to the desktop that was assigned by `ServerPush.start(org.zkoss.zk.ui.Desktop)`.

      Unlike `ServerPush.onPiggyback()`, this method is NOT called in the context of an event listener. Rather, it is called in the thread of a server-push thread.

      Specified by:
        `activate` in interface `ServerPush`
      Parameters:
        `timeout` - the maximum time to wait in milliseconds. Ignored (i.e., never timeout) if non-positive.
      Returns:
        whether it is activated or it is timeout. The only reason it returns false is timeout.
      Throws:
        `java.lang.InterruptedException` - if it is interrupted by other thread
        `DesktopUnavailableException` - if the desktop is removed (when activating).

    - #### deactivate

          public boolean deactivate​(boolean stop)

      Description copied from interface: `ServerPush`
      Deactivates the current thread (which must be a server-push thread).
      Specified by:
        `deactivate` in interface `ServerPush`
      Parameters:
        `stop` - whether to stop after deactivate
      Returns:
        true if it is stopped
      See Also:
        `ServerPush.activate(long)`

    - #### wakePending

          protected void wakePending()

    - #### onPiggyback

          public void onPiggyback()

      Description copied from interface: `ServerPush`
      Called when `Events.ON_PIGGYBACK` is received. The invocation is passive (i.e., triggered by other events, rather than spontaneous).

      This method is called in the context of an event listener. In other words, the execution is activated and you can retrieve it by `Executions.getCurrent()`.

      Specified by:
        `onPiggyback` in interface `ServerPush`