├── README.md ├── pom.xml └── src └── main ├── java ├── Monitor.java └── com │ └── uptop │ └── websocket │ └── WebSocketTest.java ├── resources └── applicationContext.xml └── webapp ├── WEB-INF └── web.xml └── index.jsp /README.md: -------------------------------------------------------------------------------- 1 | # JavaWebSocket 2 | websockt & spring demo 3 | 欢迎多多start哦,关联CSDN文章:http://blog.csdn.net/gisredevelopment/article/details/38392629 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | me.gacl 5 | JavaWebSocket 6 | war 7 | 1.0-SNAPSHOT 8 | JavaWebSocket Maven Webapp 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | 4.0.2.RELEASE 15 | 16 | 17 | 18 | 19 | javax 20 | javaee-api 21 | 7.0 22 | 23 | 24 | 25 | 26 | org.springframework 27 | spring-core 28 | ${spring.version} 29 | 30 | 31 | org.springframework 32 | spring-web 33 | ${spring.version} 34 | 35 | 36 | org.springframework 37 | spring-oxm 38 | ${spring.version} 39 | 40 | 41 | org.springframework 42 | spring-tx 43 | ${spring.version} 44 | 45 | 46 | 47 | org.springframework 48 | spring-jdbc 49 | ${spring.version} 50 | 51 | 52 | 53 | org.springframework 54 | spring-webmvc 55 | ${spring.version} 56 | 57 | 58 | org.springframework 59 | spring-aop 60 | ${spring.version} 61 | 62 | 63 | 64 | org.springframework 65 | spring-context-support 66 | ${spring.version} 67 | 68 | 69 | org.springframework 70 | spring-test 71 | ${spring.version} 72 | 73 | 74 | 75 | 76 | 77 | JavaWebSocket 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/main/java/Monitor.java: -------------------------------------------------------------------------------- 1 | import com.uptop.websocket.WebSocketTest; 2 | 3 | import java.util.Date; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.ScheduledExecutorService; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | 9 | public class Monitor implements Runnable { 10 | 11 | 12 | @Override 13 | public void run() { 14 | WebSocketTest webSocketTest = new WebSocketTest(); 15 | webSocketTest.sendMsg("当前时间:" + new Date()); 16 | } 17 | 18 | public void sendMsg() { 19 | ScheduledExecutorService newScheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); 20 | newScheduledThreadPool.scheduleWithFixedDelay(new Monitor(), 20, 5, TimeUnit.SECONDS); 21 | 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/com/uptop/websocket/WebSocketTest.java: -------------------------------------------------------------------------------- 1 | package com.uptop.websocket; 2 | 3 | import javax.websocket.*; 4 | import javax.websocket.server.ServerEndpoint; 5 | import java.io.IOException; 6 | import java.util.concurrent.CopyOnWriteArraySet; 7 | 8 | 9 | /** 10 | * @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端, 11 | * 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端 12 | * @author uptop 13 | */ 14 | @ServerEndpoint("/websocket") 15 | public class WebSocketTest { 16 | //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 17 | private static int onlineCount = 0; 18 | 19 | //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 20 | public static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet(); 21 | 22 | //与某个客户端的连接会话,需要通过它来给客户端发送数据 23 | private Session session; 24 | 25 | /** 26 | * 连接建立成功调用的方法 27 | * 28 | * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 29 | */ 30 | @OnOpen 31 | public void onOpen(Session session) { 32 | this.session = session; 33 | webSocketSet.add(this); //加入set中 34 | addOnlineCount(); //在线数加1 35 | System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); 36 | } 37 | 38 | /** 39 | * 连接关闭调用的方法 40 | */ 41 | @OnClose 42 | public void onClose() { 43 | webSocketSet.remove(this); //从set中删除 44 | subOnlineCount(); //在线数减1 45 | System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); 46 | } 47 | 48 | /** 49 | * 收到客户端消息后调用的方法 50 | * 51 | * @param message 客户端发送过来的消息 52 | * @param session 可选的参数 53 | */ 54 | @OnMessage 55 | public void onMessage(String message, Session session) { 56 | System.out.println("来自客户端的消息:" + message); 57 | //群发消息 58 | for (WebSocketTest item : webSocketSet) { 59 | try { 60 | item.sendMessage(message); 61 | } catch (IOException e) { 62 | e.printStackTrace(); 63 | continue; 64 | } 65 | } 66 | } 67 | 68 | /** 69 | * 发生错误时调用 70 | * 71 | * @param session 72 | * @param error 73 | */ 74 | @OnError 75 | public void onError(Session session, Throwable error) { 76 | System.out.println("发生错误"); 77 | error.printStackTrace(); 78 | } 79 | 80 | /** 81 | * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。 82 | * 83 | * @param message 84 | * @throws IOException 85 | */ 86 | public void sendMessage(String message) throws IOException { 87 | this.session.getBasicRemote().sendText(message); 88 | 89 | } 90 | 91 | public static synchronized int getOnlineCount() { 92 | return onlineCount; 93 | } 94 | 95 | public static synchronized void addOnlineCount() { 96 | WebSocketTest.onlineCount++; 97 | } 98 | 99 | public static synchronized void subOnlineCount() { 100 | WebSocketTest.onlineCount--; 101 | } 102 | 103 | 104 | public void sendMsg(String msg) { 105 | for (WebSocketTest item : webSocketSet) { 106 | try { 107 | item.sendMessage(msg); 108 | } catch (IOException e) { 109 | e.printStackTrace(); 110 | continue; 111 | } 112 | } 113 | } 114 | 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | contextConfigLocation 10 | classpath:applicationContext.xml 11 | 12 | 13 | org.springframework.web.context.ContextLoaderListener 14 | 15 | 16 | org.springframework.web.util.IntrospectorCleanupListener 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" pageEncoding="UTF-8" %> 2 | 3 | 4 | 5 | index Page 6 | 7 | 8 | Welcome
9 | 10 |
11 | 12 |
13 |
14 | 15 | 16 |
实时信息监控
17 | 18 | 19 | 83 | --------------------------------------------------------------------------------