You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by hn...@apache.org on 2020/02/13 14:17:45 UTC

[myfaces-tobago] branch master updated: add WebSocket example for JSF-2.3

This is an automated email from the ASF dual-hosted git repository.

hnoeth pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new 31c1cdc  add WebSocket example for JSF-2.3
31c1cdc is described below

commit 31c1cdc075065d46fe9bfe9173a0e019e95585a3
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Thu Feb 13 15:04:05 2020 +0100

    add WebSocket example for JSF-2.3
---
 .../tobago/example/demo/WebSocketController.java   | 69 ++++++++++++++++++++++
 .../src/main/webapp/WEB-INF/tobago-config.xml      |  3 +
 .../content/30-concept/18-websocket/WebSocket.js   | 20 +++++++
 .../30-concept/18-websocket/WebSocket.xhtml        | 50 ++++++++++++++++
 4 files changed, 142 insertions(+)

diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/WebSocketController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/WebSocketController.java
new file mode 100644
index 0000000..5cf8a75
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/WebSocketController.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.myfaces.tobago.example.demo;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.enterprise.context.SessionScoped;
+//import javax.faces.push.Push;
+//import javax.faces.push.PushContext;
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.io.Serializable;
+import java.lang.invoke.MethodHandles;
+import java.time.LocalTime;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@SessionScoped
+@Named
+public class WebSocketController implements Serializable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private ScheduledExecutorService scheduledExecutorService;
+
+  @Inject
+//  @Push(channel = "clock")
+//  private PushContext push;
+
+  public String startClock() {
+    if (scheduledExecutorService == null || scheduledExecutorService.isShutdown()) {
+      scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
+    }
+
+    scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
+      @Override
+      public void run() {
+//        push.send(LocalTime.now().toString());
+      }
+    }, 0, 17, TimeUnit.MILLISECONDS);
+    return null;
+  }
+
+  public String stopClock() {
+    if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) {
+      scheduledExecutorService.shutdown();
+    }
+    return null;
+  }
+}
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml
index d6075af..6893d91 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml
@@ -48,6 +48,9 @@
     <!-- XXX With CSP Tobago 3.0.x is currently not working 100% see TOBAGO-1534,
          XXX because of JSF-AJAX we need 'unsafe-eval' -->
     <!--<directive name="script-src">'unsafe-eval'</directive>-->
+
+    <!-- script-src: 'unsafe-inline' is required for WebSockets -->
+<!--    <directive name="script-src">'unsafe-inline'</directive>-->
     <!-- needed for <tc:object>  -->
     <directive name="child-src">https://www.openstreetmap.org</directive>
     <directive name="child-src">https://*.apache.org</directive>
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.js b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.js
new file mode 100644
index 0000000..a509a7c
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.js
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+function websocketListener(message, channel, event) {
+  document.getElementById("clockId").innerHTML = message + "<br/>";
+}
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.xhtml
new file mode 100644
index 0000000..e57a694
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.xhtml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+-->
+
+<ui:composition template="/main.xhtml"
+                xmlns="http://www.w3.org/1999/xhtml"
+                xmlns:f="http://java.sun.com/jsf/core"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="http://java.sun.com/jsf/facelets">
+  <p>Simpe example for WebSockets.</p>
+
+  <tc:section label="Example">
+    <p><tc:badge value="Warning" markup="warning"/>WebSockets are available since JSF 2.3</p>
+    <p><tc:badge value="Warning" markup="warning"/>CSP script-src: 'unsafe-inline' is needed.</p>
+    <p><tc:badge value="Warning" markup="warning"/>
+      WebSocket.xhtml: The comment for <code>&lt;f:websocket/></code> must be removed.</p>
+    <p><tc:badge value="Warning" markup="warning"/>
+      WebSocketController.java: The comment for 'push' and 'pushContext' must be removed.</p>
+
+    <tc:script file="#{request.contextPath}/content/30-concept/18-websocket/WebSocket.js"/>
+    <!--<f:websocket channel="clock" onmessage="websocketListener"/>-->
+
+    <tc:buttons>
+      <tc:button label="Start Clock" action="#{webSocketController.startClock}">
+        <f:ajax/>
+      </tc:button>
+      <tc:button label="Stop Clock" action="#{webSocketController.stopClock}">
+        <f:ajax/>
+      </tc:button>
+    </tc:buttons>
+
+    <tc:separator/>
+    <div id="clockId"></div>
+  </tc:section>
+</ui:composition>