请问 new WebSocket Request method 'GET' not supported
文档:org.springframework.web.HttpRequestM...
链接:http://note.youdao.com/noteshare?id=ace40fb8c842a63540618b1004748ef8&sub=60D4AC6B14594D6CB840D9757C15757F
代码 报错
http://note.youdao.com/noteshare?id=ace40fb8c842a63540618b1004748ef8&sub=60D4AC6B14594D6CB840D9757C15757F
package com.starp.exam.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.starp.util.JsonUtil;
import top.starp.util.ReturnT;
import top.starp.util.StringUtils;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* 新订单推送socket
*
* @author: pansong
* @date: 2020-06-01 11:25
**/
@Slf4j
//@ServerEndpoint("/webSocket/addOrder/{userId}")
@ServerEndpoint("/webSocket/{userId}")
@Component
//WebSocketServer.java
@Data
public class WebSocketServer {
/**
* session 与客户端的连接对话,需要通过其给客户端发送消息
* userId 用户唯一标识
* webSocketMap 存放已连接的客户端信息——ConcurrentHashMap是线程安全的
*/
private Session session;
private String userId;
private static ConcurrentMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
WebSocketServerListener webSocketServerListener;
/***
* 连接建立
*
* @param session 客户端对话
* @param userId 用户id
* @return: void
* @author: pansong
* @date: 2020/6/11 23:45
**/
@OnOpen
public void onOpen(Session session, @PathParam("userId") String userId) {
this.session = session;
this.userId = userId;
webSocketMap.put(userId, this);
log.info(userId + ":连接,在线人数:" + webSocketMap.size());
this.sendMsg("pong");
if(webSocketServerListener!=null){
webSocketServerListener.onOpen(session,userId);
}
// webSocketServerListener.onOpen(session,userId);
}
/***
* 连接关闭
*
* @param
* @return: void
* @author: pansong
* @date: 2020/6/11 23:45
**/
@OnClose
public void onClose() {
if (this.userId != null && webSocketMap.containsKey(this.userId)) {
webSocketMap.remove(this.userId);
}
log.info(this.userId + ":退出,在线人数:" + webSocketMap.size());
if(webSocketServerListener!=null){
webSocketServerListener.onClose();
}
}
/***
* 连接出错
*
* @param throwable
* @return: void
* @author: pansong
* @date: 2020/6/11 23:46
**/
@OnError
public void onError(Throwable throwable) {
log.error(this.userId + ":错误,原因:" + throwable.getMessage());
if (webSocketMap.containsKey(this.userId)) {
webSocketMap.remove(this.userId);
}
if(webSocketServerListener!=null){
webSocketServerListener.onError(throwable);
}
}
/***
* 推送消息到前端
*
* @param msg 消息内容
* @return: void
* @author: pansong
* @date: 2020/6/11 23:46
**/
public Boolean sendMsg(String msg) {
try {
this.session.getBasicRemote().sendText(msg);
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
return false;
}
return true;
}
static String screenshot = "screenshot";
/**
* let data = {
* msg:"status",
* deviceid:state.deviceId,
* state: state.wsStatus
* }
*/
static String status = "status";
/**
* 设备状态
*
* @param jsonObject
*/
void onStatusChangeMsg(JSONObject jsonObject) {
log.info("onStatusChangeMsg jsonObject {}",jsonObject);
/**
* 收到消息
*
* @param msg 消息内容
* @return: void
* @author: pansong
* @date: 2020/6/11 23:48
**/
@OnMessage
public void onMessage(String msg) {
log.info("msg {}", msg);
if ( StringUtils.isNotBlank(msg)) {
if ("ping".equals(msg)) {
log.info("get ping");
this.sendMsg("pong");
} else {
try {
log.info(this.userId + ":消息,报文:" + msg);
//解析报文
JSONObject jsonObject = JSON.parseObject(msg);
String msgType = jsonObject.getString("msg");
jsonObject.put("sendUserId", this.userId);
String receiveUserId = jsonObject.getString("receiveUserId");
JSONObject success = ReturnT.success(jsonObject);
String stringRes = JsonUtil.objectToString(success);
log.info("stringRes {}", stringRes);
//
if ( StringUtils.isNotBlank(receiveUserId)) {
log.info("单发 receiveUserId {}", receiveUserId);
if (webSocketMap.containsKey(receiveUserId)) {
String res = jsonObject.toJSONString();
log.info("res {}", res);
//
log.info("string {}", stringRes);
webSocketMap.get(receiveUserId).sendMsg(stringRes);
}
} else {//不存在 则群发
log.info("不存在 则群发");
webSocketMap.forEach((k, v) -> {
// v.sendMsg(msg);
v.sendMsg(stringRes);
});
}
} catch (Exception e) {
e.printStackTrace();
this.sendMsg("pong");
}
}
}
}
/**
* 自定义推送消息
*
* @param msg 消息内容
* @param userId 用户id
* @return: java.lang.Boolean
* @author: pansong
* @date: 2020/6/11 23:49
**/
public static Boolean sendInfo(String msg, String userId) throws IOException {
boolean notBlank = StringUtils.isNotBlank(userId);
// if (org.apache.commons.lang.StringUtils.isNotBlank(userId)
if (notBlank
&& webSocketMap.containsKey(userId)) {
webSocketMap.get(userId).sendMsg(msg);
log.info(userId + " : 发送,报文:" + msg);
return true;
} else {
log.error(userId + " : 掉线");
return false;
}
}
public static Boolean 群发(String msg) throws IOException {
webSocketMap.forEach((k, v) -> {
// v.sendMsg(msg);
v.sendMsg(msg);
});
return true;
}
public static Boolean massSend(String msg) throws IOException {
webSocketMap.forEach((k, v) -> {
// v.sendMsg(msg);
v.sendMsg(msg);
});
return true;
}
}
<template>
<div class="app-container">
<el-form
:model="queryParams"
ref="queryForm"
size="small"
label-width="100px"
>
<el-form-item label="类型" prop="type">
<el-select
v-model="queryParams.type"
clearable
placeholder="请选择类型"
>
<el-option value="1" label="全服执行SQL"></el-option>
<el-option value="2" label="查看日志"></el-option>
</el-select>
</el-form-item>
<el-form-item label="SQL语句">
<el-input
type="textarea"
:rows="3"
v-model="queryParams.msg"
placeholder="请输入SQL语句"
clearable
/>
</el-form-item>
<el-form-item>
<el-button
type="primary"
icon="el-icon-search"
size="mini"
@click="submit"
>提 交</el-button
>
</el-form-item>
</el-form>
<el-button type="" @click="init">initWs</el-button>
</div>
</template>
<script>
// import { getToken } from "@/utils/auth";
import common from "@/utils/common";
export default {
data() {
let backendIpPort= 'localhost:8003'
common. backendIpPort ='localhost:8003'
return {
// 此处为websocket地址, 8080是springboot监听的web端口
// /websocket/execute与controller中的 ServerEndpoint 的value值保持一致
path: `ws://${common. backendIpPort}/webSocket/1`,
socket: "",
loading: false,
queryParams: {},
};
},
mounted() {
this.init();
},
methods: {
initWs(){
this.init()
},
init: function () {
if (typeof WebSocket === "undefined") {
console.log("浏览器不支持websocket");
} else {
//涉及到spring security鉴权,此处加上token (正常的http请求可以设置请求头,
// websocket无法设置,只好把token放在Sec-WebSocket-Protocol中)
// let token = getToken();
// this.socket = new WebSocket(this.path, [token]);
this.socket = new WebSocket(this.path);
this.socket.onopen = this.open;
this.socket.onerror = this.error;
this.socket.onmessage = this.getMessage;
}
},
open: function () {
console.log("websocket连接成功");
},
error: function (error) {
console.log("websocket连接错误", error);
},
getMessage: function (msg) {
console.log("收到消息,", msg);
},
send: function (params) {
this.socket.send(params);
},
close: function (e) {
console.log("websocket连接关闭");
},
submit: function () {
this.loading = true;
let params = JSON.stringify(this.queryParams);
this.send(params);
console.log("发送消息,", params);
this.loading = false;
},
},
destroyed() {
this.socket.onclose = this.close;
},
};
</script>
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:200)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:419)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:365)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:65)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:401)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1232)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1015)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:103)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:158)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
控制台把websocket的实际访问地址贴出来看看。