🍊作者:计算机编程-吉哥
🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。
🍊心愿:点赞 👍 收藏 ⭐评论 📝
spring、springmvc、mybatis、maven、jsp 、mysql、jdk1.8
2张
eclipse、navicat
/*** websocket服务*/
@ServerEndpoint(value = "/chatServer", configurator = HttpSessionConfigurator.class)
public class ChatServer {private static int onlineCount = 0; //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。private static CopyOnWriteArraySet<ChatServer> webSocketSet = new CopyOnWriteArraySet<ChatServer>();private Session session; //与某个客户端的连接会话,需要通过它来给客户端发送数据private String userid; //用户名private HttpSession httpSession; //request的sessionprivate static List list = new ArrayList<>(); //在线列表,记录用户名称private static Map routetab = new HashMap<>(); //用户名和websocket的session绑定的路由表/*** 连接建立成功调用的方法* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据*/@OnOpenpublic void onOpen(Session session, EndpointConfig config){this.session = session;webSocketSet.add(this); //加入set中addOnlineCount(); //在线数加1;this.httpSession = (HttpSession) UserProperties().get(Name());this.userid=(String) Attribute("userid"); //获取当前用户list.add(userid); //将用户名加入在线列表routetab.put(userid, session); //将用户名和session绑定到路由表String message = getMessage("[" + userid + "]加入聊天室,当前在线人数为"+getOnlineCount()+"位", "notice", list);broadcast(message); //广播}/*** 连接关闭调用的方法*/@OnClosepublic void onClose(){ve(this); //从set中删除subOnlineCount(); //在线数减ve(userid); //从在线列表移除这个用户ve(userid);String message = getMessage("[" + userid +"]离开了聊天室,当前在线人数为"+getOnlineCount()+"位", "notice", list);broadcast(message); //广播}/*** 接收客户端的message,判断是否有接收人而选择进行广播还是指定发送* @param _message 客户端发送过来的消息*/@OnMessagepublic void onMessage(String _message) {JSONObject chat = JSON.parseObject(_message);JSONObject message = JSON.("message").toString());("to") == null || ("to").equals("")){ //如果to为空,则广播;如果不为空,则对指定的用户发送消息broadcast(_message);}else{String [] userlist = ("to").toString().split(",");singleSend(_message, (Session) (("from"))); //发送给自己,这个别忘了for(String user : userlist){if(!user.("from"))){singleSend(_message, (Session) (user)); //分别发送给每个指定用户}}}}/*** 发生错误时调用* @param error*/@OnErrorpublic void onError(Throwable error){error.printStackTrace();}/*** 广播消息* @param message*/public void broadcast(String message){for(ChatServer chat: webSocketSet){try {BasicRemote().sendText(message);} catch (IOException e) {e.printStackTrace();continue;}}}/*** 对特定用户发送消息* @param message* @param session*/public void singleSend(String message, Session session){try {BasicRemote().sendText(message);} catch (IOException e) {e.printStackTrace();}}/*** 组装返回给前台的消息* @param message 交互信息* @param type 信息类型* @param list 在线列表* @return*/public String getMessage(String message, String type, List list){JSONObject member = new JSONObject();member.put("message", message);member.put("type", type);member.put("list", list);String();}public int getOnlineCount() {return onlineCount;}public void addOnlineCount() {lineCount++;}public void subOnlineCount() {lineCount--;}
}
public class HttpSessionConfigurator extends ServerEndpointConfig.Configurator {@Overridepublic void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response){HttpSession httpSession = (HttpSession();UserProperties().put(Name(),httpSession);}
}
@ServerEndpoint(value = "/videoServer", configurator = HttpSessionConfigurator.class)
public class VideoServer {private static final int MAX_CONNECTION = 20;private static final long MAX_TIMEOUT = 2 * 60 * 1000;private static final Map<String, String> usermap = Collections.synchronizedMap(new HashMap<String, String>());private static final Map<String, Session> sessions = Collections.synchronizedMap(new HashMap<String, Session>());@OnOpenpublic void onOpen(Session session){System.out.println("onOpen");}@OnMessagepublic void onMessage(String message, Session session){}@OnClosepublic void onClose(Session session){}@OnErrorpublic void onError(Throwable error){error.printStackTrace();}
}
更多精彩文章栏目:计算机毕业设计项目
公众号:IT跃迁谷【更多精彩文章】
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓如果大家有任何疑虑,请在下方昵称位置详细咨询。
本文发布于:2024-01-30 14:53:56,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170659763720805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |