springboot使用websocket(代码片段)

luojiesheng luojiesheng     2022-12-20     273

关键词:

本文(2019年6月18日 飞快的蜗牛博客) 

 有许多人走着走着,就迷失了自己,所以不论发生了什么,有时候抱着自己去静下来想想,要好好的对待自己;“钱塘江上潮信来,今日方知我是我”,我信奉这句话,不是我超脱了,是有时我们醒悟了;

注意标题:springboot使用websocket

  1】第一步:引入依赖:

   <!--集成websocket-->

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

2】第二步:配置websocket

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
*
* 使用@SpringBootApplication启动类进行启动时需要下面这段代码,****但生成war包部署在tomcat中不需要这段
* 若打成war包使用tomcat运行的话,则注释掉这个类中serverEndpointExporter 方法.
*
*/

@Configuration
public class WebSocketConfig


@Bean
public ServerEndpointExporter serverEndpointExporter()
return new ServerEndpointExporter();

3】第三步:可以在controller 下写下此类:

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
/**
* 即时通讯
*/
@Component
@ServerEndpoint("/webSocket")
public class MyWebScoketController

private static int onlineCount = 0;
private static CopyOnWriteArraySet<MyWebScoketController> webSocketSet = new CopyOnWriteArraySet<MyWebScoketController>();
private Session session;

/**
* 连接建立成功调用的方法
* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
@OnOpen
public void onOpen(Session session)
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在线数加1
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());

/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose()
webSocketSet.remove(this); //从set中删除
subOnlineCount(); //在线数减1
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());

/**
* 收到客户端消息后调用的方法
* @param message 客户端发送过来的消息
* @param session 可选的参数
*/
@OnMessage
public void onMessage(String message, Session session)
System.out.println("来自客户端的消息:" + message);
//群发消息
for(MyWebScoketController item: webSocketSet)
try
item.sendMessage(message);
catch (IOException e)
e.printStackTrace();
continue;



@OnError
public void onError(Session session, Throwable error)
System.out.println("发生错误");
error.printStackTrace();


/**
*
* @param message
* @throws IOException
*/
public void sendMessage(String message) throws IOException
this.session.getBasicRemote().sendText(message);


public static synchronized int getOnlineCount()
return onlineCount;


public static synchronized void addOnlineCount()
MyWebScoketController.onlineCount++;


public static synchronized void subOnlineCount()
MyWebScoketController.onlineCount--;


4】第四步:在前端写个公共myWebsocket.js 如下
if("WebSocket" in window)
console.log("this browser supports websocket...");
var webSocket=new WebSocket("ws://"+window.location.host+"/XXXX/webSocket");
else
console.log("this browser does not supports websocket...");

webSocket.onerror=function()
console.log("链接错误...");

webSocket.onopen=function()
console.log("链接成功...");


/*
* 哪个页面使用哪个页面加
* webSocket.onmessage=function(event)
alert(event);
*/
webSocket.onclose=function()
console.log("链接关闭...");

window.onbeforeunload=function()
console.log("窗口即将关闭,准备关闭链接...");
webSocket.close();

function webSend()
webSocket.send(decodeURIComponent($("#form1").serialize(),true));

function SendMesaage(mes)
webSocket.send(mes);

var closeConn=function()
webSocket.close();

5】第五步:测试 现在页面引入
<script type="text/javascript" src="static/js/common/myWebSocket.js"></script>

在发送消息端写下:
SendMesaage(1300);

接收端写下:

webSocket.onmessage=function(event)
  此处可以接收到消息







 

springboot实现websocket(代码片段)

...一个websocket应该怎么实现,这里采用的是更加方便的Springboot的方式,如果项目中没有使用springboot框架,也是可以 查看详情

springboot实现websocket(代码片段)

...一个websocket应该怎么实现,这里采用的是更加方便的Springboot的方式,如果项目中没有使用springboot框架,也是可以 查看详情

springboot实现websocket(代码片段)

...一个websocket应该怎么实现,这里采用的是更加方便的Springboot的方式,如果项目中没有使用springboot框架,也是可以 查看详情

springboot+vue3集成使用websocket(代码片段)

...ocket</artifactId></dependency>增加配置类,声明该springboot项目使用websocket@ConfigurationpublicclassWebSocketConfig@BeanpublicServerEndpointExporterserverEndpointExporter()returnnewServerEndpointExporter();第三步,新建包增加业务代码:... 查看详情

