草庐IT

websocket前后端交互通讯

凡客丶 2023-04-21 原文

websocket协议是用于前后端长链接交互的技术,此技术多用于交互不断的场景,比如说类似于微信。QQ两者或者多者之间的交互;

websocket的前端四个注解 对应于后端的四个注解方法,对应触发时间就会对应后端接收消息,

@OnOpen,链接成功交互初始化
@OnClose,关闭事件
@OnMessage,消息事件
@OnError 异常事件

本文只是简单的记录一下项目中用到的场景,并没有详细记录,需要了解的可以查看官方文档;

一、引入WebSocket Jar包


<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-websocket</artifactId>
</dependency> 

二、创建WebSocket 配置类

该配置类用于检测带注解@ServerEndpoint 的bean 并将它们注册到容器中。

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
 
 
@Configuration
@Slf4j
public class WebSocketConfig {
    /**
     * 给spring容器注入这个ServerEndpointExporter对象
     * 相当于xml:
     * <beans>
     * <bean id="serverEndpointExporter" class="org.springframework.web.socket.server.standard.ServerEndpointExporter"/>
     * </beans>
     * <p>
     * 检测所有带有@serverEndpoint注解的bean并注册他们。
     *
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        log.info("serverEndpointExporter被注入了");
        return new ServerEndpointExporter();
    }
}

三、创建WebSocket 处理类

@Component
@Slf4j
@ServerEndpoint("/websocket/{coreCode}/{employeeCode}")//请求案例:// 接口路径 ws://localhost:8087/webSocket/userCode;
public class WebsocketService {
 
    private Session session;//与某个客户端的连接会话,需要通过它来给客户端发送数据
 
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
    //虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
    //  注:底下WebSocket是当前类名
    private static CopyOnWriteArraySet<WebsocketService> webSockets = new CopyOnWriteArraySet<>();
    // 用来存在线连接数
    private static Map<String, Session> sessionPool = new HashMap<String, Session>();
 
    // 用来存储当前用户登录的归属核企
    private static Map<String, String> coreMap = new HashMap<String, String>();
 
    private String employeeCode;  //每次链接访问的时候,都是一个新的请求,所以这个变量是不会覆盖的
 
    /**
     * 链接成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "coreCode") String coreCode, @PathParam(value = "employeeCode") String employeeCode) {
        try {
            this.session = session;
            this.employeeCode = employeeCode;
            webSockets.add(this);
            sessionPool.put(employeeCode, session);
            if (coreMap.containsKey((employeeCode))) {
                coreMap.remove(employeeCode);
                //加入map
            }
            coreMap.put(employeeCode, coreCode);
            log.info("【websocket消息】有新的连接,总数为:" + webSockets.size());
        } catch (Exception e) {
            log.error("websocket链接失败:", e.getMessage());
        }
    }
 
    /**
     * 链接关闭调用的方法<br>    *此方法关闭事件,不管是关闭浏览器还是直接退出或者超时回话都会触发这个接口<br>
     */
    @OnClose
    public void onClose() {
        try {
            webSockets.remove(this);
            if (coreMap.containsKey(employeeCode)) {
                coreMap.remove(employeeCode);
            }
            if(sessionPool.containsKey(employeeCode)){
                sessionPool.remove(employeeCode);
            }
            log.info("【websocket消息】连接断开,总数为:" + webSockets.size());
        } catch (Exception e) {
            log.error("websocket关闭失败:", e.getMessage());
        }
    }
 
    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     */
    @OnMessage
    public void onMessage(String message) {
        log.info("【websocket消息】收到客户端消息:" + message);
    }
 
