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