1.添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

2.前端代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>webscoket</title>
</head>
<body>
<script>
var websocket = null; if('WebSocket' in window){
websocket = new WebSocket("ws://localhost:8080/webSocket")
}else{
alert("该浏览器不支持websocket")
} websocket.onopen = function(event){
console.log('建立连接')
} websocket.onclose = function(event){
console.log('连接关闭')
} websocket.onmessage = function(event){
console.log('收到消息:'+event.data)
document.body.innerHTML=event.data
} websocket.onerror = function(){
alert("websocket发生通信错误")
} websocket.onbeforeunload = function(event){
websocket.close()
}
</script>
</body>
</html>

3.后台代码

WebSocketConfig.java

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Component
public class WebSocketConfig { @Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

WebSocket.java

package com.example.demo;

import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint; import org.springframework.stereotype.Component; @Component
@ServerEndpoint("/webSocket")
public class WebSocket { private Session session; private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>(); @OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this);
System.err.println("【websocket消息】 有新的连接,总数:"+webSocketSet.size());
} @OnClose
public void onClose() {
webSocketSet.remove(this);
System.err.println("【websocket消息】 连接断开,总数:"+webSocketSet.size());
} @OnMessage
public void onMessage(String message) {
System.err.println("【websocket消息】收到客户端发来的消息"+message);
} public void sendMessage(String message) {
for(WebSocket webSocket:webSocketSet) {
System.err.println("【websocket消息】广播消息,message="+message);
try {
webSocket.session.getBasicRemote().sendText(message);
}catch(Exception e) {
e.printStackTrace();
} }
}
}

Controller.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class Controller { @Autowired
WebSocket websocket; @RequestMapping("/send")
public String send(String message) {
websocket.sendMessage(message);
return "测试成功";
}
}

SpringBoot WebSocket实现的更多相关文章

  1. SpringBoot+WebSocket

    SpringBoot+WebSocket 只需三个步骤 导入依赖 <dependency> <groupId>org.springframework.boot</grou ...

  2. springboot+websocket+sockjs进行消息推送【基于STOMP协议】

    springboot+websocket+sockjs进行消息推送[基于STOMP协议] WebSocket是在HTML5基础上单个TCP连接上进行全双工通讯的协议,只要浏览器和服务器进行一次握手,就 ...

  3. SpringBoot WebSocket STOMP 广播配置

    目录 1. 前言 2. STOMP协议 3. SpringBoot WebSocket集成 3.1 导入websocket包 3.2 配置WebSocket 3.3 对外暴露接口 4. 前端对接测试 ...

  4. Java Springboot webSocket简单实现,调接口推送消息到客户端socket

    Java Springboot webSocket简单实现,调接口推送消息到客户端socket 后台一般作为webSocket服务器,前台作为client.真实场景可能是后台程序在运行时(满足一定条件 ...

  5. Springboot+Websocket+JWT实现的即时通讯模块

    场景 目前做了一个接口:邀请用户成为某课程的管理员,于是我感觉有能在用户被邀请之后能有个立马通知他本人的机(类似微博.朋友圈被点赞后就有立马能收到通知一样),于是就闲来没事搞了一套. ​ 涉及技术栈 ...

  6. Springboot+websocket+定时器实现消息推送

    由于最近有个需求,产品即将到期(不同时间段到期)时给后台用户按角色推送,功能完成之后在此做个小结 1. 在启动类中添加注解@EnableScheduling package com.hsfw.back ...

  7. springboot+websocket 归纳收集

    websocket是h5后的技术,主要实现是一个长连接跟tomcat的comet技术差不多,但websocket是基于web协议的,有更广泛的支持.当然,在处理高并发的情况下,可以结合tomcat的a ...

  8. springboot websocket 一篇足够了

    什么是WebSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 … 为什么要实现握手监控管理 如果说,连接随意创建,不管的话,会存在错误,broken pipe 表面看单纯报 ...

  9. SpringBoot + WebSocket 开发笔记

    1. 服务端的实现,我尝试了两种方式: 第一种是用“@ServerEndPoint”注解来实现,实现简单: 第二种稍显麻烦,但是可以添加拦截器在WebSocket连接建立和断开前进行一些额外操作. 不 ...

  10. springBoot -webSocket 基于STOMP协议交互

    浅谈WebSocket WebSocket是在HTML5基础上单个TCP连接上进行全双工通讯的协议,只要浏览器和服务器进行一次握手,就可以建立一条快速通道,两者就可以实现数据互传了.说白了,就是打破了 ...

随机推荐

  1. Android学习资源网站大全

    https://github.com/zhujun2730/Android-Learning-Resources 整理了一些 Android 的博客链接.学习资源网站.站在巨人的肩膀上,会看得更远.整 ...

  2. [转载]在服务器端判断request来自Ajax请求(异步)还是传统请求(同步),x-requested-with XMLHttpRequest

    在服务器端判断request来自Ajax请求(异步)还是传统请求(同步) 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步):  两种请求在请求的Header不同,Ajax 异步 ...

  3. 我的第四个Python小程序

    简单的登陆程序: # Author: fansik # Description: Simply log in to small programs # Date: 2017/9/29 _username ...

  4. PyQt4调用UI文件

    方法1,转换到py调用 指令 pyuic4 test.ui -o testUi.py 方法2.直接调用ui文件 # -*- coding: utf-8 -*- """ - ...

  5. 【读书笔记】Java核心技术-基础知识-反射

    在网页中运行Java程序称为applet. 反射 这项功能被大量地应用于JavaBeans中,它是Java组件的体系结构. 能够分析类能力的程序称为反射(reflective).反射机制的功能及其强大 ...

  6. mysql数据库补充知识1 安装数据库破解数据库密码已经创建用户

    一.安装MYSQL数据库 1.yum安装 #二进制rpm包安装 yum -y install mysql-server mysql 2.源码安装   1.解压tar包 cd /software tar ...

  7. 剑指offer 面试21题

    面试21题: 题目:调整数组的顺序使奇数位于偶数前面 题一:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 解题思路:使用两个指针 ...

  8. classmethod

    描述 classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等. 语法 classmeth ...

  9. SQL语句 自连表查询。inner join用法,partition by ,列转行查询

    use mydb1 go -- 表T_Employee2 -- Id Name Position Dept -- 1 张三 员工 市场部 -- 2 李四 经理 销售部 -- 3 王五 经理 市场部 - ...

  10. 【CodeChef】Small factorials(BigInteger笔记)

    You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t&l ...