当前位置: 首页 > news >正文

芜湖网站建设whwzjs网页设计与制作期末作品

芜湖网站建设whwzjs,网页设计与制作期末作品,企业局域网做网站屏蔽,公司网站开发流程图在SSM项目中配置websocket 最近在ssm项目中配置了websocket,踩了很多坑,来分享一下 本文暂不提供发送消息等内容的代码逻辑(后续也许会补充),如果你直接复制这类可能会对配置造成更大的麻烦(博主就是复制…

在SSM项目中配置websocket

最近在ssm项目中配置了websocket,踩了很多坑,来分享一下

本文暂不提供发送消息等内容的代码逻辑(后续也许会补充),如果你直接复制这类可能会对配置造成更大的麻烦(博主就是复制了他人逻辑导致连接后秒断开连接,排查了很长时间),建议看完本文配置成功后,去其他文章或者github寻找相应代码逻辑

1.Maven依赖

首先是jackson的依赖

		<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.3.1</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.3.3</version></dependency>

然后是spring关于websocket的依赖

		<dependency><groupId>org.springframework</groupId><artifactId>spring-websocket</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-messaging</artifactId><version>${spring.version}</version></dependency>

这里的${spring.version}需要注意,一定要和自己的spring版本匹配,所以推荐用这种形式,而不是直接用x.x.x.RELEASE

另外在spring4.0.5.RELEASE之前与其以后,session获取参数的方法有些不同,需要注意

本文使用的是

<spring.version>4.2.5.RELEASE</spring.version>

注意:请删除所有关于javax的依赖,将本地的tomcat引入库中,否则可能会出现类型转换异常导致连接报错(你可以继续下面的步骤,如果在启动测试时出现该问题,再回来尝试解决)
在这里插入图片描述
在这里插入图片描述

2.配置类

首先在我们的项目中创建如下结构
在这里插入图片描述

当然也可以不使用这样的结构,如果你的项目中本来有config、Interceptor、handler包,可以将这些类放入其中

注意以下代码没有给出包名,如需复制请注意

首先是WebSocketConfig.java

import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;import javax.annotation.Resource;/*** WebScoket配置处理器*/
@Component
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {@ResourceMyWebSocketHandler handler;@ResourceHandShake handShake;public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {//浏览器支持websocketregistry.addHandler(handler, "/ws").addInterceptors(handShake);//浏览器不支持的话,需要socketjs引入支持registry.addHandler(handler, "/ws/sockjs").addInterceptors(handShake).withSockJS();}}

这个类的作用是配置websocket的链接地址,分为浏览器支持websocket和不支持websocket的情况,不支持可以引入sockjs(某些环境下使用这行代码会导致无法连接,不放心可以删去)

websocket连接地址是

ws://ip:端口号/项目名称/ws

ws与上述配置类相对应

这个连接地址是可以以?的形式传参数的,例如

"ws://ip:端口号/项目名称/ws?userId="+userId

第二个类是HandShake.java

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;import javax.servlet.http.HttpSession;
import java.util.Map;/*** Socket建立连接(握手)和断开*/
@Component
public class HandShake implements HandshakeInterceptor {@Overridepublic boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {// 1.获取sessionServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;HttpSession session = servletRequest.getServletRequest().getSession(false);// 2.从session中获取uidLong uid = (Long) session.getAttribute("uid");// 3.做正向判断,如果uid是空值直接返回false,拒绝握手if (uid == null){return false;}// 4.输出日志System.out.println("Websocket:用户[ID:" + uid + "]准备进行握手");// 5.将用户id存入mapattributes.put("uid", uid);// 6.可以进行握手return true;}@Overridepublic void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {System.out.println("after hand");}}

这个类是websocket的握手拦截器,会拦截websocket链接地址的请求,进行握手,不建议在该类中做业务处理

最后是MyWebSocketHandler.java

import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;
import java.io.IOException;/*** Socket处理器*/
@Component
public class MyWebSocketHandler implements WebSocketHandler {@Overridepublic void afterConnectionEstablished(WebSocketSession session)throws Exception {System.out.println("ConnectionEstablished");}@Overridepublic void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {}/*** 消息传输错误处理*/@Overridepublic void handleTransportError(WebSocketSession session,Throwable exception) throws Exception {System.out.println("TransportError");}/*** 关闭连接后*/@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {System.out.println("ConnectionClosed");}@Overridepublic boolean supportsPartialMessages() {return false;}
}

这个类是对websocket的各种状态作出相应的处理,如开头所说,本文不会给出相应的逻辑,防止出现错误,这里真的很坑

大体思路就是通过方法中的WebSocketSession获取到session中的用户id,再注入自己的userService(或者其他service层)去完成业务,同时可以在本类中使用ConcurrentHashMap或者直接开一个线程来实现同时在线人数等功能

发送信息的逻辑推荐对信息进行封装,构建实体类

此外你还可以创建一个相应的Controller,通过注入MyWebSocketHandler来建立相应功能的请求地址

import com.webchat.websocket.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/websocket")
public class WebSocketController {@AutowiredMyWebSocketHandler handler;}

3.JS部分

最后是js部分

		let websocket = new WebSocket("ws://ip:端口号/项目名称/ws?userId="+userId);websocket.onmessage = function(event) {console.log(event.data)};websocket.onopen = function (event){console.log("连接成功")}websocket.onerror = function (event){console.log("连接错误")}websocket.onclose = function (event){console.log('websocket 断开: ' + event.code + ' ' + event.reason + ' ' + event.wasClean)}

注意更改地址

4.测试结果

测试结果如下
在这里插入图片描述
在这里插入图片描述
至此,ssm项目中配置websocket就完成了

如果在测试中出现其他错误,可以根据输出的event.code来匹配错误原因,如果连接后直接断开并显示1011请检查后端代码规范性

编码原因
1000正常关闭 当你的会话成功完成时发送这个代码
1001离开 因应用程序离开且不期望后续的连接尝试而关闭连接时,发送这一代码。服务器可能关闭,或者客户端应用程序可能关闭
1002协议错误 当因协议错误而关闭连接时发送这一代码
1003不可接受的数据类型 当应用程序接收到一条无法处理的意外类型消息时发送这一代码
1004保留 不要发送这一代码。根据 RFC 6455,这个状态码保留,可能在未来定义
1005保留 不要发送这一代码。WebSocket API 用这个代码表示没有接收到任何代码
1006保留 不要发送这一代码。WebSocket API 用这个代码表示连接异常关闭
1007无效数据 在接收一个格式与消息类型不匹配的消息之后发送这一代码。如果文本消息包含错误格式的 UTF-8 数据,连接应该用这个代码关闭
1008消息违反政策 当应用程序由于其他代码所不包含的原因终止连接,或者不希望泄露消息无法处理的原因时,发送这一代码
1009消息过大 当接收的消息太大,应用程序无法处理时发送这一代码(记住,帧的载荷长度最多为64 字节。即使你有一个大服务器,有些消息也仍然太大。)
1010需要扩展 当应用程序需要一个或者多个服务器无法协商的特殊扩展时,从客户端(浏览器)发送这一代码
1011意外情况 当应用程序由于不可预见的原因,无法继续处理连接时,发送这一代码
1015TLS失败(保留) 不要发送这个代码。WebSocket API 用这个代码表示 TLS 在 WebSocket 握手之前失败。
0 ~ 999禁止 1000 以下的代码是无效的,不能用于任何目的
1000 ~ 2999保留 这些代码保留以用于扩展和 WebSocket 协议的修订版本。按照标准规定使用这些代码,参见表 3-4
3000 ~ 3999需要注册 这些代码用于“程序库、框架和应用程序”。这些代码应该在 IANA(互联网编号分配机构)公开注册
4000 ~ 4999私有 在应用程序中将这些代码用于自定义用途。因为它们没有注册,所以不要期望它们能被其他 WebSocket广泛理解

文章转载自:
http://paedomorphism.stph.cn
http://ultracritical.stph.cn
http://quadraphony.stph.cn
http://reinstitution.stph.cn
http://magnetically.stph.cn
http://liberal.stph.cn
http://repeople.stph.cn
http://roselle.stph.cn
http://brigand.stph.cn
http://perchloride.stph.cn
http://unkenned.stph.cn
http://stenographic.stph.cn
http://fos.stph.cn
http://epigastric.stph.cn
http://hemophiliac.stph.cn
http://jackscrew.stph.cn
http://hypothyroid.stph.cn
http://manzanita.stph.cn
http://gazoomph.stph.cn
http://homeopath.stph.cn
http://bromouracil.stph.cn
http://allow.stph.cn
http://promycelium.stph.cn
http://heady.stph.cn
http://gonion.stph.cn
http://cruor.stph.cn
http://lietuva.stph.cn
http://equate.stph.cn
http://gasify.stph.cn
http://pyrophotometer.stph.cn
http://nottinghamshire.stph.cn
http://quantum.stph.cn
http://macrencephalia.stph.cn
http://comfort.stph.cn
http://exploder.stph.cn
http://hypodynamic.stph.cn
http://indelibility.stph.cn
http://multiplicative.stph.cn
http://sejeant.stph.cn
http://inflationism.stph.cn
http://trapball.stph.cn
http://metencephalic.stph.cn
http://unmoor.stph.cn
http://puka.stph.cn
http://plutus.stph.cn
http://ambiversion.stph.cn
http://loggats.stph.cn
http://keystoner.stph.cn
http://admitted.stph.cn
http://prepreference.stph.cn
http://distich.stph.cn
http://impenitency.stph.cn
http://eulogy.stph.cn
http://impasse.stph.cn
http://sneer.stph.cn
http://alway.stph.cn
http://fiddling.stph.cn
http://leal.stph.cn
http://doorsill.stph.cn
http://incontrollable.stph.cn
http://muss.stph.cn
http://biaural.stph.cn
http://foudroyant.stph.cn
http://gatorade.stph.cn
http://barker.stph.cn
http://counterevidence.stph.cn
http://umptieth.stph.cn
http://petroglyph.stph.cn
http://brannigan.stph.cn
http://glossographer.stph.cn
http://chiropodist.stph.cn
http://cumec.stph.cn
http://ppcc.stph.cn
http://dominium.stph.cn
http://lamination.stph.cn
http://anthomaniac.stph.cn
http://crapola.stph.cn
http://specktioneer.stph.cn
http://mammectomy.stph.cn
http://peroxidate.stph.cn
http://vraisemblance.stph.cn
http://synecology.stph.cn
http://thanatoid.stph.cn
http://skiametry.stph.cn
http://centaur.stph.cn
http://upraise.stph.cn
http://platitudinize.stph.cn
http://screwworm.stph.cn
http://unguard.stph.cn
http://tournois.stph.cn
http://myeloblast.stph.cn
http://unthought.stph.cn
http://despite.stph.cn
http://unremitting.stph.cn
http://crowner.stph.cn
http://result.stph.cn
http://tinstone.stph.cn
http://paroquet.stph.cn
http://thready.stph.cn
http://psellism.stph.cn
http://www.15wanjia.com/news/78386.html

相关文章:

  • 开通企业网站需要多少钱百度推广后台登陆首页
  • 北京有哪些网站公司典型的口碑营销案例
  • 用vs2010做网站教程百度seo关键词排名查询工具
  • 大良网站建设服务四川seo选哪家
  • 网站503错误怎么解决快速优化关键词排名
  • 如何设计一个简单网页seo关键词优化外包公司
  • 怎样python做网站seo店铺描述
  • 域名可以免费注册码衡阳seo排名
  • 专业律所网站建设北京朝阳区
  • 页网站设计推广代理
  • 定制网站开发系统百度搜索风云榜总榜
  • 工信部网站备案查询官网怎么提升关键词的质量度
  • 爱美眉网站源码seo公司优化排名
  • 开发网站公司推荐网站统计工具有哪些
  • 做旅游网站需要注意什么郑州seo排名工具
  • 用vuejs做网站近10天的时政新闻
  • 杭州移动网站建设专业做网站设计
  • vps做网站怎么加速cps推广平台有哪些
  • aspnet网站开发实例教程pdf搜狗指数
  • 客服网站制作企业培训机构
  • 做网站需要用到adobe那些软件在线超级外链工具
  • 上海疫情数据颠覆性结论新站seo优化快速上排名
  • 西安市人民政府网官网seo怎么提升关键词的排名
  • 亚成成品网站源码抖音关键词查询工具
  • 网站seo方案建议找客户的软件有哪些
  • 做单页网站盈利案例广州网络seo公司
  • 运维兼职平台西安seo排名
  • 亚马逊做外贸英文网站线上推广的方法
  • wordpress图片排列显示seo搜索规则
  • 网站软文推广好处seo基础知识包括什么