做画册找什么网站,wordpress 侧边栏头像,上海网站建设特点,绵阳网站托管其实使用消息队列也可以实现会话#xff0c;直接前端监听指定的队列#xff0c;使用rabbitmq的分组还可以实现不同群聊的效果。 1、依赖搭建#xff1a; ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org… 其实使用消息队列也可以实现会话直接前端监听指定的队列使用rabbitmq的分组还可以实现不同群聊的效果。 1、依赖搭建 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdWebStock/artifactIdversion1.0-SNAPSHOT/versionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.8/versionrelativePath//parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-websocket/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependenciespropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/projectSpringBoot都已集成完毕了不用使用原生的WebStock。 2、配置运行 如果是工作中要单独起一个服务来操作这个比较好反正是微服务多一个少一个没啥的 WebStock连接配置、 package com.quxiao.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;import java.time.LocalDateTime;/*** ws消息处理类*/
Component
Slf4j
public class MyWsHandler extends AbstractWebSocketHandler {AutowiredWsService wsService;Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {log.info(建立ws连接);WsSessionManager.add(session.getId(), session);}Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {log.info(发送文本消息);// 获得客户端传来的消息String payload message.getPayload();//这里也是一样,要取出前端传来的参数,判断发给谁.这里我就是发给了连接客户端自己.log.info(server 接收到消息 payload);wsService.sendMsg(session, server 发送给的消息 payload 发送时间: LocalDateTime.now().toString());String url session.getUri().toString();//使用?拼接参数,后端取出判断发给谁System.out.println(获取到的参数: url.substring(url.indexOf(?) 1));}Overrideprotected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {log.info(发送二进制消息);}Overridepublic void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {log.error(异常处理);WsSessionManager.removeAndClose(session.getId());}Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {log.info(关闭ws连接);WsSessionManager.removeAndClose(session.getId());}
}package com.quxiao.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;Slf4j
public class WsSessionManager {/*** 保存连接 session 的地方*/public static ConcurrentHashMapString, WebSocketSession SESSION_POOL new ConcurrentHashMap();/*** 添加 session** param key*/public static void add(String key, WebSocketSession session) {// 添加 sessionSESSION_POOL.put(key, session);}/*** 删除 session,会返回删除的 session** param key* return*/public static WebSocketSession remove(String key) {// 删除 sessionreturn SESSION_POOL.remove(key);}/*** 删除并同步关闭连接** param key*/public static void removeAndClose(String key) {WebSocketSession session remove(key);if (session ! null) {try {// 关闭连接session.close();} catch (IOException e) {// todo: 关闭出现异常处理e.printStackTrace();}}}/*** 获得 session** param key* return*/public static WebSocketSession get(String key) {// 获得 sessionreturn SESSION_POOL.get(key);}
}package com.quxiao.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;Configuration
EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {Autowiredprivate MyWsHandler myWsHandler;Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {registry.addHandler(myWsHandler, myWs)//允许跨域.setAllowedOrigins(*);}
}这里有几个后续逻辑 (1)、连接时通过前端的参数或者对其按登陆人信息绑定连接消息。 (2)、收到一个消息发送给谁就得需要前端传来参数例如某个群的id然后通过群绑定人员因为(1)中通过keyvalue人员id为keyvalue时连接信息。 直接遍历这个群下面的所有人员id获取已经连接的信息发送给他们。这里还要搞一个表存储群消息log。这样其他群员连接时可以获取到以往的消息。 发送消息工具类 package com.quxiao.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;import java.io.IOException;/*** ws操作相关服务*/
Service
Slf4j
public class WsServiceUtil {/*** 发送消息* param session* param text* return* throws IOException*/public void sendMsg(WebSocketSession session, String text) throws IOException {session.sendMessage(new TextMessage(text));}/*** 广播消息* param text* return* throws IOException*/public void broadcastMsg(String text) throws IOException {for (WebSocketSession session: WsSessionManager.SESSION_POOL.values()) {session.sendMessage(new TextMessage(text));}}}这里广播我就是遍历了储存连接消息的map容器。 package com.quxiao.controller;import com.quxiao.config.WsServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;RestController
RequestMapping(/put)
public class MessageHandler {AutowiredWsServiceUtil wsServiceUtil;PostMapping(/t1/{test})public void t1(PathVariable(test) String text) {try {wsServiceUtil.broadcastMsg(Thread.currentThread().getName() : text);} catch (Exception e) {throw new RuntimeException(e);}}}3、 前端测试页面 !DOCTYPE HTML
html
headtitleMy WebSocket/title
/headbody
input idtext typetext /
button onclicksend()Send/button
button onclickcloseWebSocket()Close/button
div idmessage/div/bodyscript typetext/javascriptlet ws null;//判断当前浏览器是否支持WebSocketif (WebSocket in window) {ws new WebSocket(ws://localhost:8080/myWs?2002);}else {alert(当前浏览器 Not support websocket)}//连接发生错误的回调方法ws.onerror function () {setMessageInnerHTML(WebSocket连接发生错误);};//连接成功建立的回调方法ws.onopen function(event) {console.log(ws调用连接成功回调方法)//ws.send()}//接收到消息的回调方法ws.onmessage function(message) {console.log(接收消息 message.data);if (typeof(message.data) string) {setMessageInnerHTML(message.data);}}//ws连接断开的回调方法ws.onclose function(e) {console.log(ws连接断开)//console.log(e)setMessageInnerHTML(ws close);}//将消息显示在网页上function setMessageInnerHTML(innerHTML) {console.log(innerHTML)document.getElementById(message).innerHTML 接收的消息: innerHTML br/;}//关闭连接function closeWebSocket() {ws.close();}//发送消息function send(msg) {if(!msg){msg document.getElementById(text).value;document.getElementById(message).innerHTML 发送的消息: msg br/;ws.send(msg);}}
/script
/html 4、总结 所以最重要的是这个消息发给谁 后端要做的就是需要根据不同的群、人员标识符去发送消息 前端需要做的就是 传入不同的标识符如果是私聊就得传人员id如果是群聊就需要传入群id。
文章转载自: http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.jnzfs.cn.gov.cn.jnzfs.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn http://www.morning.ngjpt.cn.gov.cn.ngjpt.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.rqsnl.cn.gov.cn.rqsnl.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn