springboot之websocket
一、WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。
二、长久以来, 创建实现客户端和用户端之间双工通讯的web app都会造成HTTP轮询的滥用: 客户端向主机不断发送不同的HTTP呼叫来进行询问。
(1)建立在 TCP 协议之上,服务器端的实现比较容易。
(2)与 HTTP 协议有着良好的兼容性。默认端口也是80和443,并且握手阶段采用 HTTP 协议,因此握手时不容易屏蔽,能通过各种 HTTP 代理服务器。
(3)数据格式比较轻量,性能开销小,通信高效。
(4)可以发送文本,也可以发送二进制数据。
(5)没有同源限制,客户端可以与任意服务器通信。
(6)协议标识符是ws(如果加密,则为wss),服务器网址就是 URL。
四、实例:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.pinnet</groupId>
<artifactId>springboot-websocket</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
</dependencies>
</project>
2)websocket配置
package com.pinnet.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration
public class WebSocketConfig { //加入配置,目的使用注解连接websocket
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3)websocket的连接处理服务
package com.pinnet.controller; import org.springframework.stereotype.Component; import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; @Component
//websocket的连接名称
@ServerEndpoint(value = "/webSocket")
public class WebSocketServer {
private Session session;
private List<WebSocketServer> webSocketServers = new CopyOnWriteArrayList<WebSocketServer>(); //打开连接时的操作
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketServers.add(this);
} //关闭连接时的操作
@OnClose
public void onClose() {
webSocketServers.remove(this);
} //接手消息时的操作
@OnMessage
public void onMessage(String message) {
for (WebSocketServer webSocketServer: webSocketServers) {
try {
sendMessage(webSocketServer, message);
} catch (Exception e) {
e.printStackTrace();
}
}
} //发送消息
public void sendMessage(WebSocketServer webSocketServer,String message) throws IOException {
webSocketServer.session.getBasicRemote().sendText(message);
}
}
4)顺便谢了一个测试的js,可以直接在浏览器console里面执行测试
var websocket = null;
if ('WebSocket' in window) {
websocket = new WebSocket('ws://localhost/webSocket');
} else {
alert('该浏览器不支持websocket');
} websocket.onopen = function (event) {
console.log('websocket建立连接');
websocket.send("websocket发送消息");
} websocket.onclose = function (event) {
console.log('websocket关闭连接');
} websocket.onmessage = function (event) {
console.log('websocket收到消息:' + event.data);
}
五、springboot的简单用法就是这样的,当然实际的用法,需要进行封装,具体的处理以及使用都要,一定的框架支持。
springboot之websocket的更多相关文章
- Springboot整合Websocket遇到的坑
Springboot整合Websocket遇到的坑 一.使用Springboot内嵌的tomcat启动websocket 1.添加ServerEndpointExporter配置bean @Confi ...
- SpringBoot 整合 WebSocket
SpringBoot 整合 WebSocket(topic广播) 1.什么是WebSocket WebSocket为游览器和服务器提供了双工异步通信的功能,即游览器可以向服务器发送消息,服务器也可以向 ...
- SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送
代码全部复制,仅供自己学习用 1.环境搭建 因为在上一篇基于STOMP协议实现的WebSocket里已经有大概介绍过Web的基本情况了,所以在这篇就不多说了,我们直接进入正题吧,在SpringBoot ...
- SpringBoot基于websocket的网页聊天
一.入门简介正常聊天程序需要使用消息组件ActiveMQ或者Kafka等,这里是一个Websocket入门程序. 有人有疑问这个技术有什么作用,为什么要有它?其实我们虽然有http协议,但是它有一个缺 ...
- springboot整合websocket原生版
目录 HTTP缺点 HTTP websocket区别 websocket原理 使用场景 springboot整合websocket 环境准备 客户端连接 加入战队 微信公众号 主题 HTTP请求用于我 ...
- 使用springboot+layim+websocket实现webim
使用springboot+layim+websocket实现webim 小白技术社 项目介绍 采用springboot和layim构建webim,使用websocket作为通讯协议,目前已经能够正 ...
- SpringBoot+Vue+WebSocket 实现在线聊天
一.前言 本文将基于 SpringBoot + Vue + WebSocket 实现一个简单的在线聊天功能 页面如下: 在线体验地址:http://www.zhengqingya.com:8101 二 ...
- springboot集成websocket的两种实现方式
WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...
- springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发
工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...
- springBoot 使用webSocket
本文(2019年6月18日 飞快的蜗牛博客) 有许多人走着走着,就迷失了自己,所以不论发生了什么,有时候抱着自己去静下来想想,要好好的对待自己:"钱塘江上潮信来,今日方知我是我", ...
随机推荐
- Hibernate 基于主键的一对一关联关系随手记
@Test public void testSave() { Boss boss = new Boss(); boss.setName("A-老板"); Company compa ...
- sysctl.conf学习和调优
转载于简书:sysctl.conf学习和调优 ,如有版本问题,请联系我 前言 记得第一次接触/etc/security/limits.conf和/etc/sysctl.conf时是因为部署Oracle ...
- 【转】Android单帧动画Rotate旋转
项目有一个需求,有一个刷新按钮,上面放着一个常见的静止的刷新圆圈,如下图: 一旦用户按了刷新按钮,需要让这个刷新圆圈转动起来,让用户感觉到程序还在运行着,而不是卡死了. 有两个思路,一是将这个图按照旋 ...
- Linux学习总结(十三)文本编辑器 vim
vim是vi的升级版,会根据文本属性带色彩显示,具体用法如下: 一般模式 : 1.光标定位: 左右移动一个字符, h l上下移动一个字符, k j左右下上 ,左右在两边,下上在中间这样记光标定位行首 ...
- 3spring:生命周期,属性赋值,自动装配
有不懂的可以参考之前的文章! https://www.cnblogs.com/Mrchengs/p/10109053.html 1.Bean的生命周期 创建---初始化---销毁 容器管理 ...
- Ajax同时上传表单序列化参数+自定义参数
$.ajax({ type:'POST', url :"<{:U('jiuzhu/edit')}>", data:$.param({'name1':value1,'na ...
- HDU 1045 Fire Net(DFS 与8皇后问题类似)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 《Inetnet History,Technology and Security》学习笔记
前言 本文为观看Cousera的Michigan<Internet History, Technology and Security>教程的个人学习笔记,包括了每个week的概要和个人感想 ...
- ubuntu software center和新立得
打开软件库,可以搜索或在终端输入software-center如果没有,可能没有安装软件库,可以在终端安装:sudo apt-get install software-center[注意全部小写]使用 ...
- 【浏览器-Safari-网页开发指南】官方Sarari开发指南译文
Safari是一个功能比较完整的浏览器.它支持所有按照W3C标准HTML,CSS,Javascript开发的网页. 当然,让网站正常访问只是我们的初级目标.开发者应该致力于给用户提供更好的用户体验.比 ...