springboot 中使用websocket简单例子文档格式.docx

上传人:b****5 文档编号:15706209 上传时间:2022-11-15 格式:DOCX 页数:6 大小:15.68KB
下载 相关 举报
springboot 中使用websocket简单例子文档格式.docx_第1页
第1页 / 共6页
springboot 中使用websocket简单例子文档格式.docx_第2页
第2页 / 共6页
springboot 中使用websocket简单例子文档格式.docx_第3页
第3页 / 共6页
springboot 中使用websocket简单例子文档格式.docx_第4页
第4页 / 共6页
springboot 中使用websocket简单例子文档格式.docx_第5页
第5页 / 共6页
点击查看更多>>
下载资源
资源描述

springboot 中使用websocket简单例子文档格式.docx

《springboot 中使用websocket简单例子文档格式.docx》由会员分享,可在线阅读,更多相关《springboot 中使用websocket简单例子文档格式.docx(6页珍藏版)》请在冰豆网上搜索。

springboot 中使用websocket简单例子文档格式.docx

importorg.springframework.context.annotation.Configuration;

importorg.springframework.web.socket.config.annotation.EnableWebSocket;

importorg.springframework.web.socket.config.annotation.WebSocketConfigurer;

importorg.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

importorg.springframework.web.socket.server.standard.ServerEndpointExporter;

/**

*websocket配置

*@authorxiaojf2017/3/29:

50.

*/

@Configuration

@EnableWebSocket

publicclassWebSocketConfigimplementsWebSocketConfigurer{

@Bean

publicServerEndpointExporterserverEndpointExporter(ApplicationContextcontext){

returnnewServerEndpointExporter();

}

@Override

publicvoidregisterWebSocketHandlers(WebSocketHandlerRegistryregistry){

registry.addHandler(newCountWebSocketHandler(),"

/web/count"

).addInterceptors(newHandshakeInterceptor());

}

消息拦截处理

packagecn.xiaojf.today.ws.interceptor;

importorg.springframework.http.server.ServerHttpRequest;

importorg.springframework.http.server.ServerHttpResponse;

importorg.springframework.web.socket.WebSocketHandler;

importorg.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

importjava.util.Map;

*消息拦截处理类

*@authorxiaojf2017/3/210:

36.

publicclassHandshakeInterceptorextendsHttpSessionHandshakeInterceptor{

publicbooleanbeforeHandshake(ServerHttpRequestrequest,ServerHttpResponseresponse,WebSocketHandlerwsHandler,

Map&

lt;

String,Object&

gt;

attributes)throwsException{

//解决Theextension[x-webkit-deflate-frame]isnotsupported问题

if(request.getHeaders().containsKey("

Sec-WebSocket-Extensions"

)){

request.getHeaders().set("

"

permessage-deflate"

);

System.out.println("

BeforeHandshake"

returnsuper.beforeHandshake(request,response,wsHandler,attributes);

publicvoidafterHandshake(ServerHttpRequestrequest,ServerHttpResponseresponse,WebSocketHandlerwsHandler,

Exceptionex){

AfterHandshake"

super.afterHandshake(request,response,wsHandler,ex);

消息处理相应类

packagecn.xiaojf.today.ws.handler;

importcom.alibaba.druid.support.json.JSONUtils;

importmons.collections.map.HashedMap;

importorg.springframework.stereotype.Component;

importorg.springframework.web.socket.CloseStatus;

importorg.springframework.web.socket.TextMessage;

importorg.springframework.web.socket.WebSocketSession;

importorg.springframework.web.socket.handler.TextWebSocketHandler;

importjava.io.IOException;

importjava.util.*;

55.

@Component

publicclassCountWebSocketHandlerextendsTextWebSocketHandler{

privatestaticlongcount=0;

privatestaticMap&

String,WebSocketSession&

sessionMap=newHashedMap();

protectedvoidhandleTextMessage(WebSocketSessionsession,TextMessagemessage)throwsException{

//session.sendMessage(newTextMessage(session.getPrincipal().getName()+"

你是第"

+(sessionMap.size())+"

位访客"

));

//p2p

Objectparse=JSONUtils.parse(message.getPayload());

Collection&

WebSocketSession&

sessions=sessionMap.values();

for(WebSocketSessionws:

sessions){//广播

ws.sendMessage(message);

sendMessage(sessionMap.keySet(),"

你好"

publicvoidafterConnectionEstablished(WebSocketSessionsession)throwsException{

sessionMap.put(session.getPrincipal().getName(),session);

super.afterConnectionEstablished(session);

publicvoidafterConnectionClosed(WebSocketSessionsession,CloseStatusstatus)throwsException{

sessionMap.remove(session.getPrincipal().getName());

super.afterConnectionClosed(session,status);

/**

*发送消息

*@authorxiaojf2017/3/211:

43

publicstaticvoidsendMessage(Stringusername,Stringmessage)throwsIOException{

sendMessage(Arrays.asList(username),Arrays.asList(message));

publicstaticvoidsendMessage(Collection&

String&

acceptorList,Stringmessage)throwsIOException{

sendMessage(acceptorList,Arrays.asList(message));

*发送消息,p2p群发都支持

acceptorList,Collection&

msgList)throwsIOException{

if(acceptorList!

=null&

amp;

&

msgList!

=null){

for(Stringacceptor:

acceptorList){

WebSocketSessionsession=sessionMap.get(acceptor);

if(session!

for

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 自我管理与提升

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1