forked from ntut-ben/ezfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
308 changed files
with
390 additions
and
656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "发送失败"; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
src/main/java/chat/handler/HttpSessionIdHandshakeInterceptor.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.