java 即时通讯服务器_Java开发之使用websocket实现web客户端与服务器之间的实时通讯...

本文介绍了如何使用Java的WebSocket API实现一个即时通讯服务器。通过`@ServerEndpoint`注解定义端点,`OnOpen`、`OnClose`、`OnMessage`、`OnError`方法处理客户端的连接、关闭、消息接收和错误情况。服务器维护了一个客户端连接的Map,并通过`addOnlineCount`和`subOnlineCount`跟踪在线用户数。当接收到消息时,服务器能够将消息广播给所有客户端或特定ID的客户端。
摘要由CSDN通过智能技术生成

1 importjava.io.IOException;2 importjava.util.Map;3 importjava.util.concurrent.ConcurrentHashMap;4

5 importjavax.websocket.OnClose;6 importjavax.websocket.OnError;7 importjavax.websocket.OnMessage;8 importjavax.websocket.OnOpen;9 importjavax.websocket.Session;10 importjavax.websocket.server.PathParam;11 importjavax.websocket.server.ServerEndpoint;12

13 importcom.alibaba.fastjson.JSON;14 importcom.utime.facade.model.systemplate.WebSocketMessage;15

16 @ServerEndpoint("/webSocketServer/{userID}")17 public classWebSocketServer {18 //连接客户端数量

19 private static int onlineCount = 0;20 //所有的连接客户端

21 private static Map clients = new ConcurrentHashMap();22 //当前客户端连接的唯一标示

23 privateSession session;24 //当前客户端连接的用户ID

25 privateString userID;26

27 /**

28 * 客户端连接服务端回调函数29 *30 *@paramuserID 用户ID31 *@paramsession 会话32 *@throwsIOException33 */

34 @OnOpen35 public void onOpen(@PathParam("userID") String userID, Session session) throwsIOException {36 this.userID =userID;37 this.session =session;38

39 addOnlineCount();40 clients.put(userID, this);41 System.out.println("WebSocket日志: 有新连接加入!当前在线人数为" +getOnlineCount());42 }43

44 @OnClose45 public void onClose() throwsIOException {46 clients.remove(userID);47 subOnlineCount();48 System.out.println("WebSocket日志: 有一连接关闭!当前在线人数为" +getOnlineCount());49 }50

51 /**

52 * 接受到来自客户端的消息53 *54 *@parammessage55 *@throwsIOException56 */

57 @OnMessage58 public void onMessage(String message) throwsIOException {59 System.out.println("WebSocket日志: 来自客户端的消息:" +message);60 WebSocketMessage webSocketMessage = JSON.parseObject(message, WebSocketMessage.class);61

62 //发送消息给所有客户端

63 if ("0".equals(webSocketMessage.getReceiverId())) {64 for(WebSocketServer item : clients.values()) {65 item.session.getAsyncRemote().sendText(webSocketMessage.getMessageContent());66 System.out.println("WebSocket日志: ID为"+ webSocketMessage.getSenderId() +"的用户给ID为"+ item.userID +"的客户端发送:" +webSocketMessage.getMessageContent());67 }68 } else { //发送消息给指定ID的客户端

69 for(WebSocketServer item : clients.values()) {70 if(item.userID.equals(webSocketMessage.getReceiverId())){71 //发消息给指定客户端

72 item.session.getAsyncRemote().sendText(webSocketMessage.getMessageContent());73 System.out.println("WebSocket日志: ID为"+ webSocketMessage.getSenderId() +"的用户给ID为"+ item.userID +"的客户端发送:" +webSocketMessage.getMessageContent());74 if (!webSocketMessage.getSenderId().equals(webSocketMessage.getReceiverId())) {75 //发消息给自己

76 this.session.getAsyncRemote().sendText(webSocketMessage.getMessageContent());77 System.out.println("WebSocket日志: ID为"+ webSocketMessage.getSenderId() +"的用户给ID为"+ this.userID +"的客户端发送:" +webSocketMessage.getMessageContent());78 }79 break;80 }81 }82 }83 }84

85 /**

86 * 服务端报错了87 *88 *@paramsession89 *@paramerror90 */

91 @OnError92 public voidonError(Session session, Throwable error) {93 System.out.println("WebSocket日志: 发生错误");94 error.printStackTrace();95 }96

97 /**

98 * 客户端连接数+199 */

100 public static synchronized voidaddOnlineCount() {101 WebSocketServer.onlineCount++;102 }103

104 /**

105 * 客户端连接数-1106 */

107 public static synchronized voidsubOnlineCount() {108 WebSocketServer.onlineCount--;109 }110

111 public static synchronized intgetOnlineCount() {112 returnonlineCount;113 }114

115 public static synchronized MapgetClients() {116 returnclients;117 }118 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值