Java中Spring WebSocket详解
首先pom.xml
org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE org.apache.commons commons-io javax.websocket javax.websocket-api 1.0 provided org.springframework spring-websocket org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-undertow
接收消息后的处理类GameHandler:
importjava.net.URI; importorg.springframework.web.socket.BinaryMessage; importorg.springframework.web.socket.CloseStatus; importorg.springframework.web.socket.PongMessage; importorg.springframework.web.socket.TextMessage; importorg.springframework.web.socket.WebSocketSession; importorg.springframework.web.socket.handler.AbstractWebSocketHandler; publicclassGameHandlerextendsAbstractWebSocketHandler{ /** *处理字符串类的信息 * *@paramsession *@parammessage *@throwsException */ @Override protectedvoidhandleTextMessage(WebSocketSessionsession,TextMessagemessage)throwsException{ session.sendMessage(newTextMessage(message.asBytes())); } /** *处理二进制类的信息 * *@paramsession *@parammessage *@throwsException */ @Override protectedvoidhandleBinaryMessage(WebSocketSessionsession,BinaryMessagemessage)throwsException{ session.sendMessage(newBinaryMessage(message.getPayload())); } /** *ping-pong * *@paramsession *@parammessage *@throwsException */ @Override protectedvoidhandlePongMessage(WebSocketSessionsession,PongMessagemessage)throwsException{ } /** *传出错误的处理 * *@paramsession *@paramexception *@throwsException */ @Override publicvoidhandleTransportError(WebSocketSessionsession,Throwableexception)throwsException{ } /** *连接关闭的处理 * *@paramsession *@paramstatus *@throwsException */ @Override publicvoidafterConnectionClosed(WebSocketSessionsession,CloseStatusstatus)throwsException{ } /** *连接建立后的处理 * *@paramsession *@throwsException */ @Override publicvoidafterConnectionEstablished(WebSocketSessionsession)throwsException{ } }
握手信息拦截器WebSocketHandshakeInterceptor:
importjava.util.Map; importjavax.servlet.http.Cookie; importorg.springframework.http.server.ServerHttpRequest; importorg.springframework.http.server.ServerHttpResponse; importorg.springframework.http.server.ServletServerHttpRequest; importorg.springframework.web.socket.WebSocketHandler; importorg.springframework.web.socket.server.HandshakeInterceptor; publicclassWebSocketHandshakeInterceptorimplementsHandshakeInterceptor{ @Override publicbooleanbeforeHandshake(ServerHttpRequestrequest,ServerHttpResponseshr1,WebSocketHandlerwsh,Mapattributes)throwsException{ //此处可以做一些权限认证的事情或者其他 returntrue; } @Override publicvoidafterHandshake(ServerHttpRequestshr,ServerHttpResponseshr1,WebSocketHandlerwsh,Exceptionexcptn){ } }
使用WebSocket的配置类WebSocketConfig:
importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; importorg.springframework.web.socket.config.annotation.EnableWebSocket; importorg.springframework.web.socket.config.annotation.WebSocketConfigurer; importorg.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @Configuration @EnableWebSocket publicclassWebSocketConfigextendsWebMvcConfigurerAdapterimplementsWebSocketConfigurer{ @Override publicvoidregisterWebSocketHandlers(WebSocketHandlerRegistryregistry){ //允许连接的域,只能以http或https开头 String[]allowsOrigins={"http://127.0.0.1:1213","http://localhost:1213"}; registry.addHandler(gameHandler(),"/game").addInterceptors(handshakeInterceptor()).setAllowedOrigins(allowsOrigins); } @Bean publicGameHandlergameHandler(){ returnnewGameHandler(); } @Bean publicWebSocketHandshakeInterceptorhandshakeInterceptor(){ returnnewWebSocketHandshakeInterceptor(); } }
启动类Launcher:
@SpringBootApplication publicclassLauncher{ publicstaticvoidmain(String[]params){ SpringApplication.run(Launcher.class,params); } }
配置文件main/resources/application.properties:
server.port=1213 server.session-timeout=1800 server.undertow.io-threads=4 server.undertow.worker-threads=20 server.undertow.buffer-size=1024 server.undertow.buffers-per-region=1024 server.undertow.direct-buffers=true
前端的测试页面main\resources\static\index.html
PlatformGateway WebSocketTestPage
WebSocketAddress: ws://127.0.0.1:1213/