Skip to content

Commit

Permalink
work chat
Browse files Browse the repository at this point in the history
  • Loading branch information
ntut-ben committed Nov 9, 2019
1 parent e3571fd commit 03bd5e7
Show file tree
Hide file tree
Showing 308 changed files with 390 additions and 656 deletions.
5 changes: 0 additions & 5 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
Expand Down
26 changes: 15 additions & 11 deletions .settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">





<wb-module deploy-name="git_ezfit-0.0.1-SNAPSHOT">





<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>





<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>



<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>


<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>


<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>



<property name="java-output-path" value="/ezfit/target/classes"/>



<property name="context-root" value="git_ezfit"/>





</wb-module>





</project-modules>
1 change: 0 additions & 1 deletion .springBeans
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
<config>src/main/resources/beans.xml</config>
</configs>
<autoconfigs>
</autoconfigs>
Expand Down
26 changes: 18 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<hibernate.version>5.4.6.Final</hibernate.version>
</properties>





<dependencies>

<!-- 動態網頁 -->
Expand Down Expand Up @@ -69,6 +65,16 @@
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Hibernate Framewrok -->
<dependency>
<groupId>org.hibernate</groupId>
Expand Down Expand Up @@ -128,17 +134,21 @@
<version>20190722</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.29</version>
</dependency>

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
20 changes: 0 additions & 20 deletions src/main/java/_99/HashTest.java

This file was deleted.

128 changes: 128 additions & 0 deletions src/main/java/chat/WebSocketChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package chat;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.apache.log4j.Logger;
import org.springframework.web.socket.server.standard.SpringConfigurator;

@ServerEndpoint(value = "/chat/{userId}", configurator = SpringConfigurator.class)
public class WebSocketChat {
private static Logger logger = Logger.getRootLogger();
// 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;

// 记录每个用户下多个终端的连接
private static Map<String, Set<WebSocketChat>> userSocket = new HashMap<>();

// 需要session来对用户发送数据, 获取连接特征userId
private Session session;
private String userId;

/**
* @Title: onOpen
* @Description: websocekt连接建立时的操作
* @param @param userId 用户id
* @param @param session websocket连接的session属性
* @param @throws IOException
*/
@OnOpen
public void onOpen(@PathParam("userId") String userId, Session session) throws IOException {
this.session = session;
this.userId = userId;
onlineCount++;
// 根据该用户当前是否已经在别的终端登录进行添加操作
if (userSocket.containsKey(this.userId)) {
logger.info("当前用户id:{" + this.userId + "}已有其他终端登录");
userSocket.get(this.userId).add(this); // 增加该用户set中的连接实例
} else {
logger.info("当前用户id:{" + this.userId + "}第一个终端登录");
Set<WebSocketChat> addUserSet = new HashSet<>();
addUserSet.add(this);
userSocket.put(this.userId, addUserSet);
}
logger.info("用户{" + userId + "}登录的终端个数是为{" + userSocket.get(this.userId).size() + "}");
logger.info("当前在线用户数为:{" + userSocket.size() + "},所有终端个数为:{" + onlineCount + "}");
}

/**
* @Title: onClose
* @Description: 连接关闭的操作
*/
@OnClose
public void onClose() {
// 移除当前用户终端登录的websocket信息,如果该用户的所有终端都下线了,则删除该用户的记录
if (userSocket.get(this.userId).size() == 0) {
userSocket.remove(this.userId);
} else {
userSocket.get(this.userId).remove(this);
}
logger.info("用户{" + this.userId + "}登录的终端个数是为{" + userSocket.get(this.userId).size() + "}");
logger.info("当前在线用户数为:{" + userSocket.size() + "},所有终端个数为:{" + onlineCount + "}");
}

/**
* @Title: onMessage
* @Description: 收到消息后的操作
* @param @param message 收到的消息
* @param @param session 该连接的session属性
*/
@OnMessage
public void onMessage(String message, Session session) {
logger.info("收到来自用户id为:{" + this.userId + "}的消息:{" + message + "}");
if (session == null)
logger.info("session null");
// 测试向客户端发送消息发送
sendMessageToUser(this.userId, "服务器收到你的消息:" + message);
}

/**
* @Title: onError
* @Description: 连接发生错误时候的操作
* @param @param session 该连接的session
* @param @param error 发生的错误
*/
@OnError
public void onError(Session session, Throwable error) {
logger.info("用户id为:{" + this.userId + "}的连接发送错误");
error.printStackTrace();
}

/**
* @Title: sendMessageToUser
* @Description: 发送消息给用户下的所有终端
* @param @param userId 用户id
* @param @param message 发送的消息
* @param @return 发送成功返回true,反则返回false
*/
public Boolean sendMessageToUser(String userId, String message) {
if (userSocket.containsKey(userId)) {
logger.info(" 给用户id为:{" + userId + "}的所有终端发送消息:{" + message + "}");
for (WebSocketChat WS : userSocket.get(userId)) {
logger.info("sessionId为:{" + WS.session.getId() + "}");
try {
WS.session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
logger.info(" 给用户id为:{" + userId + "}发送消息失败");
return false;
}
}
return true;
}
logger.info("发送错误:当前连接不包含id为:{" + userId + "}的用户");
return false;
}

}
20 changes: 0 additions & 20 deletions src/main/java/chat/component/CacheManager.java

This file was deleted.

33 changes: 33 additions & 0 deletions src/main/java/chat/controller/MessageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package chat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import chat.service.WSMessageService;

@Controller
@RequestMapping("/message")
public class MessageController {

// websocket服务层调用类
@Autowired
private WSMessageService wsMessageService;

// 请求入口
@RequestMapping(value = "/TestWS", method = RequestMethod.GET)
@ResponseBody
public String TestWS(@RequestParam(value = "userId", required = true) String userId,
@RequestParam(value = "message", required = true) String message) {
System.out.println("收到发送请求,向用户{" + userId + "}的消息:{" + message + "}");

if (wsMessageService.sendToAllTerminal(userId, message)) {
return "发送成功";
} else {
return "发送失败";
}
}
}
13 changes: 0 additions & 13 deletions src/main/java/chat/handler/CacheConstant.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/chat/handler/Constants.java

This file was deleted.

37 changes: 0 additions & 37 deletions src/main/java/chat/handler/HttpSessionIdHandshakeInterceptor.java

This file was deleted.

Loading

0 comments on commit 03bd5e7

Please sign in to comment.