    /**
     * 发送错误时的处理
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("用户错误,原因:" + error.getMessage());
        error.printStackTrace();
    }
 
 
    /**
     * 全部人员消息
     *
     * @param message
     */
    public void sendAllMessage(String message) {
        log.info("【websocket消息】全部人员消息:" + message);
        for (WebsocketService webSocket : webSockets) {
            try {
                if (webSocket.session.isOpen()) {
                    webSocket.session.getAsyncRemote().sendText(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("全部人员发送消息失败:", e.getMessage());
            }
        }
    }
 
    /**
     * 点对点发送指定人消息
     *
     * @param employeeCode
     * @param message
     */
    public void sendOneMessage(String employeeCode, String message) {
        Session session = sessionPool.get(employeeCode);
        if (session != null && session.isOpen()) {
            try {
                log.info("【websocket消息】 点对点发送指定人消息:" + message);
                session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("点对点发送指定人消息:", e.getMessage());
            }
        }
    }
 
    /**
     * 发送指定多个人员消息
     *
     * @param employeeCodes
     * @param message
     */
    public void sendMoreMessage(String[] employeeCodes, String message) {
        for (String employeeCode : employeeCodes) {
            Session session = sessionPool.get(employeeCode);
            if (session != null && session.isOpen()) {
                try {
                    log.info("【websocket消息】 发送指定多个人员消息:" + message);
                    session.getAsyncRemote().sendText(message);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("发送指定多个人员消息:", e.getMessage());
                }
            }
        }
    }
 
    /**
     * 点对点发送指定场景人员编码消息
     *
     * @param centres
     * @param configTemplates
     */
    public void sendScenetOneMessage(List<InformationCentre> centres, InformationConfigTemplate configTemplates, String code) {
        log.info("当前存储的用户编码和核企的关系集合:{},session集合:{}",coreMap,sessionPool);
        centres.stream().forEach(item -> {
            String value = coreMap.get(item.getEmployeeCode());
            if(StrUtil.isNotBlank(value)){
                //判断是否是全部核企还是部分核企,如果是部分核企,当前登录人员所属核企消息发送对象是否包含
                if ("PART".equals(configTemplates.getScope()) && StrUtil.isNotBlank(configTemplates.getCoreStrand())) {
                    List<InformationCofigCore> informationCofigCores = JSONUtil.toList(configTemplates.getCoreStrand(), InformationCofigCore.class);
                    List<String> coreList = informationCofigCores.stream().map(InformationCofigCore::getCoreCode).collect(Collectors.toList());
                    String coreCodeMap = coreMap.get(item.getEmployeeCode());
                    if (!coreList.contains(coreCodeMap)) {
                        return;
                    }
                }
                if (InformationEnum.INTERNALINFORMATIONCHANNEL.code.equals(code)) {
                    //发送给当前登录人消息
                    Session session = sessionPool.get(item.getEmployeeCode());
                    if (session != null && session.isOpen()) {
                        try {
                            log.info("【websocket消息】 点对点发送指定人消息:" + item);
                            session.getAsyncRemote().sendText(JSONUtil.toJsonStr(item));
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error("点对点发送指定人消息:", e.getMessage());
                        }
                    }
                }
            }
        });
    }
}

有关websocket前后端交互通讯的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

  2. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

  3. ruby - 在 ruby​​ 中的字符串前后添加空格? - 2

    我想在随机字符串前后添加一个空格。我试过使用"Random_string".center(1,"")但它不起作用。谢谢 最佳答案 我发现这是最优雅的解决方案:padded_string="#{random_string}"走简单的路没有错。 关于ruby-在ruby​​中的字符串前后添加空格?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3357897/

  4. C/C++好用的websocket库 - 2

    IntrductionLibwebsocketsisasimple-to-use,MIT-license,pureClibraryprovidingclientandserverforhttp/1,http/2,websockets,MQTTandotherprotocolsinasecurity-minded,lightweight,configurable,scalableandflexibleway.It’seasytobuildandcross-buildviacmakeandissuitablefortasksfromembeddedRTOSthroughmasscloudservi

  5. ruby - 如何在 Ruby 中使用 OpenID 访问 Google 通讯录 - 2

    我正在使用devise登录omniauth,authid。当用户登录时,我得到user_info:name:RiccardoTacconilast_name:Tacconiemail:email@gmail.comfirst_name:Riccardouid:https://www.google.com/accounts/o8/id?id=xxxxxxxxxprovider:google_apps我找到了一个插件:http://stakeventures.com/articles/2009/10/06/portable-contacts-in-ruby获取Google通讯录。我只需要使

  6. ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境 - 2

    我有带有gemwebsocket-rails0.7的Rails3.2应用程序。在开发机上,一切正常在生产环境中,我使用Nginx/1.6作为代理服务器,Unicorn作为http服务器。Thin用于独立模式(在https://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server-Mode之后)。nginx配置:location/websocket{proxy_passhttp://localhost:3001/websocket;proxy_http_version1.1;proxy_set_headerUp

  7. ruby - 如何在子类中的方法前后运行代码? - 2

    我的第一个想法是这样的:classAbstractBuilderattr_reader:time_takendefbuild_with_timerstarted_at=Time.nowbuild@time_taken=Time.now-started_atenddefbuildraise'Implementthismethodinasubclass'endendclassMyBuilder我怀疑有更好的方法可以提供更好的灵active,例如理想情况下,我想在MyBuilder的实例上调用“build”而不是“build_with_timer”,并且始终记录执行时间。我确实考虑过使用al

  8. WebSocket的那些事(1-概念篇) - 2

    目录一、什么是Websocket二、WebSocket部分header介绍三、HTTPVSWebSocket四、什么时候使用WebSockets五、关于SockJS和STOMP一、什么是Websocket根据RFC6455标准,Websocket协议提供了一种标准化的方式在客户端和服务端之间通过TCP连接建立全双工、双向通信渠道。它是一种不同于HTTP的TCP协议,但是被设计为在HTTP基础上运行。Websocket交互始于HTTP请求,该请求会通过HTTPUpgrade请求头去升级请求,进而切换到Websocket协议。请求报文如下:GET/spring-websocket-portfoli

  9. ruby - 如何与 Ruby 中的 Perl 程序交互? - 2

    据我了解,在Ruby和Perl之间没有“桥梁”可以让您直接从Ruby调用Perl函数。据我了解,要从Ruby调用Perl程序,只需将其放在反引号中(即result=`./helloWorld.pl`)。但是,这不允许与Perl程序交互(即您不能与提示交互或提供输入)。我的问题如下:有没有什么方法可以从Ruby向Perl程序提供输入(除了参数)?Ruby和Perl之间没有桥梁,我错了吗?在导航提示时与程序的标准输入交互似乎是错误的方式,我正在处理的程序设计良好,并且具有包含适当Perl函数的库。 最佳答案 有Inline::Ruby模

  10. python模拟通讯录的数据删除功能 - 2

    通讯录(删除)描述模拟通讯录的数据删除功能。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬通讯录中信息包括:姓名,电话,所在学院。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬

随机推荐