如果我在创建客户端后不立即连接,则使用 sockjs-client 和 stompjs 的 WebSocket 无法连接
WebSocket using sockjs-client and stompjs cannot connect if I do not connect immediately after client creation
我只尝试在页面加载时连接到遥控器一次,然后在必要时订阅频道。
为了保证代码可以被不同的页面共享,我把代码(闭包)写成如下,导出函数webSocket
如下:
export function webSocket() {
const socket = new SockJS(WebSocketEndPoint.RESOURCE_CONTROLLER);
const stompClient = Stomp.over(socket);
let isConnected = false;
function subscribe(address, callback) {
if (isConnected) {
stompClient.subscribe(address, callback);
} else {
stompClient.connect({}, () => {
isConnected = true;
stompClient.subscribe(address, callback);
});
}
};
function subscribeRealtimeExecOutput(addr, callback){
subscribe(addr, callback);
}
return {
subscribeRealtimeExecOutput,
};
}
和
在其他页面使用如下:
import { webSocket } from '../../websocket/websocketService';
//in the constructor
this.subscriber = webSocket().subscribeRealtimeExecOutput;
//used when required
this.subscriber(pane.jobIdentifier, (data) => {
console.log(data);
});
而且我希望 connect
方法在同一个页面中只会 运行 一次,然后订阅者将根据连接订阅不同的频道。
但是,它似乎不起作用 - 连接将无法建立,因此浏览器只是提示我 正在打开 Web 套接字...
有人可以帮忙吗?非常感谢您的帮助。
此致,听众
我自己在本地测试后发现,在我创建 client 并直接 connect 到服务器后,一切正常一个魅力。所以我重构了我的代码 subscribe 如下:
function subscribe(address, callback) {
if (isConnected) {
stompClient.subscribe(address, callback);
} else {
stompClient = Stomp.over(socket);
isConnected = true;
stompClient.connect({}, () => {
stompClient.subscribe(address, callback);
}, () => {
console.error('Sorry, I cannot connect to the server right now.');
});
}
}
其中,如果连接仍未建立,我再次创建 client 并在目录中设置 isConnected 并连接到服务器。
后来我就是很好奇这到底是怎么回事,想找点有意思的:
http://jmesnil.net/stomp-websocket/doc/
Once a STOMP client is created, it must call its connect() method to effectively connect and authenticate to the STOMP server.
此外
But what happens if the connection fails? the connect() method accepts an optional error_callbackargument which will be called if the client is not able to connect to the server.
还有来自 Whosebug 的 post,这进一步证实了我的重构
I create a new instance of Stomp.Client since the current one has its Web Socket closed and there is no way to reopen a Web Socket once it it closed.
我只尝试在页面加载时连接到遥控器一次,然后在必要时订阅频道。
为了保证代码可以被不同的页面共享,我把代码(闭包)写成如下,导出函数webSocket
如下:
export function webSocket() {
const socket = new SockJS(WebSocketEndPoint.RESOURCE_CONTROLLER);
const stompClient = Stomp.over(socket);
let isConnected = false;
function subscribe(address, callback) {
if (isConnected) {
stompClient.subscribe(address, callback);
} else {
stompClient.connect({}, () => {
isConnected = true;
stompClient.subscribe(address, callback);
});
}
};
function subscribeRealtimeExecOutput(addr, callback){
subscribe(addr, callback);
}
return {
subscribeRealtimeExecOutput,
};
}
和
在其他页面使用如下:
import { webSocket } from '../../websocket/websocketService';
//in the constructor
this.subscriber = webSocket().subscribeRealtimeExecOutput;
//used when required
this.subscriber(pane.jobIdentifier, (data) => {
console.log(data);
});
而且我希望 connect
方法在同一个页面中只会 运行 一次,然后订阅者将根据连接订阅不同的频道。
但是,它似乎不起作用 - 连接将无法建立,因此浏览器只是提示我 正在打开 Web 套接字...
有人可以帮忙吗?非常感谢您的帮助。
此致,听众
我自己在本地测试后发现,在我创建 client 并直接 connect 到服务器后,一切正常一个魅力。所以我重构了我的代码 subscribe 如下:
function subscribe(address, callback) {
if (isConnected) {
stompClient.subscribe(address, callback);
} else {
stompClient = Stomp.over(socket);
isConnected = true;
stompClient.connect({}, () => {
stompClient.subscribe(address, callback);
}, () => {
console.error('Sorry, I cannot connect to the server right now.');
});
}
}
其中,如果连接仍未建立,我再次创建 client 并在目录中设置 isConnected 并连接到服务器。
后来我就是很好奇这到底是怎么回事,想找点有意思的:
http://jmesnil.net/stomp-websocket/doc/
Once a STOMP client is created, it must call its connect() method to effectively connect and authenticate to the STOMP server.
此外
But what happens if the connection fails? the connect() method accepts an optional error_callbackargument which will be called if the client is not able to connect to the server.
还有来自 Whosebug 的 post,这进一步证实了我的重构
I create a new instance of Stomp.Client since the current one has its Web Socket closed and there is no way to reopen a Web Socket once it it closed.