Reactjs Can';t使用;这";在stomp客户端订阅-反应
我有我的Spring Boot服务设置,所以我可以通过websocket将消息发送到我的浏览器,并且它可以正常工作Reactjs Can';t使用;这";在stomp客户端订阅-反应,reactjs,spring-boot,websocket,spring-websocket,web-stomp,Reactjs,Spring Boot,Websocket,Spring Websocket,Web Stomp,我有我的Spring Boot服务设置,所以我可以通过websocket将消息发送到我的浏览器,并且它可以正常工作 //@MessageMapping @RequestMapping(value = "/notify") @SubscribeMapping("/notification") @SendTo("/topic/notification") public String sendNotification() throws Exception {
//@MessageMapping
@RequestMapping(value = "/notify")
@SubscribeMapping("/notification")
@SendTo("/topic/notification")
public String sendNotification() throws Exception {
sendMessage();
return "Request to update Tanks has been sent!";
}
public void sendMessage() {
this.messagingTemplate.convertAndSend("/topic/notification", "IT WORKS");
}
以下是chrome的控制台日志:
<<< MESSAGE
destination:/topic/notification
content-type:text/plain;charset=UTF-8
subscription:sub-1519225601109-13
message-id:f2qodiqn-8
content-length:8
IT WORKS
以及提取组件didmount()
我无法使用this.showNotification(notification.body)
,因此无法将状态设置为能够重新蚀刻对象。我尝试在类之外创建方法,但是我不能使用主类中的任何内容
有没有办法让react再次运行componentDidMount,或者更好,当我通过websocket从spring获得消息时,只访问类中的fetch方法
像这样:
componentDidMount(){
var socket = new SockJS("http://192.168.1.139:8610/refresh");
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('connected: ' + frame);
stompClient.subscribe('/topic/notification', function(notification){
refetchTanks(); // call fetch tanks -> can't use "this"
})
}, function(err) {
console.log('err', err);
});
谢谢 为了能够使用我的类方法和stompClient的
.subscribe
方法中的状态,我尝试了一切。如果服务死机,我可以连接并重新连接,但它无法工作
我决定使用,这很有效。我可以在onMessage=…
中使用类方法。这就是我的代码的样子:
var socket = new SockJS("http://localhost:6667/refresh");
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('connected: ' + frame);
stompClient.subscribe('/topic/notification', function(notification){
console.log(notification.body);
//this.showNotification(JSON.parse(notification.body).content);
//this.showNotification(notification.body);
})
}, function(err) {
console.log('err', err);
});
<SockJsClient
url = 'http://localhost:8610/refresh/'
topics={['/topic/notification']}
onConnect={console.log("Connection established!")}
onDisconnect={console.log("Disconnected!")}
onMessage={() => this.update()} <------ this method performs a new GET
request
debug= {true}
/>
this.update()}
我还必须在服务器端以特定的方式发送消息,因为在发送字符串时出现JSON错误
this.messagingTemplate.send("/topic/notification", "{"text":"text"}");
<<< MESSAGE
destination:/topic/notification
content-type:text/plain;charset=UTF-8
subscription:sub-0
message-id:aaylfxl4-1
content-length:49
{
"text": "text"
}
this.messagingTemplate.send(“/topic/notification”,“{”text:“text}”);
我尽了一切努力来使用我的类方法和stompClient的.subscribe
方法中的状态。如果服务死机,我可以连接并重新连接,但它无法工作
我决定使用,这很有效。我可以在onMessage=…
中使用类方法。这就是我的代码的样子:
var socket = new SockJS("http://localhost:6667/refresh");
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('connected: ' + frame);
stompClient.subscribe('/topic/notification', function(notification){
console.log(notification.body);
//this.showNotification(JSON.parse(notification.body).content);
//this.showNotification(notification.body);
})
}, function(err) {
console.log('err', err);
});
<SockJsClient
url = 'http://localhost:8610/refresh/'
topics={['/topic/notification']}
onConnect={console.log("Connection established!")}
onDisconnect={console.log("Disconnected!")}
onMessage={() => this.update()} <------ this method performs a new GET
request
debug= {true}
/>
this.update()}
我还必须在服务器端以特定的方式发送消息,因为在发送字符串时出现JSON错误
this.messagingTemplate.send("/topic/notification", "{"text":"text"}");
<<< MESSAGE
destination:/topic/notification
content-type:text/plain;charset=UTF-8
subscription:sub-0
message-id:aaylfxl4-1
content-length:49
{
"text": "text"
}
this.messagingTemplate.send(“/topic/notification”,“{”text:“text}”);
我知道,这是一个有点老的问题,但因为它弹出每次当你们搜索跺脚的问题,我想回答它。在回调中访问此对象的方法是首先将回调与此绑定,然后可以在回调中访问整个对象。
例如:
如果看到上面的代码,两个回调都可以访问这个问题。我知道,这是一个有点老的问题,但由于每次搜索stomp问题时它都会弹出,所以我想回答它。在回调中访问此对象的方法是首先将回调与此绑定,然后可以在回调中访问整个对象。
例如:
如果看到上面的代码,两个回调都可以访问它。您是否尝试在stompClient.connect方法中为回调使用箭头函数?或者您可以使用this.refetchTanks=this.refetchTanks.bind(this)将刷新槽绑定到此;在组件的结构中是的,我试过了。我得到一个“属性refetchTanks()未定义”。我无法访问类方法。您从何处导入SockJS
?是否尝试在stompClient.connect方法中对回调使用箭头函数?或者您可以使用this.refetchTanks=this.refetchTanks.bind(this)将刷新槽绑定到此;在组件的结构中是的,我试过了。我得到一个“属性refetchTanks()未定义”。我只是无法访问类方法。你从哪里导入SockJS
?谢谢你花时间!我没有考虑将函数作为参数传递。我的代码是stompClient.connect({},(frame)=>{console.log('Connected:'+frame);stompClient.subscribe('/user/api/getChat',(messageOutput)=>{//this.connectCallBack.bind(this);console.log(JSON.parse(messageOutput.body).message);var mymessage=JSON.parse(messageOutput.body).message;this.state={incomingchatMessage:[],chatMessages:[]}};});但无法刷新它。怎么办?谢谢你花时间!我没有考虑将函数作为参数传递。我的代码是stompClient.connect({},(frame)=>{console.log('Connected:'+frame);stompClient.subscribe('/user/api/getChat',(messageOutput)=>{//this.connectCallBack.bind(this);console.log(JSON.parse(messageOutput.body).message);var mymessage=JSON.parse(messageOutput.body).message;this.state={incomingchatMessage:[],chatMessages:[]}};});但无法刷新它。怎么办?