欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring和Websocket相結(jié)合實現(xiàn)消息的推送

 更新時間:2016年02月01日 10:19:47   投稿:mrr  
這篇文章主要介紹了Spring和Websocket相結(jié)合實現(xiàn)消息的推送的相關(guān)資料,本文介紹的非常詳細(xì)具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧

本文主要有三個步驟

1、用戶登錄后建立websocket連接,默認(rèn)選擇websocket連接,如果瀏覽器不支持,則使用sockjs進(jìn)行模擬連接
2、建立連接后,服務(wù)端返回該用戶的未讀消息
3、服務(wù)端進(jìn)行相關(guān)操作后,推送給某一個用戶或者所有用戶新消息 相關(guān)環(huán)境 Spring4.0.6(要選擇4.0+),tomcat7.0.55

Websocet服務(wù)端實現(xiàn)

WebSocketConfig.java

@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(systemWebSocketHandler(),"/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor());
registry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor())
.withSockJS();
}
@Bean
public WebSocketHandler systemWebSocketHandler(){
return new SystemWebSocketHandler();
}
}

不要忘記在springmvc的配置文件中配置對此類的自動掃描

<context:component-scan base-package="com.ldl.origami.websocket" />

@Configuration
@EnableWebMvc
@EnableWebSocket

這三個大致意思是使這個類支持以@Bean的方式加載bean,并且支持springmvc和websocket,不是很準(zhǔn)確大致這樣,試了一下@EnableWebMvc不加也沒什么影響,@Configuration本來就支持springmvc的自動掃描

registry.addHandler(systemWebSocketHandler(),"/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor())

用來注冊websocket server實現(xiàn)類,第二個參數(shù)是訪問websocket的地址

registry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor())
.withSockJS();
}

這個是使用Sockjs的注冊方法

首先SystemWebSocketHandler.java

