SpringBoot实现WebSocket单聊

一、创建项目并导入依赖

?
?

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-websocket</artifactId>

</dependency>

<!--https://mvnrepository.com/artifact/org.webjars/sockjs-client-->

<dependency>

<groupId>org.webjars</groupId>

<artifactId>sockjs-client</artifactId>

<version>1.1.2</version>

</dependency>

<!--https://mvnrepository.com/artifact/org.webjars.bower/jquery-->

<dependency>

<groupId>org.webjars.bower</groupId>

<artifactId>jquery</artifactId>

<version>3.4.1</version>

</dependency>

?
?

<!--https://mvnrepository.com/artifact/org.webjars/stomp-websocket-->

<dependency>

<groupId>org.webjars</groupId>

<artifactId>stomp-websocket</artifactId>

<version>2.3.3</version>

</dependency>

<!--https://mvnrepository.com/artifact/org.webjars/webjars-locator-core-->

<dependency>

<groupId>org.webjars</groupId>

<artifactId>webjars-locator-core</artifactId>

<version>0.38</version>

</dependency>

?
?

注:上面的sockjs-client,jquery,stomp-websocket,webjars-locator-core

属于webjar!webjar就是js,只不过被maven管理方便些。和用js方式一样

?
?

二、相关配置和代码

?
?

2.1)首先创建一个bean用来存储用户之间点对点发送的信息

?
?

?
?

2.2)创建WebSocketConfig配置类并实现WebSocketMessageBrokerConfigurer

?
?

?
?

2.3)创建SecurityConfig配置类并继承WebSecurityConfigurerAdapter

?
?

?
?

SecurityConfig配置为了方便只配置最基本配置

?
?

2.4)controller层

?
?

?
?

这里说一下Principal类和Authentication类,这俩个类是springsecurity的和websocket无关

这两类存放这用户的信息。不了解可以加springsecurity依赖后在controller试试打印出来。至于为什么这样你就得自己看源码了

?
?

2.5)前端html

?
?

<!DOCTYPEhtml>

<htmllang="en">

<head>

<metacharset="UTF-8">

<title>群聊室</title>

<scriptsrc="/webjars/jquery/3.4.1/dist/jquery.min.js"></script>

<scriptsrc="/webjars/sockjs-client/1.1.2/sockjs.min.js"></script>

<scriptsrc="/webjars/stomp-websocket/2.3.3/stomp.min.js"></script>

</head>

<body>

<table>

<tr>

<td><inputtype="button"id="connection"value="连接"/></td>

<td><inputtype="button"id="disconnection"disabled="disabled"value="断开"/></td>

</tr>

</table>

?
?

<divid="chat"style="display:none">

<table>

?
?

<tr>

<td>请输入聊天内容:</td>

<td><inputtype="text"id="content"></td>

<td>请输入要发送到的用户名:</td>

<td><inputtype="text"id="to"></td>

<td><inputtype="button"id="send"value="发送"></td>

</tr>

</table>

<divid="conversation"style="border:1px#ffd86csolid;width:300px;text-align:center">信息框</div>

</div>

?
?

<script>

$(function(){

varstompClient=null;

//连接

$("#connection").click(function(){

connection(true)

});

//断开

$("#disconnection").click(function(){

connection(false)

})

//发送

$("#send").click(function(){

send($("#content").val())

})

});

?
?

//连接

functionconnection(flag){

if(!flag&&stompClient!=null){

bottomStatus(flag)

showHidenChat(flag)

//断开

stompClient.disconnect();

return

}

bottomStatus(flag)

showHidenChat(flag)

//服务器连接点

varsocket=newSockJS("/singleChat")

//返回客户端对象

stompClient=Stomp.over(socket);

?
?

stompClient.connect({},function(successCallback){

?
?

//订阅服务器上的消息

stompClient.subscribe("/user/queue/greetings",function(msg){

//服务端返回的对象放在msg.body

showGreeting(JSON.parse(msg.body))

});

});

}

