摄影网站排行,网站上的验证码怎么做的,海口市网站开发,wordpress 统计浏览前言 个人地址#xff1a;使用SpringBoot搭建一个简单的webSocket服务 什么是WebSocket#xff1f; WebSocket是一个HTML5新增的协议,它的目的在浏览器和服务器之间建立一个不受限的双向实时通信的通道。比如#xff0c;服务器可以任意时刻发送消息给浏览器。它是基于TCP使用SpringBoot搭建一个简单的webSocket服务 什么是WebSocket WebSocket是一个HTML5新增的协议,它的目的在浏览器和服务器之间建立一个不受限的双向实时通信的通道。比如服务器可以任意时刻发送消息给浏览器。它是基于TCP先通过HTTP/HTTPS协议发起一条特殊的HTTP请求进行握手后创建一个用于交换数据的TCP连接。 2.有什么优势 webSocket只需要一次握手就可以实时发送消息。
搭建环境
1. 创建基础工程
我们使用Idea编辑器创建一个开发的基本工程这里我们通过Spring Initializr创建。 通过next下一步填写自己的包路径以及项目名称。 一直下一步直到创建完成。
2. 引入jar包
!-- springboot依赖 --
parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.4.5/versionrelativePath/ !-- lookup parent from repository --
/parent
!-- springboot websocket依赖 --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-websocket/artifactId
/dependency3. 注册bean配置
Bean
public ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();
}4. 编写服务类
package com.zy.websocket.server;import org.springframework.stereotype.Component;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;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;Component
ServerEndpoint(value /webSocket)
public class SocketServer {/*** 静态变量用来记录当前在线连接数。应该把它设计成线程安全的。*/private static int onlineCount 0;/*** concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象。*/private static CopyOnWriteArraySetSocketServer webSocketSet new CopyOnWriteArraySetSocketServer();/*** 与某个客户端的连接会话需要通过它来给客户端发送数据*/private Session session;/*** 连接建立成功调用的方法*/OnOpenpublic void onOpen(Session session) {this.session session;//加入set中webSocketSet.add(this);//在线数加1addOnlineCount();System.out.println(有新连接加入当前在线人数为 getOnlineCount());try {sendMessage(当前在线人数为 getOnlineCount());} catch (IOException e) {System.out.println(IO异常);}}/*** 连接关闭调用的方法*/OnClosepublic void onClose() {//从set中删除webSocketSet.remove(this);//在线数减1subOnlineCount();System.out.println(有一连接关闭当前在线人数为 getOnlineCount());}/*** 收到客户端消息后调用的方法** param message 客户端发送过来的消息*/OnMessagepublic void onMessage(String message, Session session) {System.out.println(来自客户端的消息: message);//群发消息for (SocketServer item : webSocketSet) {try {item.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}}OnErrorpublic void onError(Session session, Throwable error) {System.out.println(发生错误);error.printStackTrace();}public void sendMessage(String message) throws IOException {this.session.getBasicRemote().sendText(message);}/*** 群发自定义消息*/public static void sendInfo(String message) throws IOException {for (SocketServer item : webSocketSet) {try {item.sendMessage(message);} catch (IOException e) {continue;}}}public static synchronized int getOnlineCount() {return onlineCount;}public static synchronized void addOnlineCount() {SocketServer.onlineCount;}public static synchronized void subOnlineCount() {SocketServer.onlineCount--;}
}
5.测试服务