当前位置: 首页 > news >正文

Websocket升级版

之前写过一个关于websocket的博客,是看书时候做的一个demo。但是纸上得来终觉浅,这次实战后实打实的踩了不少坑,写个博客记录总结。

1.安装postman

websocket接口调试,需要8.5以及以上版本的postman

先把以前的卸载,直接exe安装就好

点击这个连接可以下载,或者去官网下载

https://download.csdn.net/download/qq_35653822/88419296

2.postman调试方法

2.1参考这个博客的postman调试方法

Postman进行Websocket接口测试_postman websocket-CSDN博客

2.2请求路径

不是http开头  而是ws开头

如 ws://127.0.0.1:9999/test/3

2.3测试方式

websocket接口和普通接口不同,需要先建立连接,再发送消息

建立连接时候这个url可以是pathvariable那种

如 /test/{id}

3.上代码

3.1controller层

1.@ServerEndpoint
这个注解算是websocket中极其重要的一个注解了
这里边要配置上前后端建立连接的url,后端返回前端内容的解析器。我这写了两个分别对应字符串和指定实体的解析器。以及websocket对应的config文件

2. @OnOpen  @OnMessage

@OnOpen 前后端建立连接时走的方法,也就是postman点击connect按钮时候会走的方法

@OnMessage 前端给后端发送消息时走的方法,也就是postman点击send的时候会走的方法

3.session.getBasicRemote().sendObject();

后端给前端发送消息,此处根据类型不同,走不同的解析器。也就是@ServerEndpoint中的

encoders

4.注入bean

根据idea提示,需要用set方法注入 

package websocket;import com.fasterxml.jackson.databind.ObjectMapper;
import com.valley.common.result.Result;
import com.valley.system.enums.LinkType;
import com.valley.system.pojo.bo.ConnectResultBO;
import com.valley.system.pojo.form.SourceForm;
import com.valley.system.service.SysDataSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;import javax.websocket.EncodeException;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.security.Principal;/*** @Description: websocket  url中得sourceId是为了指定websocket得连接*/@ServerEndpoint(value = "/linkTest/{sourceId}", encoders = {ServerEncoder.class,StringEncoder.class},configurator = WebSocketConfig.class)
@Component
@Slf4j
public class WebSocketController {private static SysDataSourceService sysDataSourceService;@Autowiredpublic void setDeviceListenerService(SysDataSourceService sysDataSourceService) {WebSocketController.sysDataSourceService = sysDataSourceService;}/*** 实现服务器主动推送*/public void sendMessage(String message, Session session) throws IOException {session.getBasicRemote().sendText(message);}/*** 连接建立成功调用的方法*/@OnOpenpublic void onOpen(Session session, @PathParam("sourceId") String sourceId) throws EncodeException, IOException {log.info("连接成功,sourceId:{}", sourceId);session.getBasicRemote().sendObject(sourceId);}/*** 收到客户端消息后调用的方法** @param message 客户端发送过来的消息*/@OnMessagepublic void onMessage(String message, Session session) throws Exception {log.info("【websocket消息】收到客户端发来的消息:{}", message);// 创建ObjectMapper对象ObjectMapper objectMapper = new ObjectMapper();// 将JSON字符串解析为JSON对象SourceForm sourceForm = objectMapper.readValue(message, SourceForm.class);//根据type判断是否是心跳连接if(LinkType.HEARTBEAT.equals(sourceForm.getLinkType())){//前端约定返回“pong”session.getBasicRemote().sendObject("pong");return;}ConnectResultBO bo = sysDataSourceService.linkTest(sourceForm);try {Result<ConnectResultBO> result = Result.success(bo);session.getBasicRemote().sendObject(result);//session.getBasicRemote().sendText(message.toString());} catch (IOException e) {e.printStackTrace();}}}

3.2config层

1.需要配置在controller层的@ServerEndpoint中

@ServerEndpoint(value = "/api/v1/source/linkTest/{sourceId}", encoders = {ServerEncoder.class,StringEncoder.class},configurator = WebSocketConfig.class)

2. @Configuration修饰

因为要返回其他的bean

3.websocket的token上传返回

需要取header中的 Sec-WebSocket-Protocol 属性,并且返回给前端的时候也要在header中携带

package websocket;import cn.hutool.core.lang.Assert;
import com.valley.system.config.Constant;
import jodd.util.StringUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.Collections;
import java.util.List;
import java.util.Map;/*** @Description: websocket配置*/
@Configuration
public class WebSocketConfig extends ServerEndpointConfig.Configurator{@Beanpublic ServerEndpointExporter serverEndpointExporter(){return new ServerEndpointExporter();}/*** 建立握手时,连接前的操作*/@Overridepublic void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {// 这个userProperties 可以通过 session.getUserProperties()获取//获取请求头String token = request.getHeaders().get("Sec-WebSocket-Protocol").get(0);Assert.notNull(token,"token不可为空");//todo 之后需要对token进行解析校验//OAuth2AccessToken accessToken = tokenStore.readAccessToken(token);//前端会把空格传成&nbsp//token = token.replace(Constant.NBSP," ");//当Sec-WebSocket-Protocol请求头不为空时,需要返回给前端相同的响应response.getHeaders().put("Sec-WebSocket-Protocol", Collections.singletonList(token));super.modifyHandshake(sec, request, response);}/*** 初始化端点对象,也就是被@ServerEndpoint所标注的对象*/@Overridepublic <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {return super.getEndpointInstance(clazz);}
}

3.3解析器

1.如果不配置,在调用

session.getBasicRemote().sendObject(sourceId);

返回方法的时候会报此类型解析器不存在的错误

2.解析器写完之后要加到controller层的@ServerEndpoint中

@ServerEndpoint(value = "/api/v1/source/linkTest/{sourceId}", encoders = {ServerEncoder.class,StringEncoder.class},configurator = WebSocketConfig.class)

3.3.1实体解析器

package websocket;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.valley.common.result.Result;
import com.valley.system.pojo.bo.ConnectResultBO;import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;/*** @Description: websocket发送给前端 泛型中Result<ConnectResultBO>是要发送的类型*/
public class ServerEncoder implements Encoder.Text<Result<ConnectResultBO>> {@Overridepublic void destroy() {// TODO Auto-generated method stub// 这里不重要}@Overridepublic void init(EndpointConfig arg0) {// TODO Auto-generated method stub// 这里也不重要}@Overridepublic String encode(Result<ConnectResultBO> responseMessage) {try {JsonMapper jsonMapper = new JsonMapper();return jsonMapper.writeValueAsString(responseMessage);} catch (JsonProcessingException e) {e.printStackTrace();return null;}}
}