springboot+vue3集成使用websocket(代码片段)

...ocket</artifactId></dependency>增加配置类,声明该springboot项目使用websocket@ConfigurationpublicclassWebSocketConfig@BeanpublicServerEndpointExporterserverEndpointExporter()returnnewServerEndpointExporter();第三步,新建包增加业务代码:... 查看详情

springboot使用websocket

目录springboot使用WebSocket前端:后端springboot使用WebSocket来源:https://blog.lqdev.cn/2018/08/14/springboot/chapter-nineteen/类似聊天室的功能,WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议。在WebSocketAPI中,浏览器和服务器只... 查看详情

springboot使用@serverendpoint无法依赖注入问题解决(websocket)

 如上两图所示,在WebSocket中我想使用Redis。把自己编写的RedisUtil使用@Autowired自动注入到当前类。在运行时,出现异常:java.lang.NullPointException(上面第二张图的代码)A.可能原因:自己编写的RedisUtil没有放到spring容器中(导致... 查看详情

springboot整合websocket,使用stomp协议+redis解决负载场景问题(代码片段)

前言上一篇,简单给大家整合了一下websocket,使用stomp方式。这篇,就是考虑到单体的服务使用websocket,按照上一篇的整合,确实没问题。但是如果一旦是负载多台服务的时候,那么就会出现丢失问题。什... 查看详情

大屏的接口开发一:基于springboot的websocket数据推送(代码片段)

...的免手动刷新,实现数据的同步变动展示。这里主要使用SpringBoot集成webSocket实现数据的全局发送,指定用户发送,值得注意的是这里使用了Spring自带的握手拦截器获取前端传来的userId,与webSocket的会话进行绑定,用于长连接的... 查看详情

大屏的接口开发一:基于springboot的websocket数据推送(代码片段)

...的免手动刷新,实现数据的同步变动展示。这里主要使用SpringBoot集成webSocket实现数据的全局发送,指定用户发送,值得注意的是这里使用了Spring自带的握手拦截器获取前端传来的userId,与webSocket的会话进行绑定,用于长连接的... 查看详情

springboot+websocket学习(代码片段)

Springboot+WebSocket聊天室项目WebSocket介绍WebSocket的特点webSocket协议客户端(浏览器)实现websocket对象websocket事件WebSocket方法服务端实现服务端如何接受客户端发送过来的数据呢?服务端如何推送数据给客户端呢?基于WebSocket的网页聊... 查看详情

springboot整合websocket简单聊天室(代码片段)

springboot整合websocket(一)简单聊天室springboot整合websocket(一)简单聊天室springboot整合websocket(二)上传文件(引导篇)springboot整合websocket(三)上传文件(终篇& 查看详情

springboot中使用websocket实现一对多聊天及一对一聊天(代码片段)

为什么需要WebSocket?我们已经有了http协议,为什么还需要另外一个协议?有什么好处?比如我想得到价格变化,只能是客户端想服务端发起请求,服务器返回结果,HTTP协议做不到服务器主动向客户端推送消息,这种单向请求的... 查看详情

springboot集成websocket,实现后台向前端推送信息(代码片段)

前言在一次项目开发中,使用到了Netty网络应用框架,以及MQTT进行消息数据的收发,这其中需要后台来将获取到的消息主动推送给前端,于是就使用到了MQTT,特此记录一下。一、什么是websocket?WebSocket协... 查看详情

springboot集成websocket,实现后台向前端推送信息(代码片段)

前言在一次项目开发中,使用到了Netty网络应用框架,以及MQTT进行消息数据的收发,这其中需要后台来将获取到的消息主动推送给前端,于是就使用到了MQTT,特此记录一下。一、什么是websocket?WebSocket协... 查看详情

使用springboot+layim+websocket实现webim

          使用springboot+layim+websocket实现webim 小白技术社 项目介绍采用springboot和layim构建webim,使用websocket作为通讯协议,目前已经能够正常聊天,并没有对好友的操作进行实现,查找和加好友没有实现,有需... 查看详情

springboot+netty+websocket实现消息推送(代码片段)

关于NettyNetty是一个利用Java的高级网络的能力,隐藏其背后的复杂性而提供一个易于使用的API的客户端/服务器框架。Maven依赖<dependencies> <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency>  <groupId&g 查看详情

springboot+websocket(代码片段)

1、添加配置类importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.socket.server.standard.ServerEndpointExporter;@Configu 查看详情