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

记录Java使用websocket

实现场景:每在小程序中添加一条数据时,后台将主动推送一个标记给PC端,PC端接收到标记将进行自动播放音频。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;@Configuration
public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}
}
import org.springframework.stereotype.Component;import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;@Component
@ServerEndpoint("/websocket")
public class WebSocketServer {private static final CopyOnWriteArraySet<WebSocketServer> connections = new CopyOnWriteArraySet<>();private Session session;@OnOpenpublic void onOpen(Session session) {this.session = session;connections.add(this);this.sendMessage("0");}@OnClosepublic void onClose() {connections.remove(this);System.out.println("连接关闭: " + session.getId());}@OnErrorpublic void onError(Throwable error) {System.out.println("错误信息:"+error.getMessage());error.printStackTrace();}private void sendMessage(String message) {try {this.session.getBasicRemote().sendText(message);} catch (IOException e) {e.printStackTrace();}}public static void broadcast(String message) {for (WebSocketServer client : connections) {client.sendMessage(message);}}}
@Overridepublic void addUser(req req) {QueryWrapper<User> userWrapper = new QueryWrapper<>();userWrapper .eq("name",req.getName());User u = userMapper.selectOne(userWrapper);if (u == null){WebSocketServer.broadcast("1");}}

每次小程序调用 addUser() 方法时,都会执行 WebSocketServer.broadcast("1"); 这时候触发主动推送消息给PC端。


遇到的问题:

         Error during WebSocket handshske: Unexpected response code: 200

解决方案:

        本地项目中存在账号密码验证的过滤器,其中 /websocket 进行了转发,所以需要过滤请求

 

重新启动项目即可。 


部署到线上的操作:websocket的地址(wss://xxx.com/api/websocket

        其中 /api/websocket为自己项目中的websocket的接口地址

1.修改ws的地址 -> 将本地IP修改为线上域名

2.修改nginx

3.PC端进行访问并且查看日志,是否连接成功

出现的问题:

1.Error during WebSocket handshake: Unexpected response code: 301

        域名为 https ,需要将 ws 修改为 wss 

2.Error during WebSocket handshake: Unexpected response code: 404

        nginx 里面对当前域名进行 websocket 配置

        参考地址:https://www.cnblogs.com/cnsyear/p/12635301.html

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

相关文章:

  • (javaweb)分层解耦
  • 2024华为数通HCIP-datacom最新题库(H12-831变题更新⑨)
  • PCIe学习笔记(21)
  • 分享Embedding 模型微调的实现
  • TED: 1靶场复现【附代码】(权限提升)
  • Python(TensorFlow)衍射光学层卷积算法模拟(英伟达GPU)
  • iOS开发进阶(二十二):Xcode* 离线安装 iOS Simulator
  • Prostgresql的Timescaledb插件/扩展部署
  • 分布式知识总结(一致性Hash算法)
  • 图数据库在社交网络分析中的应用
  • Git基础使用教程
  • 技术速递|Python in Visual Studio Code 2024年8月发布
  • 【话题】重塑未来:AI辅助编程对程序员工作的影响与应对策略
  • 在Debian上安装freeswitch
  • 论文分享 | Fuzz4All: 基于大语言模型的通用模糊测试
  • VS Code 配置docker 管理员权限终端
  • 使用Linux实现FTP云盘1
  • tombo resquiggle
  • vue3获取vue实例 并注册全局属性方法
  • function calling后,如何让大模型进行自然语言输出?
  • Android笔试面试题AI答之Kotlin(8)
  • LVS服务的搭建之NAT模式、DR模式的搭建实战
  • Raft分布式存储
  • 【Linux】使用nm命令查看动态库包含的符号表
  • 你还不知道苹果手机截长图的方法?4 种方法都可以
  • C++选择题带答案
  • Unity动画模块 之 简单创建一个序列帧动画
  • 学会高效记录并整理编程学习笔记
  • Llama 3.1中文微调数据集已上线,超大模型一键部署
  • css实现太极图