简述:websocket的基础概念就不再讲了 直接上干货
一、加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
二、前台代码
controller跳转页面传参userid 我用的是thymeleaf jsp啥的也都可以
<input type="hidden" th:value="${user.userId}" id="userId"/>
业务需要初始化页面连接websocket 弹窗使用 toastr
$(function(){
connect();
//参数设置,若用默认值可以省略以下面代
toastr.options = {
"closeButton": true, //是否显示关闭按钮
"debug": false, //是否使用debug模式
"positionClass": "toast-bottom-right",//弹出窗的位置
"showDuration": "300",//显示的动画时间
"hideDuration": "1000",//消失的动画时间
"timeOut": "5000", //展现时间
"extendedTimeOut": "1000",//加长展示时间
"showEasing": "swing",//显示时的动画缓冲方式
"hideEasing": "linear",//消失时的动画缓冲方式
"showMethod": "fadeIn",//显示时的动画方式
"hideMethod": "fadeOut" //消失时的动画方式
};
});
var ws = null;
function connect(){
var userId = $("#userId").val();
if(userId!=null && userId != ""){
var wsUrl ="ws://127.0.0.1:8080/soc