public class SystemWebSocketHandler implements WebSocketHandler {
private static final Logger logger;
private static final ArrayList<WebSocketSession> users;
static {
users = new ArrayList<>();
logger = LoggerFactory.getLogger(SystemWebSocketHandler.class);
}
@Autowired
private WebSocketService webSocketService;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
logger.debug("connect to the websocket success......");
users.add(session);
String userName = (String) session.getAttributes().get(Constants.WEBSOCKET_USERNAME);
if(userName!= null){
//查詢未讀消息
int count = webSocketService.getUnReadNews((String) session.getAttributes().get(Constants.WEBSOCKET_USERNAME));
session.sendMessage(new TextMessage(count + ""));
}
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
//sendMessageToUsers();
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if(session.isOpen()){
session.close();
}
logger.debug("websocket connection closed......");
users.remove(session);
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
logger.debug("websocket connection closed......");
users.remove(session);
}
@Override
public boolean supportsPartialMessages() {
return false;
}
/**
* 給所有在線用戶發(fā)送消息
*
* @param message
*/
public void sendMessageToUsers(TextMessage message) {
for (WebSocketSession user : users) {
try {
if (user.isOpen()) {
user.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 給某個用戶發(fā)送消息
*
* @param userName
* @param message
*/
public void sendMessageToUser(String userName, TextMessage message) {
for (WebSocketSession user : users) {
if (user.getAttributes().get(Constants.WEBSOCKET_USERNAME).equals(userName)) {
try {
if (user.isOpen()) {
user.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
}

相關(guān)內(nèi)容大家一看就能明白,就不多解釋了

然后WebSocketHandshakeInterceptor.java

public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {
private static Logger logger = LoggerFactory.getLogger(HandshakeInterceptor.class);
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object
> attributes) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false);
if (session != null) {
//使用userName區(qū)分WebSocketHandler,以便定向發(fā)送消息
String userName = (String) session.getAttribute(Constants.SESSION_USERNAME);
attributes.put(Constants.WEBSOCKET_USERNAME,userName);
}
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
}
}

這個的主要作用是取得當(dāng)前請求中的用戶名,并且保存到當(dāng)前的WebSocketHandler中,以便確定WebSocketHandler所對應(yīng)的用戶,具體可參考HttpSessionHandshakeInterceptor

用戶登錄建立websocket連接

index.jsp

<script type="text/javascript" src="http://localhost:8080/Origami/websocket/sockjs-0.3.min.js"></script>
<script>
var websocket;
if ('WebSocket' in window) {
websocket = new WebSocket("ws://localhost:8080/Origami/webSocketServer");
} else if ('MozWebSocket' in window) {
websocket = new MozWebSocket("ws://localhost:8080/Origami/webSocketServer");
} else {
websocket = new SockJS("http://localhost:8080/Origami/sockjs/webSocketServer");
}
websocket.onopen = function (evnt) {
};
websocket.onmessage = function (evnt) {
$("#msgcount").html("(<font color='red'>"+evnt.data+"</font>)")
};
websocket.onerror = function (evnt) {
};
websocket.onclose = function (evnt) {
}
</script>

使用sockjs時要注意

1、這兩個的寫法

<script type="text/javascript" src="http://localhost:8080/Origami/websocket/sockjs-0.3.min.js"></script>
websocket = new SockJS(http://localhost:8080/Origami/sockjs/webSocketServer);

2、web.xml中

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">

version

web-app_3_1.xsd

這兩個的版本都要是3.0+

然后在這個servlet中加入

<async-supported>true</async-supported>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>

然后所有的filter中也加入

<async-supported>true</async-supported>

3、添加相關(guān)依賴

<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>

好了,現(xiàn)在websocket可以正常建立起來了

返回用戶未讀的消息

當(dāng)連接建立后,會進(jìn)入SystemWebSocketHandler的afterConnectionEstablished方法,代碼看上邊,取出WebSocketHandshakeInterceptor中保存的用戶名

查詢信息后使用session.sendMessage(new TextMessage(count + ""));返回給用戶,從哪來回哪去

服務(wù)端推送消息給用戶

@Controller
public class AdminController {
static Logger logger = LoggerFactory.getLogger(AdminController.class);
@Autowired(required = false)
private AdminService adminService;
@Bean
public SystemWebSocketHandler systemWebSocketHandler() {
return new SystemWebSocketHandler();
}
@RequestMapping("/auditing")
@ResponseBody
public String auditing(HttpServletRequest request){
//無關(guān)代碼都省略了
int unReadNewsCount = adminService.getUnReadNews(username);
systemWebSocketHandler().sendMessageToUser(username, new TextMessage(unReadNewsCount + ""));
return result;
}
}

在這里可以使用sendMessageToUser給某個用戶推送信息,也可以使用sendMessageToUsers給所有用戶推送信息

相關(guān)文章

  • 詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系

    詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系

    這篇文章主要介紹了詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系,本文先對兩種方法作了介紹,然后對二者聯(lián)系進(jìn)行分析,具有一定參考價值,需要的朋友可以了解下。
    2017-09-09
  • kotlin和Java的相互調(diào)用示例詳解

    kotlin和Java的相互調(diào)用示例詳解

    Kotlin 的設(shè)計過程中就考慮到了與 Java 的互操作性。在 Kotlin 中可以直接調(diào)用既有的 Java 代碼, 反過來在 Java 中也可以很流暢地使用 Kotlin 代碼,下面這篇文章主要給大家介紹了關(guān)于kotlin和Java的相互調(diào)用的相關(guān)資料,需要的朋友可以參考下。
    2018-02-02
  • MyBatis框架迭代器模式實現(xiàn)原理解析

    MyBatis框架迭代器模式實現(xiàn)原理解析

    這篇文章主要介紹了MyBatis框架迭代器模式實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • idea2020中復(fù)制一個微服務(wù)實例的方法

    idea2020中復(fù)制一個微服務(wù)實例的方法

    這篇文章主要介紹了idea2020中復(fù)制一個微服務(wù)實例的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • mybatis Reflector反射類的具體使用

    mybatis Reflector反射類的具體使用

    Reflector類是MyBatis反射模塊的核心,負(fù)責(zé)處理類的元數(shù)據(jù),以實現(xiàn)屬性與數(shù)據(jù)庫字段之間靈活映射的功能,本文主要介紹了mybatis Reflector反射類的具體使用,感興趣的可以了解一下
    2024-02-02
  • Spring Boot 項目啟動失敗的解決方案

    Spring Boot 項目啟動失敗的解決方案

    這篇文章主要介紹了Spring Boot 項目啟動失敗的解決方案,幫助大家更好的理解和學(xué)習(xí)使用Spring Boot,感興趣的朋友可以了解下
    2021-03-03
  • 解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題

    解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題

    這篇文章主要介紹了解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 詳解如何在spring boot中使用spring security防止CSRF攻擊

    詳解如何在spring boot中使用spring security防止CSRF攻擊

    這篇文章主要介紹了詳解如何在spring boot中使用spring security防止CSRF攻擊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 徹底搞定堆排序:二叉堆

    徹底搞定堆排序:二叉堆

    二叉堆有兩種:最大堆和最小堆。最大堆:父結(jié)點的鍵值總是大于或等于任何一個子節(jié)點的鍵值;最小堆:父結(jié)點的鍵值總是小于或等于任何一個子節(jié)點的鍵值
    2021-07-07
  • 官方詳解HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)

    官方詳解HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)

    這篇文章主要為大家介紹了HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)的?官方詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評論