zoukankan      html  css  js  c++  java
  • 个人博客

    2021年5月19日:

    今天想了一个办法来解决别人断线的问题,以下是加的几串代码:

    private static Map<String, List<String>> map = new HashMap<>();
    private static List<String> list = new ArrayList<>();

    我给websocket类加了两个属性,分别是一个map集合和一个list集合,这个map集合的作用在于当用户上线时,就会往map集合中添加一个用户,当用户下线时,如果检测不到该用户就会把相应的信息存到list集合中,然后只要用户一上线那就把list集合中的信息循环的发给他,这样一来就完美的解决了用户下线的问题。

    以下是我的websocket的完整代码:

    package com.atguigu.crud.utils;

    import javax.websocket.*;
    import javax.websocket.server.PathParam;
    import javax.websocket.server.ServerEndpoint;
    import org.springframework.stereotype.Component;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;

    @ServerEndpoint("/websocket/{userno}")
    @Component
    public class websocket {
    private static ConcurrentHashMap<String, websocket> webSocketSet = new ConcurrentHashMap<String, websocket>();
    private static Map<String, List<String>> map = new HashMap<>();
    private static List<String> list = new ArrayList<>();
    private Session WebSocketsession;
    private String userno = "";

    @OnOpen
    public void onOpen(@PathParam(value = "userno") String param, Session WebSocketsession, EndpointConfig config)
    throws IOException {
    userno = param;
    this.WebSocketsession = WebSocketsession;
    websocket webSocketServer = webSocketSet.get(param);
    if (webSocketServer != null) { // 同样业务的连接已经在线,则把原来的挤下线。
    webSocketServer.WebSocketsession.getBasicRemote().sendText(param + "重复连接被挤下线了");
    webSocketServer.WebSocketsession.close();
    }
    webSocketSet.put(param, this);
    if (map.get(param) != null) {
    webSocketSet.get(param).sendMessage(param);
    }
    }

    @OnClose
    public void onClose() {
    if (!userno.equals("")) {
    webSocketSet.remove(userno);
    }
    }

    @OnMessage
    public void onMessage(String message) {
    if (message.split("[|]")[1].contains(",")) {
    sendAll(message.split("[|]")[1].split(","));
    } else {
    sendToUser(message);
    }
    }
    public void sendToUser(String message) {
    String sendUserno = message.split("[|]")[1];
    String sendMessage = message.split("[|]")[0];
    try {
    if (webSocketSet.get(sendUserno) != null) {
    webSocketSet.get(sendUserno).sendMessage(sendUserno);
    } else {
    if (map.get(sendUserno) == null) {
    map.put(sendUserno, list);
    map.get(sendUserno).add(sendMessage);
    } else {
    map.get(sendUserno).add(sendMessage);
    }
    }
    } catch (IOException e) {
    }
    }

    private void sendAll(String a[]) {
    for (String key : a) {
    try {
    if (webSocketSet.get(key) != null) {
    webSocketSet.get(key).sendMessage(key);
    } else {
    if (map.get(key) == null) {
    map.put(key, list);
    map.get(key).add(key);
    } else {
    map.get(key).add(key);
    }
    }
    } catch (IOException e) {
    }
    }
    }

    @OnError
    public void onError(Session session, Throwable error) {
    error.printStackTrace();
    }

    public void sendMessage(String message) throws IOException {
    this.WebSocketsession.getBasicRemote().sendText(message);
    }
    }

  • 相关阅读:
    MVC知识总结(前序)
    MySql 安装
    django【ORM】model字段类型
    gmail注册时“此电话号码无法用于进行验证”
    Python3 re模块正则表达式中的re.S
    django【ORM】 通过外键字段找对应类
    Django【进阶】modelform
    python3-字符编码
    python3-可变和不可变数据类型
    Django【设计】同功能不同实现模式的兼容性
  • 原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/14873664.html
Copyright © 2011-2022 走看看