 3.3.2字符串解析器

现在复盘来看,我这个解析器写的完全没必要,如果要发送字符串  把

session.getBasicRemote().sendObject

改为

session.getBasicRemote().sendText

就行了

public class StringEncoder implements Encoder.Text<String>{@Overridepublic String encode(String s){return s;}@Overridepublic void init(EndpointConfig endpointConfig) {}@Overridepublic void destroy() {}
}

4.security权限过滤

这块踩了很久的坑,我们项目是开源项目改的,然后自己拆成了好几个微服务。我开始的时候只专注去改auth项目中的,不管是忽略路径permitall,还是加切面,还是加filter。都不生效。

最后,在公共项目common中找到一个关于security的配置,改了permitall,才生效

但是还要注意哈,既然在这里把权限放开了,那在config中,就得把权限校验加上

此处放开是因为,上边说过前端只能把token放到header的Sec-WebSocket-Protocol属性中,而不能像平时一样放到Authorization属性中。

 5.插曲

在进行实体转换的时候,前端多给我传了参数,引起了报错

objectMapper.readValue(message, SourceForm.class);

为了让代码健壮一点,给实体加了个注解

@JsonIgnoreProperties(ignoreUnknown = true)

Unrecognized field , not marked as ignorable解决办法-CSDN博客

http://www.lryc.cn/news/193194.html

相关文章:

  • 基于音频SOC开发板的主动降噪ANC算法源码实现
  • 【Pytorch】深度学习之损失函数
  • 3.4 构造方法
  • 代码随想录
  • 2核4G游戏服务器推荐(阿里云/腾讯云/华为云)
  • SQL标识列实现自动编号的步骤和技巧以及优势
  • 【Debian】报错:su: Authentication failure
  • 我测试用的mark down教程
  • 网络编程基础知识总结——IP,端口,协议
  • 【LeetCode力扣】297. 二叉树的序列化与反序列化
  • Linux寄存器+Linux2.6内核进程调度队列+命令行参数+环境变量
  • 组合数(2)获取C(n,k)组合数列表的QT实现
  • SparkCore编程RDD
  • VBA技术资料MF69:添加和删除工作表中的分页符
  • 数字技术助力智慧公厕,让公厕变身为全新创新应用
  • electron 升级 v22 遇到问题
  • 跟我学c++中级篇——Pimpl
  • [补题记录] Atcoder Beginner Contest 295(E)
  • 解决git在window11操作很慢,占用很大cpu的问题
  • C++智能指针(二)——weak_ptr初探
  • 540 - Team Queue (UVA)
  • 投资组合之如何估值
  • 2024届通信工程保研经验分享(预推免入营即offer)
  • L2-025 分而治之 - java
  • Python+高光谱数据预处理-机器学习-深度学习-图像分类-参数回归
  • 免费 AI 编程助手 Amazon CodeWhisperer 体验
  • 【Linux】从零开始学习Linux基本指令(一)
  • Java GC 算法
  • vue3 v-html中使用v-viewer
  • Leetcode算法解析——查找总价格为目标值的两个商品