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édenteProchaine révisionLes deux révisions suivantes | ||
projets:2018:stagejava [2019/01/12 16:37] – christian.jacolot | projets:2018:stagejava [2019/03/02 14:45] – christian.jacolot | ||
---|---|---|---|
Ligne 334: | Ligne 334: | ||
< | < | ||
</ | </ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
</ | </ | ||
Ligne 437: | Ligne 444: | ||
implementation=" | implementation=" | ||
< | < | ||
- | < | + | < |
</ | </ | ||
</ | </ | ||
Ligne 452: | Ligne 459: | ||
</ | </ | ||
+ | pom.xml | ||
+ | < | ||
+ | <project xmlns=" | ||
+ | |||
+ | xmlns: | ||
+ | |||
+ | xsi: | ||
+ | http:// | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | <!-- Dependency needed by the Web-socket --> | ||
+ | <!-- Tomcat has it, so no need to package into the war file --> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | <!-- Used to serialize the message from the browser --> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | <!-- Sevlet jars for compilation, | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | 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(); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
projets/2018/stagejava.txt · Dernière modification : 2024/04/16 22:26 de 127.0.0.1