//发信息

functionsend(msg){

stompClient.send("/app/hello",{},JSON.stringify({"to":$("#to").val(),"content":$("#content").val()}))

}

//展示信息的框

functionshowGreeting(msg){

$("#conversation").append("<div>"+msg.from+":"+msg.content+"</div>");

}

?
?

//连接断开的按钮状态

functionbottomStatus(flag){

$("#connection").prop("disabled",flag);

$("#disconnection").prop("disabled",!flag);

}

//发送消息输入框的状态

functionshowHidenChat(flag){

if(flag){

$("#chat").show();

}else{

$("#chat").hide();

}

}

</script>

</body>

</html>

?
?

原文地址:https://www.cnblogs.com/fernfei/p/12208790.html

时间: 2024-07-29 08:18:41

SpringBoot实现WebSocket单聊的相关文章

Websocket 单聊功能

单聊代码 import json from flask import Flask,request,render_template from geventwebsocket.handler import WebSocketHandler from gevent.pywsgi import WSGIServer from geventwebsocket.websocket import WebSocket app = Flask(__name__) user_socket_dict = {} @ap

SpringBoot实现WebSocket群聊

一.创建项目并导入依赖 ? ? <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <arti

spring websocket 和socketjs实现单聊群聊,广播的消息推送详解

spring websocket 和socketjs实现单聊群聊,广播的消息推送详解 WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功能,使服务端也能主动向客户端发送数据. 我们知道,传统的HTTP协议是无状态的,每次请求(request)都要由客户端(如 浏览器)主动发起,服务端进行处理后返回response结果,而

websocket 群聊,单聊,加密,解密

群聊 from flask import Flask, request, render_templatefrom geventwebsocket.handler import WebSocketHandlerfrom gevent.pywsgi import WSGIServer from geventwebsocket.websocket import WebSocket app = Flask(__name__) # type:Flask user_socket_list = [] @app

websocket实现群聊和单聊(转)

昨日内容回顾 1.Flask路由 1.endpoint="user" # 反向url地址 2.url_address = url_for("user") 3.methods = ["GET","POST"] # 允许请求进入视图函数的方式 4.redirect_to # 在进入视图函数之前重定向 5./index/<nid> # 动态参数路由 <int:nid> def index(nid) 6.str

基于flask 写的web_socket 单聊和群聊

群聊 from flask import Flask,request,render_template from geventwebsocket.handler import WebSocketHandler from gevent.pywsgi import WSGIServer from geventwebsocket.websocket import WebSocket app = Flask(__name__) user_socket_list = [] @app.route("/conn

使用genvent.socket实施群聊/单聊模式

使用genvent.socket实施群聊 from flask import Flask, request, render_template from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler from geventwebsocket.websocket import WebSocket import json user_dict = {} #设置一个公共变量 app

SpringBoot+Vue+WebSocket 实现在线聊天

一.前言 本文将基于 SpringBoot + Vue + WebSocket 实现一个简单的在线聊天功能 页面如下: 在线体验地址:http://www.zhengqingya.com:8101 二.SpringBoot + Vue + WebSocket 实现在线聊天 1.引入websocket依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-bo

ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(三) 之 实现单聊,群聊,发送图片,文件。

上篇讲解了如何搭建聊天服务器,以及客户端js怎么和layui的语法配合.服务器已经连接上了,那么聊天还会远吗? 进入正题,正如上一篇提到的我们用 Client.Group(groupId)的方法向客户端推送消息.本篇就先不把业务搞复杂了,就默认现在两个用户都各自打开了对方的聊天窗口,那么聊天过程是这样的. 同理,B给A发消息也是这个流程,因为无论如何,A(ID)和B(ID)都会按照规则生成同一个组名.其中由于LayIM已经帮我们在客户端做好了发送消息并且将消息展示在面板上,所以我们要做的就是当接