IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> WebSocket -> 正文阅读

[网络协议]WebSocket

WebSocketConfig.java

package com.ruoyi.framework.config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;


@Component
public class WebSocketConfig {

? ? /**
? ? ?* ServerEndpointExporter 作用
? ? ?*
? ? ?* 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint
? ? ?*
? ? ?* @return
? ? ?*/
? ? @Bean
? ? public ServerEndpointExporter serverEndpointExporter() {
? ? ? ? return new ServerEndpointExporter();
? ? }
}

WebSocketTest.java

package com.ruoyi.framework.web.service;

import org.springframework.stereotype.Component;

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

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

/**
?* @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
?* 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
?*/
@Component
@ServerEndpoint("/websocket")
public class WebSocketTest {
? ? private Session session;

? ? private static CopyOnWriteArraySet<WebSocketTest> webSockets =new CopyOnWriteArraySet<>();
? ? private static Map<String,Session> sessionPool = new HashMap<String,Session>();

? ? @OnOpen
? ? public void onOpen(Session session, @PathParam(value="shopId")String shopId) {
? ? ? ? this.session = session;
? ? ? ? webSockets.add(this);
? ? ? ? sessionPool.put(shopId, session);
? ? ? ? System.out.println("【websocket消息】有新的连接,总数为:"+webSockets.size());
? ? }

? ? @OnClose
? ? public void onClose() {
? ? ? ? webSockets.remove(this);
? ? ? ? System.out.println("【websocket消息】连接断开,总数为:"+webSockets.size());
? ? }

? ? @OnMessage
? ? public void onMessage(String message) {
? ? ? ? System.out.println("【websocket消息】收到客户端消息:"+message);
? ? }

? ? // 此为广播消息
? ? public void sendAllMessage(String message) {
? ? ? ? for(WebSocketTest webSocket : webSockets) {
? ? ? ? ? ? System.out.println("【websocket消息】广播消息:"+message);
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? webSocket.session.getAsyncRemote().sendText(message);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? // 此为单点消息 (发送文本)
? ? public void sendTextMessage(String shopId, String message) {
? ? ? ? Session session = sessionPool.get(shopId);
? ? ? ? if (session != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? session.getBasicRemote().sendText(message);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? // 此为单点消息 (发送对象)
? ? public void sendObjMessage(String shopId, Object message) {
? ? ? ? Session session = sessionPool.get(shopId);
? ? ? ? if (session != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? session.getAsyncRemote().sendObject(message);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
};
?


?

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 08:11:23  更:2021-07-28 08:13:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 19:09:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码