springboot搭建一个简单的websocket的实时推送应用
说一下实用springboot搭建一个简单的websocket 的实时推送应用
websocket是什么
我们以前用的http协议只能单向的浏览器给服务器发请求,然后服务器再去相应返回数据。
websocket呢就是可以服务器主动给浏览器发数据
优点
较少的控制开销
更强的实时性
保持连接状态
更好的二进制的支持
支持扩展
更换的压缩效果
pom文件
springboot项目的话只需要下面这个依赖就可以了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
springboot的项目搭建
这里看我上一篇文章就不在多说了
我们需要去注入ServerEndpointExporter
这是一个检测类型的bean 检测带注释@ServerEndpoint的bean并注册它们
稍后我们可以讲一下@configuration @bean 和@component @autowired 等注解
package com.gzh;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @Configuration
* @bean
* 表示给spring容器注入bean 他们两个一般一起使用
* 他们和@component的区别是他们使用了动态代理cglib 所以每次调用都会返回同一个实例
*/
@Configuration
public class WebSocketConfig {
/**
* 给spring容器注入这个ServerEndpointExporter对象
* 相当于xml:
* <beans>
* <bean id="serverEndpointExporter" class="org.springframework.web.socket.server.standard.ServerEndpointExporter"/>
* </beans>
*
* 检测所有带有@serverEndpoint注解的bean并注册他们。
* @return
*/
@Bean
public ServerEndpointExporter serverEndpointExporter(){
System.out.println("我被注入了");
return new ServerEndpointExporter();
}
}
下面使我们的websocket类
下面一共写了五种方法
建立通信
关闭通信
接收消息
发送消息
异常
package com.gzh;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @Component 将类注入到容器
* @ServerEndpoint 前端通过这个url进行访问通信 建立连接
*/
@ServerEndpoint("/websocket")
@Component
public class MyWebSocket {
//存放websocket 的线程安全的无序的集合
private static CopyOnWriteArraySet<MyWebSocket> websocket = new CopyOnWriteArraySet<MyWebSocket>();
private Session session;
public static CopyOnWriteArraySet<MyWebSocket> getWebsocket() {
return websocket;
}
public static void setWebsocket(CopyOnWriteArraySet<MyWebSocket> websocket) {
MyWebSocket.websocket = websocket;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
/**
* 连接建立成功调用的方法
* */
@OnOpen
public void onOpen(Session session) {
this.session = session;
websocket.add(this); //加入set中
// addOnlineCount(); //在线数加1
System.out.println("进入onOpen方法");
try {
sendMessage("连接已建立成功.");
} catch (Exception e) {
System.out.println("IO异常");
}
}
/**
* 关闭通信连接
* @param session
*/
@OnClose
public void onClose(Session session){
//关闭连接后将此socket删除
websocket.remove(this);
System.out.println("进入onClose方法");
}
/**
* 获取客户端发来的信息
*/
@OnMessage
public void onMessage(String message){
System.out.println("进入onMessage方法; message = " + message);
}
/**
* 给客户端推送信息
*/
public void sendMessage(String message) throws IOException {
System.out.println("进入sendMessage方法");
this.session.getBasicRemote().sendText(message);
}
/**
* 异常方法
*/
@OnError
public void onError(Session session, Throwable error){
System.out.println("进入error方法");
error.printStackTrace();
}
}
这里呢我们稍微分析下这个方法里面的东西
注解@ServerEndpoint
package javax.websocket.server;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.server.ServerEndpointConfig.Configurator;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface ServerEndpoint {
//注释类应映射到的URI或URI模板(必写的参数)。
String value(); //子协议
String[] subprotocols() default {};
//解码器 输入websocket消息 输出java对象
Class<? extends Decoder>[] decoders() default {};
//编码器 输入java对象 输出websocket
Class<? extends Encoder>[] encoders() default {};
//配置器
Class<? extends Configurator> configurator() default Configurator.class;
}
CopyOnWriteArraySet(看完就知道我们为什么要用它了)
java.util.concurrent包下 俗称 JUC
线程安全的无序的集合,可以将它理解成线程安全的HashSet
Set:不可重复,检索元素效率低下,删除和插入效率高,
List:可重复,和数组类似,List可以动态增长,查找元素效率高,插入删除元素效率低
@onopen @onclose等注解
和前台websocket.onerror等方法相对应
我们的主方法执行服务
这里我们每三秒启动个线程
package com.gzh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 标注一个主程序,说明这是一个springboot应用
*/
@SpringBootApplication
public class SpringBootMainApplication {
//编写主程序方法
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplication.class);
for (int i = 0; i <= 10 ; i++){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
}
}
}
线程类
在线程的run方法里让他去发送消息
package com.gzh;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
public class MyThread implements Runnable{
@Override
public void run() {
CopyOnWriteArraySet<MyWebSocket> websocket = MyWebSocket.getWebsocket();
for (MyWebSocket myWebSocket : websocket) {
try {
myWebSocket.sendMessage("我要发消息了"+ Math.random());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
html前端
这里我们只需要更改我们的地址路径就即可
<!DOCTYPE HTML>
<html>
<head>
<title>My WebSocket</title>
</head>
<body>
Welcome<br/>
<input id="text" type="text" /><button onclick="send()">Send</button> <button onclick="closeWebSocket()">Close</button>
<div id="message">
</div>
</body>
<script type="text/javascript">
var websocket = null;
//判断当前浏览器是否支持WebSocket ,主要此处要更换为自己的地址
if('WebSocket' in window){
websocket = new WebSocket("ws://localhost:8080/websocket");
}
else{
alert('Not support websocket')
}
//连接发生错误的回调方法
websocket.onerror = function(){
setMessageInnerHTML("error");
};
//连接成功建立的回调方法
websocket.onopen = function(event){
setMessageInnerHTML("open");
}
//接收到消息的回调方法
websocket.onmessage = function(event){
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function(){
setMessageInnerHTML("close");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function(){
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML){
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭连接
function closeWebSocket(){
websocket.close();
}
//发送消息
function send(){
var message = document.getElementById('text').value;
websocket.send(message);
}
</script>
</html>
这里说一下我们的路径 ws 我们一般会看到wss 什么时候用哪个呢?
他们的使用规则主要是和浏览器的兼容有关
1.Firefox环境下https不能使用ws连接
2.chrome内核版本号低于50的浏览器是不允许https下使用ws链接
3.Firefox环境下https下使用wss链接需要安装证书
所以我们可以对这部分浏览器做个处理
let protocol = location.protocol === 'https'
? 'wss://localhost:8888'
: 'ws://localhost:8889';
new WebSocket(protocol);
以上就是springboot下实现简单的websocket的应用
springboot搭建一个简单的websocket的实时推送应用的更多相关文章
- 【SpingBoot】 测试如何使用SpringBoot搭建一个简单后台1
很久没写博客了,最近接到一个组内的测试开发任务是做一个使用SpringBoot 开发一个后台程序(还未完成),特写感想记录一下 1. 为什么选择SpringBoot ? 首先是目前很多公司的后台还是J ...
- 使用SpringBoot搭建一个简单的web工程
最近在学习SpringBoot,想写在博客园上记录一下,如有错误之处还望指出. 首先创建一个maven工程,不用勾选骨架. 在pom.xml文件中添加如下内容,使工程变成Springboot应用. & ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
- 使用 SpringBoot+Dubbo 搭建一个简单分布式服务
实战之前,先来看几个重要的概念 开始实战之前,我们先来简单的了解一下这样几个概念:Dubbo.RPC.分布式.由于本文的目的是带大家使用SpringBoot+Dubbo 搭建一个简单的分布式服务,所以 ...
- 用 Go 编写一个简单的 WebSocket 推送服务
用 Go 编写一个简单的 WebSocket 推送服务 本文中代码可以在 github.com/alfred-zhong/wserver 获取. 背景 最近拿到需求要在网页上展示报警信息.以往报警信息 ...
- 使用SignalR ASP.NET Core来简单实现一个后台实时推送数据给Echarts展示图表的功能
什么是 SignalR ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功能添加到应用程序的功能. 实时 web 功能使服务器端代码可以立 ...
- 【原创】node+express+socket搭建一个实时推送应用
技术背景 Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新. 应用场景: 监控系统:后台硬件热插拔.LED.温度.电压发生变化 即 ...
- 用nodejs搭建一个简单的服务器
使用nodejs搭建一个简单的服务器 nodejs优点:性能高(读写文件) 数据操作能力强 官网:www.nodejs.org 验证是否安装成功:cmd命令行中输入node -v 如果显示版本号表示安 ...
- 初学Node(六)搭建一个简单的服务器
搭建一个简单的服务器 通过下面的代码可以搭建一个简单的服务器: var http = require("http"); http.createServer(function(req ...
随机推荐
- nyoj 77-开灯问题 (倍数遍历)
77-开灯问题 内存限制:64MB 时间限制:3000ms 特判: No 通过数:13 提交数:24 难度:1 题目描述: 有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 的倍 ...
- 安装Fedora后
更新操作系统版本: https://fedoraproject.org/wiki/DNF_system_upgrade 靠谱: 设置ssh:ssh生成公钥私钥.默认root(.ssh/confi ...
- 更换JDK
1.更换JDK 1).卸载原有jdk 检查一下系统中的jdk版本 java -version 显示 java version "1.6.0_24" OpenJDK Runtime ...
- element表格点击行即选中该行复选框
关键代码如下 <el-table ref="multipleTable" :data="tableData" highlight-current-row ...
- 使用boost实现线程池thread pool | boost thread pool example
本文首发于个人博客https://kezunlin.me/post/f241bd30/,欢迎阅读! boost thread pool example Guide boost thread pool ...
- web前端开发面试题(Vue.js)
1.active-class是哪个组件的属性?嵌套路由怎么定义? 答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? ...
- Coding,命名是个技术活
来吧 日常编码少不了的事情就是给代码命名,代码中命名的重要性在项目前期不会有太大感受,因为是边做边命名,代码天天见,自然会加深记忆.但到了后期上线后半年一年后,再回过头看的时候,我擦,这个变量是啥意思 ...
- 联想Y7000,I5-9300H+Nvidia GTX 1050, kali linux的nvidia显卡驱动安装
转载自,Linux安装NVIDIA显卡驱动的正确姿势 https://blog.csdn.net/wf19930209/article/details/81877822#NVIDIA_173 ,主要用 ...
- springboot+swagger接口文档企业实践(下)
目录 1.引言 2. swagger接口过滤 2.1 按包过滤(package) 2.2 按类注解过滤 2.3 按方法注解过滤 2.4 按分组过滤 2.4.1 定义注解ApiVersion 2.4.2 ...
- 安卓开发之Java学习
Java之素数(这里附上王智超大佬的博客地址)https://blog.csdn.net/weixin_43862765/article/details/103311286