projets:2018:stagejava
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| projets:2018:stagejava [2019/03/02 14:33] – christian.jacolot | projets:2018:stagejava [2024/04/16 22:26] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 531: | Ligne 531: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | |||
| + | class ChatWebsocket.java | ||
| + | < | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | package net.mdl29.websocket; | ||
| + | |||
| + | import java.io.IOException; | ||
| + | import java.util.Set; | ||
| + | import java.util.concurrent.CopyOnWriteArraySet; | ||
| + | import java.util.concurrent.atomic.AtomicInteger; | ||
| + | |||
| + | import javax.websocket.OnClose; | ||
| + | import javax.websocket.OnError; | ||
| + | import javax.websocket.OnMessage; | ||
| + | import javax.websocket.OnOpen; | ||
| + | import javax.websocket.Session; | ||
| + | import javax.websocket.server.ServerEndpoint; | ||
| + | |||
| + | @ServerEndpoint(value = "/ | ||
| + | public class ChatWebsocket { | ||
| + | |||
| + | private static final String GUEST_PREFIX = " | ||
| + | private static final AtomicInteger connectionIds = new AtomicInteger(0); | ||
| + | private static final Set< | ||
| + | new CopyOnWriteArraySet<> | ||
| + | |||
| + | private final String nickname; | ||
| + | private Session session; | ||
| + | |||
| + | public ChatWebsocket() { | ||
| + | nickname = GUEST_PREFIX + connectionIds.getAndIncrement(); | ||
| + | } | ||
| + | |||
| + | |||
| + | @OnOpen | ||
| + | public void start(Session session) { | ||
| + | this.session = session; | ||
| + | connections.add(this); | ||
| + | String message = String.format(" | ||
| + | broadcast(message); | ||
| + | } | ||
| + | |||
| + | |||
| + | @OnClose | ||
| + | public void end() { | ||
| + | connections.remove(this); | ||
| + | String message = String.format(" | ||
| + | nickname, "has disconnected." | ||
| + | broadcast(message); | ||
| + | } | ||
| + | |||
| + | |||
| + | @OnMessage | ||
| + | public void incoming(String message) { | ||
| + | // Never trust the client | ||
| + | String filteredMessage = String.format(" | ||
| + | nickname, filter(message.toString())); | ||
| + | broadcast(filteredMessage); | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | @OnError | ||
| + | public void onError(Throwable t) throws Throwable { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | |||
| + | private static void broadcast(String msg) { | ||
| + | for (ChatWebsocket client : connections) { | ||
| + | try { | ||
| + | synchronized (client) { | ||
| + | client.session.getBasicRemote().sendText(msg); | ||
| + | } | ||
| + | } catch (IOException e) { | ||
| + | System.out.println(" | ||
| + | connections.remove(client); | ||
| + | try { | ||
| + | client.session.close(); | ||
| + | } catch (IOException e1) { | ||
| + | // Ignore | ||
| + | } | ||
| + | String message = String.format(" | ||
| + | client.nickname, | ||
| + | broadcast(message); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static String filter(String message) { | ||
| + | |||
| + | if (message == null) | ||
| + | return null; | ||
| + | |||
| + | char content[] = new char[message.length()]; | ||
| + | message.getChars(0, | ||
| + | StringBuilder result = new StringBuilder(content.length + 50); | ||
| + | for (int i = 0; i < content.length; | ||
| + | switch (content[i]) { | ||
| + | case '<': | ||
| + | result.append("& | ||
| + | break; | ||
| + | case '>': | ||
| + | result.append("& | ||
| + | break; | ||
| + | case '&': | ||
| + | result.append("& | ||
| + | break; | ||
| + | case '"': | ||
| + | result.append("& | ||
| + | break; | ||
| + | default: | ||
| + | result.append(content[i]); | ||
| + | } | ||
| + | } | ||
| + | return result.toString(); | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Web client chat.xhtml: | ||
| + | < | ||
| + | <?xml version=" | ||
| + | <!-- | ||
| + | Licensed to the Apache Software Foundation (ASF) under one or more | ||
| + | contributor license agreements. | ||
| + | this work for additional information regarding copyright ownership. | ||
| + | The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| + | (the " | ||
| + | the License. | ||
| + | |||
| + | http:// | ||
| + | |||
| + | 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. | ||
| + | --> | ||
| + | <html xmlns=" | ||
| + | < | ||
| + | < | ||
| + | <style type=" | ||
| + | input#chat { | ||
| + | width: 410px | ||
| + | } | ||
| + | |||
| + | # | ||
| + | width: 400px; | ||
| + | } | ||
| + | |||
| + | #console { | ||
| + | border: 1px solid #CCCCCC; | ||
| + | border-right-color: | ||
| + | border-bottom-color: | ||
| + | height: 170px; | ||
| + | overflow-y: scroll; | ||
| + | padding: 5px; | ||
| + | width: 100%; | ||
| + | } | ||
| + | |||
| + | #console p { | ||
| + | padding: 0; | ||
| + | margin: 0; | ||
| + | } | ||
| + | ]]></ | ||
| + | <script type=" | ||
| + | "use strict"; | ||
| + | |||
| + | var Chat = {}; | ||
| + | |||
| + | Chat.socket = null; | ||
| + | |||
| + | Chat.connect = (function(host) { | ||
| + | if (' | ||
| + | Chat.socket = new WebSocket(host); | ||
| + | } else if (' | ||
| + | Chat.socket = new MozWebSocket(host); | ||
| + | } else { | ||
| + | Console.log(' | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | Chat.socket.onopen = function () { | ||
| + | Console.log(' | ||
| + | document.getElementById(' | ||
| + | if (event.keyCode == 13) { | ||
| + | Chat.sendMessage(); | ||
| + | } | ||
| + | }; | ||
| + | }; | ||
| + | |||
| + | Chat.socket.onclose = function () { | ||
| + | document.getElementById(' | ||
| + | Console.log(' | ||
| + | }; | ||
| + | |||
| + | Chat.socket.onmessage = function (message) { | ||
| + | Console.log(message.data); | ||
| + | }; | ||
| + | }); | ||
| + | |||
| + | Chat.initialize = function() { | ||
| + | if (window.location.protocol == ' | ||
| + | Chat.connect(' | ||
| + | } else { | ||
| + | Chat.connect(' | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | Chat.sendMessage = (function() { | ||
| + | var message = document.getElementById(' | ||
| + | if (message != '' | ||
| + | Chat.socket.send(message); | ||
| + | document.getElementById(' | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | var Console = {}; | ||
| + | |||
| + | Console.log = (function(message) { | ||
| + | var console = document.getElementById(' | ||
| + | var p = document.createElement(' | ||
| + | p.style.wordWrap = ' | ||
| + | p.innerHTML = message; | ||
| + | console.appendChild(p); | ||
| + | while (console.childNodes.length > 25) { | ||
| + | console.removeChild(console.firstChild); | ||
| + | } | ||
| + | console.scrollTop = console.scrollHeight; | ||
| + | }); | ||
| + | |||
| + | Chat.initialize(); | ||
| + | |||
| + | |||
| + | document.addEventListener(" | ||
| + | // Remove elements with " | ||
| + | var noscripts = document.getElementsByClassName(" | ||
| + | for (var i = 0; i < noscripts.length; | ||
| + | noscripts[i].parentNode.removeChild(noscripts[i]); | ||
| + | } | ||
| + | }, false); | ||
| + | |||
| + | ]]></ | ||
| + | </ | ||
| + | < | ||
| + | <div class=" | ||
| + | Javascript and reload this page!</ | ||
| + | <div> | ||
| + | <p> | ||
| + | <input type=" | ||
| + | </p> | ||
| + | <div id=" | ||
| + | <div id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | Les sources complets avec pom, html, java: {{ : | ||
| + | |||
projets/2018/stagejava.1551533588.txt.gz · Dernière modification : 2024/04/16 22:26 (modification externe)
