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:06:54 UTC
[myfaces-tobago] branch tobago-4.x 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 tobago-4.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git
The following commit(s) were added to refs/heads/tobago-4.x by this push:
new 7ee7627 add WebSocket example for JSF-2.3
7ee7627 is described below
commit 7ee762764dd05f73102279cedc65769b7ccd6ccb
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 ++++++++++++++++++++++
.../apache/myfaces/tobago/example/demo/Demo.xml | 1 +
.../src/main/webapp/WEB-INF/tobago-config.xml | 3 +
.../content/30-concept/18-websocket/WebSocket.js | 20 +++++++
.../30-concept/18-websocket/WebSocket.xhtml | 51 ++++++++++++++++
5 files changed, 144 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/resources/org/apache/myfaces/tobago/example/demo/Demo.xml b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
index e40ff2c..1190e1f 100644
--- a/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
+++ b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
@@ -150,6 +150,7 @@
<entry key="segment_layout">SegmentLayout</entry>
<entry key="labeled_layout">LabeledLayout</entry>
<entry key="grid_layout">GridLayout</entry>
+ <entry key="websocket">WebSocket</entry>
<entry key="format">Formatting</entry>
<entry key="exception_handler">Ajax Exception Handler</entry>
<entry key="transition">Transition</entry>
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 0187999..481a5dc 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..665b335
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/18-websocket/WebSocket.xhtml
@@ -0,0 +1,51 @@
+<?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">
+ <ui:param name="title" value="#{demoBundle.websocket}"/>
+ <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><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>