在服务器上,仅在/api.
在客户端调用此路径时,react-router 接管并响应:
browser.js:49 Warning: [react-router] Location "/api/my_request" did not match any routes
Run Code Online (Sandbox Code Playgroud)
我们如何告诉 react-router 绕过这个,而只是将请求发送到服务器?
更新:
这是请求的发送方式:
const sock = new SockJS('http://localhost:3030/api')
sock.onopen = () => {
console.log('open')
}
sock.onmessage = (e) => {
console.log('message', e.data)
}
sock.onclose = () => {
console.log('close')
}
sock.send('test')
Run Code Online (Sandbox Code Playgroud) 我想将聊天集成到我制作的应用程序中,在遵循一些教程并运行该应用程序后,我在控制台上不断收到“哎呀!与http://localhost:8080/ws 的连接丢失”,我尝试使用 sockjs 路径作为“/ ws”但仍然出现相同的错误,请有人向我解释我做错了什么吗?
这是我的代码片段:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/ws")
.setHandshakeHandler(new CustomHandshakeHandler())
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/message");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的客户
var socket = new SockJS('http://localhost:8080/ws');
stompClient = Stomp.over(socket);
stompClient.connect({}, onConnected, onError);
function onConnected() {
console.log("its working");
}
function onError(error) {
console.log(error);
}
Run Code Online (Sandbox Code Playgroud) 我试图找出一种方法让我的SockJS客户端重新连接到服务器,如果它应该关闭.
我目前有这个:
new_conn = function() {
socket = new SockJS(protocol + serverDomain + '/echo', null, {
'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
});
};
socket.onopen = function () {
clearInterval(recInterval);
};
socket.onclose = function () {
recInterval = window.setInterval(function () {
new_conn();
}, 2000);
};
Run Code Online (Sandbox Code Playgroud)
问题是setInterval即使在成功重新连接后仍会继续射击.似乎socket.onopen永远不会被执行.
我有什么想法可能做错了吗?
我使用了sockjs websocket服务器的spring实现,无法传输超过8KB的消息,以下是错误
2014-02-12 19:36:29,990 - org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession - DEBUG - SockJS session id=35n41xel was closed, CloseStatus [code=1009, reason=The decoded text message was too big for the output buffer and the endpoint does not support partial messages]
Run Code Online (Sandbox Code Playgroud)
任何想法如何增加缓冲区大小
我使用以下工厂作为spring sockjs利用tomcat容器(App部署在tomcat中,我也调试确认它确实使用了tomcat lib)
@Bean
public WebSocketContainerFactoryBean createWebSocketContainer() {
WebSocketContainerFactoryBean container = new WebSocketContainerFactoryBean();
container.setMaxTextMessageBufferSize(16384);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
Run Code Online (Sandbox Code Playgroud)
然后我的URL映射看起来
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(coBrowseSockJsCustomerHandler(), "/sockjs/cobrowse/customer/").withSockJS();}
Run Code Online (Sandbox Code Playgroud)
我是否需要在某处设置sockjs这个bean?sockjs怎么知道它必须使用这个工具?
我有一个Spring MVC服务器,它提供了一堆REST端点以及一个websocket端点.除登录端点之外的所有内容都需要身份验 我正在使用JWT来验证来自客户端的请求.
当用户登录时,我正在返回包含JWT令牌的X-AUTH-TOKEN标头.然后,在每个请求到服务器的同一个头中传递此令牌.这一切都适用于REST端点,但我无法弄清楚如何在websocket上执行此操作.
我正在使用SockJS,当我打开连接时:
var socket = new SockJS('/socket/updates', null, {});
Run Code Online (Sandbox Code Playgroud)
这会导致对/ socket/updates/info?t = xxx的GET请求返回403(因为默认情况下一切都需要auth).
理想情况下,我只是在SockJS发出的任何XHR请求上发送我的X-AUTH-TOKEN标头,但我看不到添加标头查看API的任何方法.
最糟糕的情况我可以改变SockJS这样做,但我想知道这个功能是否被故意遗漏了?我知道SockJS出于安全原因不支持cookie,但这不是我想要做的.
此外,为了进行测试,我确实允许信息端点具有匿名访问权限,但是在其他端点上有403个权限 - 简单地传递这些请求的auth详细信息比在我的服务器安全性中挖洞更为优雅.
任何帮助赞赏.
干杯
当SockJS发生握手时,我需要发送令牌。我尝试了许多建议的实现,但是调用了相同的异常
java.lang.IllegalArgumentException: JWT String argument cannot be null or empty.
Run Code Online (Sandbox Code Playgroud)
在后端 WebSocketConfig
@Configuration
@EnableWebSocketMessageBroker
@CrossOrigin
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/socket");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").withSockJS();
}
}
Run Code Online (Sandbox Code Playgroud)
尝试与套接字建立连接的函数。普通的javascript。
function connect() {
var socket = new SockJS('http://localhost:8889/websocket',
null,
{
transports: ['xhr-streaming'],
headers: {'Authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9...' }
});
stompClient = Stomp.over(socket);
stompClient.connect({},function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/socket/event', function (greeting) {
showGreeting(JSON.parse(greeting.body).content);
});
});
}
Run Code Online (Sandbox Code Playgroud)
问题出在握手上,这些标头似乎未正确传递令牌。我已经尝试过多种握手方式,但是我找不到正确的方式。
在握手之后尝试使用标头之前,我从这里得到了实现的想法,但我发现它立即需要令牌。
https://github.com/sockjs/sockjs-client/issues/196#issuecomment-61469141 …
我有一个 Angular 8 前端代码。构建后,它在浏览器控制台中显示以下错误。
收到警告 sockjs.js:2998 WebSocket connection to 'ws://localhost:4200/sockjs-node/173/ypgvodsj/websocket' failed: WebSocket 在连接建立之前关闭。
WebSocketTransport.prototype.close = function()
{
debug('close');
var ws = this.ws;
this._cleanup();
if (ws)
{
ws.close();
}
};
Run Code Online (Sandbox Code Playgroud) 有没有办法做到这一点?
客户端:
function connectWebSocket() {
var socket = new SockJS('/socket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("connected");
});
}
Run Code Online (Sandbox Code Playgroud)
服务器端并不重要.执行上面的代码后,我需要知道我的会话ID.
我正在尝试使用Springframework SimpMessagingTemplate(默认的Stomp实现)来传输时间序列数据,以将消息广播到SockJS客户端订阅的主题.但是,消息是无序接收的.服务器是单线程,消息按时间戳按升序发送.客户端以某种方式收到了订单中的消息.
我正在使用stompjs和springframework(4.1.6版本)的最新版本.
运行webpack-dev-server很好.首页加载正常.但是当我刷新浏览器时,每2秒'WDS disconnected'会出现在控制台和浏览器中无限重新加载.此问题仅出现在Firefox/Firefox Developer Edition浏览器中.浏览器Chrome,Edge,IE11没有问题.

package.json:
....
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"
webpack.config.js:
....
entry: [
//'app': './app/app.ts'
'webpack-dev-server/client?http://localhost:8080/', './app/app.ts'
],
output: {
publicPath: 'http://localhost:8080/',
filename: 'app/app.bundle.js'
},
....
Run Code Online (Sandbox Code Playgroud)
Firefox Developer Edition 51.0a2
sockjs ×10
websocket ×5
javascript ×4
spring ×3
stomp ×2
angular8 ×1
chromium ×1
firefox ×1
java ×1
react-router ×1
rest ×1
spring-boot ×1
webpack ×1